diff --git a/inputs/day_02-example.txt b/inputs/day_02-example.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/inputs/day_02-example.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/inputs/day_02.txt b/inputs/day_02.txt new file mode 100644 index 0000000..7090db6 --- /dev/null +++ b/inputs/day_02.txt @@ -0,0 +1 @@ +975119-1004307,35171358-35313940,6258659887-6258804661,737649-837633,85745820-85956280,9627354-9679470,2327309144-2327509647,301168-351545,537261-631588,364281214-364453549,9563727253-9563879587,3680-9127,388369417-388406569,6677501-6752949,650804-678722,3314531-3365076,1052-2547,24134-68316,8888820274-8888998305,82614-107458,456819-529037,358216-389777,24222539-24266446,874565-916752,3886244-3960191,25-110,9696951376-9696996784,171-671,5656545867-5656605587,75795017-75865731,1-16,181025-232078 diff --git a/src/main.rs b/src/main.rs index 601704e..998d950 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ fn main() -> Result<()> { let (solver, input_build_time) = match args.day { 1 => solver!(Day01, raw_input), + 2 => solver!(Day02, raw_input), _ => bail!("invalid day: `{}`", args.day), }; println!("Parsed input int {input_build_time:#?}"); diff --git a/src/solutions/day_02.rs b/src/solutions/day_02.rs new file mode 100644 index 0000000..0bb7a38 --- /dev/null +++ b/src/solutions/day_02.rs @@ -0,0 +1,53 @@ +use crate::solution::Solution; + +#[derive(Clone, Copy, Debug)] +struct IdRange(u64, u64); + +impl IdRange { + fn parse(range: &str) -> Self { + let (start, end) = range.split_once('-').unwrap(); + Self(start.parse().unwrap(), end.parse().unwrap()) + } + + fn ids(&self) -> impl Iterator { + (self.0..=self.1).map(Id) + } +} + +#[derive(Clone, Copy, Debug)] +struct Id(u64); + +impl Id { + fn contains_repeat(&self) -> bool { + let s = self.0.to_string(); + + if !s.len().is_multiple_of(2) { + return false; + } + + let (left, right) = s.split_at(s.len() / 2); + + left == right + } +} + +#[derive(Debug)] +pub struct Day02 { + ranges: Vec, +} + +impl Solution for Day02 { + fn with_input(input: String) -> Self { + let ranges = input.trim().split(',').map(IdRange::parse).collect(); + Self { ranges } + } + + fn part1(&self) -> String { + self.ranges + .iter() + .flat_map(IdRange::ids) + .filter_map(|id| id.contains_repeat().then_some(id.0)) + .sum::() + .to_string() + } +} diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index a4b0340..d66dbfd 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -1,3 +1,5 @@ mod day_01; +mod day_02; pub use day_01::Day01; +pub use day_02::Day02;