diff --git a/2020/Cargo.lock b/2020/Cargo.lock new file mode 100644 index 0000000..f2840e8 --- /dev/null +++ b/2020/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "advent2020" +version = "0.1.0" +dependencies = [ + "anyhow", +] + +[[package]] +name = "anyhow" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" diff --git a/2020/Cargo.toml b/2020/Cargo.toml new file mode 100644 index 0000000..e948882 --- /dev/null +++ b/2020/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "advent2020" +version = "0.1.0" +authors = ["Bill Thiede "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.34" diff --git a/2020/src/bin/1-input.txt b/2020/src/bin/1-input.txt new file mode 100644 index 0000000..802b74e --- /dev/null +++ b/2020/src/bin/1-input.txt @@ -0,0 +1,200 @@ +1619 +1919 +1441 +1861 +1932 +1514 +1847 +1871 +1764 +1467 +1970 +1589 +2009 +1429 +1098 +1327 +1502 +1398 +1710 +1562 +1512 +1468 +1762 +1348 +1356 +1950 +1266 +1969 +1815 +1583 +1959 +1092 +1694 +1814 +1763 +1151 +1981 +1193 +1614 +1413 +1642 +1943 +1407 +895 +1430 +1706 +1962 +1522 +1486 +1986 +1623 +1489 +1411 +1851 +1817 +1416 +1654 +1438 +1419 +1649 +1362 +690 +1804 +1452 +1766 +1360 +1807 +1385 +1964 +1626 +1832 +745 +1702 +1602 +1471 +1996 +1915 +1813 +1460 +1925 +1638 +1581 +1584 +1379 +1148 +1554 +1564 +1914 +1757 +1820 +1559 +1096 +1944 +1587 +1499 +390 +1733 +1371 +1781 +2002 +324 +1655 +1639 +1482 +1198 +1264 +1953 +1320 +1704 +1321 +1449 +1455 +1509 +1765 +1797 +1703 +1758 +1610 +1756 +1901 +1707 +1968 +1601 +1328 +1336 +1592 +1678 +1699 +1793 +1957 +2000 +1306 +1094 +1545 +1331 +1751 +1739 +1335 +1753 +1983 +1966 +1934 +1831 +1426 +1711 +1840 +1857 +1347 +1789 +1409 +1310 +1752 +1897 +1497 +1485 +1125 +1803 +1577 +919 +1635 +1791 +1456 +1796 +1974 +1954 +1828 +2004 +1890 +1376 +1569 +1406 +1463 +2006 +1109 +1620 +1656 +1870 +1498 +1645 +1145 +1681 +1269 +1527 +1621 +1575 +1324 +1647 +1519 +1697 +1421 +1216 +1846 +1625 +1585 +1369 +1882 +1823 +1388 +1548 +1879 diff --git a/2020/src/bin/1-test.txt b/2020/src/bin/1-test.txt new file mode 100644 index 0000000..e3fb011 --- /dev/null +++ b/2020/src/bin/1-test.txt @@ -0,0 +1,6 @@ +1721 +979 +366 +299 +675 +1456 diff --git a/2020/src/bin/1.rs b/2020/src/bin/1.rs new file mode 100644 index 0000000..c833a44 --- /dev/null +++ b/2020/src/bin/1.rs @@ -0,0 +1,82 @@ +//! --- Day 1: Report Repair --- +//! After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you. +//! +//! The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room. +//! +//! To save your vacation, you need to get all fifty stars by December 25th. +//! +//! Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! +//! +//! Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. +//! +//! Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. +//! +//! For example, suppose your expense report contained the following: +//! +//! 1721 +//! 979 +//! 366 +//! 299 +//! 675 +//! 1456 +//! In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. +//! +//! Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? + +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::Path; + +use anyhow::{anyhow, Result}; + +fn main() -> Result<()> { + let path = std::env::args() + .nth(1) + .ok_or(anyhow!("Usage: 1 "))?; + let nums = parse(path)?; + let pair = find_2020(nums).ok_or(anyhow!("Couldn't find pairs summing to 2020"))?; + println!("Product of {} x {} = {}", pair.0, pair.1, pair.0 * pair.1); + Ok(()) +} + +/// Finds pairs of numbers in `nums` that sum to 2020. If no pairs are found, `None` is returned. +fn find_2020(nums: Vec) -> Option<(u32, u32)> { + for (idx, first) in nums.iter().enumerate() { + for second in nums.iter().skip(idx + 1) { + if first + second == 2020 { + return Some((*first, *second)); + } + } + } + None +} + +/// Reads text file containing one integer per line, and parses them into `Vec`. Any +/// non-number will result in an error returned. +fn parse>(path: P) -> Result> { + let f = File::open(path)?; + let f = BufReader::new(f); + let mut nums = Vec::new(); + for line in f.lines() { + let num: u32 = line?.parse()?; + nums.push(num) + } + Ok(nums) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse() { + let nums = parse("src/bin/1-test.txt").expect("failed to parse"); + assert_eq!(nums, vec![1721, 979, 366, 299, 675, 1456]); + } + + #[test] + fn test_find_2020() { + let nums = parse("src/bin/1-test.txt").expect("failed to parse"); + assert_eq!(find_2020(nums), Some((1721, 299))); + } +}