diff --git a/day01/Cargo.lock b/day01/Cargo.lock new file mode 100644 index 0000000000000000000000000000000000000000..6c920f66d1a8e50dcb0183bffa182a2092c845b9 --- /dev/null +++ b/day01/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day01" +version = "0.1.0" diff --git a/day01/Cargo.toml b/day01/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..f00fd2c768cae1ab9af02587408205e3bf575154 --- /dev/null +++ b/day01/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day01" +version = "0.1.0" +authors = ["Philip Molares <philip.molares@udo.edu>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day01/input.txt b/day01/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..e858abe12089c790cc115ba06907463d79fa600f --- /dev/null +++ b/day01/input.txt @@ -0,0 +1,200 @@ +1509 +1857 +1736 +1815 +1576 +1970 +1567 +1778 +1508 +1833 +1377 +1890 +1375 +1396 +1102 +1639 +1818 +1469 +1138 +1333 +1906 +1557 +1686 +1712 +1990 +1930 +1761 +1881 +1551 +1627 +1801 +1728 +1960 +1407 +1832 +1842 +1393 +1870 +1295 +1528 +251 +1945 +1589 +1850 +1650 +1793 +1997 +1758 +1477 +1697 +1081 +1825 +1899 +1171 +1104 +1839 +1974 +1630 +1831 +1671 +1723 +1811 +1489 +1647 +1486 +1107 +1786 +1680 +1942 +1640 +1112 +1703 +1315 +1769 +1966 +997 +2010 +1635 +1196 +383 +1986 +1860 +1743 +1756 +1555 +1111 +1823 +48 +1953 +1083 +1804 +1933 +1626 +1895 +1807 +1669 +1783 +389 +1821 +1883 +1114 +1587 +1941 +1725 +1646 +456 +1550 +1939 +1975 +1324 +1201 +1018 +1001 +1402 +1885 +1481 +1633 +1781 +1622 +1822 +1559 +1696 +1510 +1251 +1732 +1790 +1813 +1695 +1121 +704 +1964 +1984 +1763 +1656 +1183 +1771 +1276 +1764 +1810 +1992 +1213 +1840 +1318 +1965 +1943 +1549 +1768 +1506 +1949 +1739 +1852 +1787 +1570 +1988 +1357 +1909 +1837 +561 +1994 +1777 +1547 +1925 +1897 +1817 +1677 +1668 +1982 +1667 +1753 +1041 +1826 +1961 +1797 +1765 +1720 +1835 +1688 +1705 +1744 +1977 +1971 +1775 +1782 +1661 +1385 +1162 +1755 +1846 +1674 +1698 +1882 +1766 +1820 +1531 +1577 +1710 +1382 +1246 +1864 +1702 diff --git a/day01/src/main.rs b/day01/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..852682a08a8e2f9a00e075ccd72f8871a24a77af --- /dev/null +++ b/day01/src/main.rs @@ -0,0 +1,63 @@ +use std::fs::File; +use std::io::{self, prelude::*, BufReader}; + +const WANTED_SUM: i64 = 2020; + +fn find_sum(numbers: &[i64], first: &mut usize, last: &mut usize, wanted_sum: i64) { + while first < last { + let sum = numbers[*first] + numbers[*last]; + if sum < wanted_sum { + *first += 1; + } else if sum > wanted_sum { + *last -= 1; + } else { + break; + } + } +} + +fn main() -> io::Result<()> { + println!("Advent of Code 2020 – Day 1:"); + + let file = File::open("input.txt")?; + let reader = BufReader::new(file); + + let mut input_numbers: Vec<i64> = reader + .lines() + .map(|l| { + l.unwrap() + .parse::<i64>() + .expect("Error: Could not parse each line") + }) + .collect(); + + input_numbers.sort(); + + let mut first = 0; + let mut last = input_numbers.len() - 1; + + find_sum(&input_numbers, &mut first, &mut last, WANTED_SUM); + + let product: i64 = input_numbers[first] * input_numbers[last]; + + println!("Answer Part 1: {} * {} = {}", input_numbers[first], input_numbers[last], product); + + let mut middle = 0; + + for index in 0..(input_numbers.len() - 1) { + first = index + 1; + last = input_numbers.len() - 1; + find_sum(&input_numbers, &mut first, &mut last, WANTED_SUM-input_numbers[index]); + let sum = input_numbers[first] + input_numbers[index] + input_numbers[last]; + if sum == WANTED_SUM { + middle = index; + break; + } + } + + let product2: i64 = input_numbers[first] * input_numbers[middle] * input_numbers[last]; + + println!("Answer Part 2: {} * {} * {} = {}", input_numbers[first], input_numbers[middle], input_numbers[last], product2); + + Ok(()) +}