From 2ce3804c08f8b49d8a921c07f02dba68df047e66 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 1 Dec 2020 22:41:26 -0500 Subject: puzzle 1-2 --- data/1-1.txt | 200 -------------------------------------------------------- data/1.txt | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/2020/mod.rs | 44 +++++++++---- 3 files changed, 232 insertions(+), 212 deletions(-) delete mode 100644 data/1-1.txt create mode 100644 data/1.txt diff --git a/data/1-1.txt b/data/1-1.txt deleted file mode 100644 index 15a9273..0000000 --- a/data/1-1.txt +++ /dev/null @@ -1,200 +0,0 @@ -1511 -1344 -1925 -1970 -1864 -1951 -1557 -1984 -1743 -1526 -1972 -1945 -1969 -1760 -2008 -1592 -736 -1963 -1994 -2009 -1777 -1856 -1899 -1926 -1850 -687 -2005 -1094 -1949 -1326 -2002 -1805 -1493 -1341 -1828 -1778 -1767 -1364 -1973 -1768 -1929 -1377 -2000 -1726 -1913 -2001 -1574 -1859 -1793 -1957 -1959 -1388 -1593 -1392 -724 -1962 -1999 -252 -1982 -1662 -1892 -1610 -1343 -1831 -1862 -1991 -1394 -1946 -1935 -1986 -1911 -1358 -1322 -1956 -1988 -1758 -1490 -1998 -1744 -1844 -1294 -1764 -1543 -1560 -1562 -1747 -1870 -1292 -1989 -1752 -1471 -1980 -1897 -1544 -1914 -1923 -1944 -1375 -1987 -1993 -1742 -1975 -1479 -1977 -1934 -1939 -1950 -1992 -1983 -1474 -1643 -2010 -1814 -1942 -322 -1425 -1646 -1878 -1410 -1927 -1761 -1948 -1779 -1753 -1847 -274 -1659 -1773 -1960 -1772 -1674 -1809 -1568 -1978 -1952 -1947 -1976 -1953 -1961 -1937 -1932 -1781 -2007 -1941 -1393 -1573 -1745 -169 -89 -1408 -1974 -1810 -1979 -1967 -890 -1958 -1930 -1954 -1759 -720 -1936 -1576 -1407 -2004 -1964 -1462 -1875 -1943 -1938 -2006 -1739 -1378 -1922 -1924 -2003 -1792 -1985 -1729 -1966 -1355 -1940 -1928 -1357 -1955 -1896 -1115 -1836 -1971 -1329 -1807 -1997 -1359 -1801 -1933 -1965 -1981 -1711 -1905 -1625 -1968 diff --git a/data/1.txt b/data/1.txt new file mode 100644 index 0000000..15a9273 --- /dev/null +++ b/data/1.txt @@ -0,0 +1,200 @@ +1511 +1344 +1925 +1970 +1864 +1951 +1557 +1984 +1743 +1526 +1972 +1945 +1969 +1760 +2008 +1592 +736 +1963 +1994 +2009 +1777 +1856 +1899 +1926 +1850 +687 +2005 +1094 +1949 +1326 +2002 +1805 +1493 +1341 +1828 +1778 +1767 +1364 +1973 +1768 +1929 +1377 +2000 +1726 +1913 +2001 +1574 +1859 +1793 +1957 +1959 +1388 +1593 +1392 +724 +1962 +1999 +252 +1982 +1662 +1892 +1610 +1343 +1831 +1862 +1991 +1394 +1946 +1935 +1986 +1911 +1358 +1322 +1956 +1988 +1758 +1490 +1998 +1744 +1844 +1294 +1764 +1543 +1560 +1562 +1747 +1870 +1292 +1989 +1752 +1471 +1980 +1897 +1544 +1914 +1923 +1944 +1375 +1987 +1993 +1742 +1975 +1479 +1977 +1934 +1939 +1950 +1992 +1983 +1474 +1643 +2010 +1814 +1942 +322 +1425 +1646 +1878 +1410 +1927 +1761 +1948 +1779 +1753 +1847 +274 +1659 +1773 +1960 +1772 +1674 +1809 +1568 +1978 +1952 +1947 +1976 +1953 +1961 +1937 +1932 +1781 +2007 +1941 +1393 +1573 +1745 +169 +89 +1408 +1974 +1810 +1979 +1967 +890 +1958 +1930 +1954 +1759 +720 +1936 +1576 +1407 +2004 +1964 +1462 +1875 +1943 +1938 +2006 +1739 +1378 +1922 +1924 +2003 +1792 +1985 +1729 +1966 +1355 +1940 +1928 +1357 +1955 +1896 +1115 +1836 +1971 +1329 +1807 +1997 +1359 +1801 +1933 +1965 +1981 +1711 +1905 +1625 +1968 diff --git a/src/2020/mod.rs b/src/2020/mod.rs index 87c213b..27e56fc 100644 --- a/src/2020/mod.rs +++ b/src/2020/mod.rs @@ -4,13 +4,42 @@ use std::io::BufRead as _; pub fn run(day: u8, puzzle: u8) -> anyhow::Result<()> { match (day, puzzle) { (1, 1) => report_repair(), + (1, 2) => report_repair_2(), _ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)), } } fn report_repair() -> anyhow::Result<()> { - let f = std::fs::File::open("data/1-1.txt") - .context("couldn't find data file")?; + let ints = read_ints("data/1.txt")?; + for i in &ints { + for j in &ints { + if i + j == 2020 { + println!("{} * {} = {}", i, j, i * j); + return Ok(()); + } + } + } + Err(anyhow::anyhow!("no numbers summing to 2020 found")) +} + +fn report_repair_2() -> anyhow::Result<()> { + let ints = read_ints("data/1.txt")?; + for i in &ints { + for j in &ints { + for k in &ints { + if i + j + k == 2020 { + println!("{} * {} * {} = {}", i, j, k, i * j * k); + return Ok(()); + } + } + } + } + Err(anyhow::anyhow!("no numbers summing to 2020 found")) +} + +fn read_ints(filename: &str) -> anyhow::Result> { + let f = std::fs::File::open(filename) + .with_context(|| format!("couldn't find data file {}", filename))?; let f = std::io::BufReader::new(f); let ints: anyhow::Result> = f .lines() @@ -20,14 +49,5 @@ fn report_repair() -> anyhow::Result<()> { .context("failed to parse line into an integer") }) .collect(); - let ints = ints?; - for i in &ints { - for j in &ints { - if i + j == 2020 { - println!("{} + {} = {}", i, j, i * j); - return Ok(()); - } - } - } - Err(anyhow::anyhow!("no numbers summing to 2020 found")) + ints } -- cgit v1.2.3