summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-12-01 22:41:26 -0500
committerJesse Luehrs <doy@tozt.net>2020-12-01 22:43:01 -0500
commit2ce3804c08f8b49d8a921c07f02dba68df047e66 (patch)
tree1ff66438456feb80a39244e95c1661b1c7558b49
parentff32088cffa839acd41b68fdce1db1d6c7fd6165 (diff)
downloadadvent-of-code-2ce3804c08f8b49d8a921c07f02dba68df047e66.tar.gz
advent-of-code-2ce3804c08f8b49d8a921c07f02dba68df047e66.zip
puzzle 1-2
-rw-r--r--data/1.txt (renamed from data/1-1.txt)0
-rw-r--r--src/2020/mod.rs44
2 files changed, 32 insertions, 12 deletions
diff --git a/data/1-1.txt b/data/1.txt
index 15a9273..15a9273 100644
--- a/data/1-1.txt
+++ b/data/1.txt
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<Vec<i32>> {
+ 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<Vec<i32>> = 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
}