diff options
author | Irene Knapp <ireneista@gmail.com> | 2021-11-30 18:44:19 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2021-11-30 18:44:19 -0800 |
commit | 0fc36ff992d4ea1a7fe5240da8c5c63f7c602a6e (patch) | |
tree | 02ccf50a7a7f4500bc41ca997bfb4618b07bffb3 /01 |
Initial, based on last year
Diffstat (limited to '01')
-rw-r--r-- | 01/Cargo.toml | 11 | ||||
-rw-r--r-- | 01/input | 200 | ||||
-rw-r--r-- | 01/src/main.rs | 68 | ||||
-rw-r--r-- | 01/tests/main.rs | 17 |
4 files changed, 296 insertions, 0 deletions
diff --git a/01/Cargo.toml b/01/Cargo.toml new file mode 100644 index 0000000..4534ef6 --- /dev/null +++ b/01/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "advent_01" +version = "0.1.0" +authors = ["Irene Knapp <ireneista@gmail.com>"] +edition = "2018" + +[dependencies] +advent_lib = { path = "../lib" } + +[dev-dependencies] +assert_cmd = "0.10" diff --git a/01/input b/01/input new file mode 100644 index 0000000..0cf9a7b --- /dev/null +++ b/01/input @@ -0,0 +1,200 @@ +1130 +1897 +1850 +1218 +1198 +1761 +1082 +1742 +1821 +1464 +1834 +1413 +1917 +1746 +1954 +1942 +1560 +1227 +1852 +1976 +1773 +1404 +1824 +1011 +1532 +1306 +1819 +1739 +1540 +1973 +1436 +1196 +1176 +1856 +1332 +1617 +1895 +1749 +1718 +1536 +1811 +113 +1008 +1908 +1799 +1914 +1603 +1782 +1980 +1228 +1838 +2006 +1953 +1846 +1903 +1470 +1774 +1599 +1446 +1324 +1054 +1952 +1928 +1997 +1764 +1943 +1932 +1615 +1428 +1036 +721 +1097 +1998 +1033 +1892 +1904 +1803 +1825 +1370 +1836 +1853 +1963 +1469 +1385 +246 +1987 +1153 +178 +1790 +1927 +1139 +1865 +1804 +1974 +1235 +1681 +1185 +2009 +1894 +1141 +1203 +1808 +1867 +1274 +1891 +1779 +1342 +1920 +851 +1994 +1975 +1979 +1880 +1647 +1365 +448 +1119 +1256 +1212 +1268 +1878 +1805 +1889 +1870 +1906 +1959 +1898 +1305 +1559 +1088 +1845 +1783 +1841 +1864 +1961 +1267 +1437 +1823 +801 +1579 +1538 +1745 +1972 +1259 +1899 +1517 +1940 +1543 +1882 +1933 +1240 +1608 +1263 +1429 +1197 +1508 +1631 +1988 +1350 +1638 +1800 +1999 +1822 +1776 +1896 +1610 +1831 +1921 +1535 +1526 +1491 +1876 +1476 +1945 +1702 +1900 +1814 +1289 +1992 +1859 +1967 +1966 +1283 +2002 +1195 +1066 +1924 +1968 +1835 +1971 +1977 +1430 +1844 +1465 +1595 +1957 +1472 +219 +1851 +1955 diff --git a/01/src/main.rs b/01/src/main.rs new file mode 100644 index 0000000..b7cdbbf --- /dev/null +++ b/01/src/main.rs @@ -0,0 +1,68 @@ +use advent_lib::prelude::*; + +use std::collections::BTreeSet; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let mut input = advent_lib::read_int_file(&filename)?; + + input.sort(); + + let mut input_set = BTreeSet::new(); + for item in &input { + input_set.insert(item); + } + + for i in 0 .. input.len() { + let a = input[i]; + if a > 2020 { + break; + } + + let b = 2020 - a; + if input_set.contains(&b) { + let product = a * b; + println!("a: {:?}, b: {:?}, a*b: {:?}", a, b, product); + break; + } + } + + let mut done = false; + for i in 0 .. input.len() { + if done { + break; + } + + let a = input[i]; + if a > 2020 { + break; + } + + for j in i+1 .. input.len() { + let b = input[j]; + + if a + b > 2020 { + break; + } + + let c = 2020 - a - b; + if input_set.contains(&c) { + let product = a * b * c; + println!("a: {:?}, b: {:?}, c: {:?}, a*b*c: {:?}", a, b, c, product); + + done = true; + break; + } + } + } + + Ok(()) +} + diff --git a/01/tests/main.rs b/01/tests/main.rs new file mode 100644 index 0000000..b3d3cec --- /dev/null +++ b/01/tests/main.rs @@ -0,0 +1,17 @@ +use assert_cmd::prelude::*; +//use predicates::prelude::*; +use std::process::Command; + + +#[test] +fn personal_input() -> Result<(), Box<dyn std::error::Error>> { + let mut command = Command::cargo_bin("advent_01")?; + + command.arg("input"); + command.assert().success().stdout( + "a: 246, b: 1774, a*b: 436404\n\ + a: 448, b: 721, c: 851, a*b*c: 274879808\n"); + + Ok(()) +} + |