From 7082fbb0058c0f518597d84f5c5953091deb6575 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sun, 1 Dec 2024 11:40:15 +0100 Subject: [PATCH] add part2 --- 2024/data/01/{1.out => 1.part1.out} | 0 2024/data/01/1.part2.out | 1 + 2024/src/bin/01.rs | 31 ++++++++++++++++++++++------ 2024/src/lib.rs | 32 ++++++++++++++++++++--------- 4 files changed, 48 insertions(+), 16 deletions(-) rename 2024/data/01/{1.out => 1.part1.out} (100%) create mode 100644 2024/data/01/1.part2.out diff --git a/2024/data/01/1.out b/2024/data/01/1.part1.out similarity index 100% rename from 2024/data/01/1.out rename to 2024/data/01/1.part1.out diff --git a/2024/data/01/1.part2.out b/2024/data/01/1.part2.out new file mode 100644 index 0000000..b74e882 --- /dev/null +++ b/2024/data/01/1.part2.out @@ -0,0 +1 @@ +31 \ No newline at end of file diff --git a/2024/src/bin/01.rs b/2024/src/bin/01.rs index 7f74d2e..4e60642 100644 --- a/2024/src/bin/01.rs +++ b/2024/src/bin/01.rs @@ -1,17 +1,20 @@ -use std::{fs::File, io::{BufRead, BufReader}}; +use std::{collections::HashSet, fs::File, io::{BufRead, BufReader}}; use anyhow::{anyhow, Result}; use aoc2024::{Context, Day}; use itertools::Itertools; -fn part1(ctx: &Context) -> Result { - let tuples = BufReader::new(File::open(&ctx.args.input)?).lines().map(|line| -> Result<_> { +fn tuples(ctx: &Context) -> Result>> { + Ok(BufReader::new(File::open(&ctx.args.input)?).lines().map(|line| -> Result<_> { let line = line?; let nums = line.split_whitespace().map(|n| n.parse::()).filter_map(|n| n.ok()).collect_tuple::<(u32, u32)>().ok_or(anyhow!("Invalid input"))?; - + Ok(nums) - }); + })) +} +fn part1(ctx: &Context) -> Result { + let tuples = tuples(ctx)?; let mut first = Vec::new(); let mut second = Vec::new(); for res in tuples { @@ -26,6 +29,22 @@ fn part1(ctx: &Context) -> Result { Ok(sum.to_string()) } +fn part2(ctx: &Context) -> Result { + let tuples = tuples(ctx)?; + + let mut first = HashSet::new(); + let mut second = Vec::new(); + + for res in tuples { + let (x, y) = res?; + first.insert(x); + second.push(y); + } + + let sum = second.iter().filter(|y| first.contains(&y)).sum::(); + Ok(sum.to_string()) +} + fn main() { - Day::new().part1(part1).run(); + Day::new().part1(part1).part2(part2).run(); } \ No newline at end of file diff --git a/2024/src/lib.rs b/2024/src/lib.rs index 3518b3b..45bd70c 100644 --- a/2024/src/lib.rs +++ b/2024/src/lib.rs @@ -19,7 +19,8 @@ pub struct Args { pub struct Context { pub args: Args, pub day: u8, - pub output: String + pub output1: String, + pub output2: String, } impl Context { @@ -33,15 +34,25 @@ impl Context { .unwrap() .parse() .unwrap(); - let mut output = args.input.clone(); - if let Some(pos) = output.rfind('.') { - output.replace_range(pos.., ".out"); + let mut output1 = args.input.clone(); + let mut output2 = args.input.clone(); + if let Some(pos) = output1.rfind('.') { + output1.replace_range(pos.., ".part1.out"); + output2.replace_range(pos.., ".part2.out"); } - Self { args, day, output } + Self { args, day, output1, output2 } } - fn read_output(&self) -> Option { - std::fs::read_to_string(&self.output).ok() + fn output(&self, part: u8) -> &str { + match part { + 1 => &self.output1, + 2 => &self.output2, + _ => unreachable!() + } + } + + fn read_output(&self, part: u8) -> Option { + std::fs::read_to_string(&self.output(part)).ok() } } @@ -78,7 +89,7 @@ impl Day { fn verify_part(&self, part: u8, ctx: &Context) -> Result<()> { println!("Day {} Part {}", ctx.day, part); - let expected = ctx.read_output(); + let expected = ctx.read_output(part); let start = std::time::Instant::now(); let result = match part { 1 => self.part1.as_ref().unwrap()(ctx), @@ -97,8 +108,9 @@ impl Day { } } else { println!("{}", result); - std::fs::write(ctx.output.to_string() + ".run", &result)?; - println!("Output written to {}", ctx.output.to_string() + ".run"); + let path = ctx.output(part).to_string() + ".run"; + std::fs::write(&path, &result)?; + println!("Output written to {} - no example output provided, nothing to diff.", path); } println!("OK"); Ok(())