diff --git a/.aoc_tiles/tiles/2025/05.png b/.aoc_tiles/tiles/2025/05.png new file mode 100644 index 0000000..fd91c3e Binary files /dev/null and b/.aoc_tiles/tiles/2025/05.png differ diff --git a/README.md b/README.md index effef6d..205bb05 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- 2025 - 8 ⭐ - Rust + 2025 - 9 ⭐ - Rust

@@ -14,4 +14,7 @@ + + + diff --git a/src/day5.rs b/src/day5.rs new file mode 100644 index 0000000..0ffc29a --- /dev/null +++ b/src/day5.rs @@ -0,0 +1,75 @@ +use std::ops::RangeInclusive; + +use aoc_runner_derive::{aoc, aoc_generator}; + +struct Database { + fresh_ingredients: Vec>, + available_ingredients: Vec, +} + +#[aoc_generator(day5)] +fn parse(input: &str) -> Database { + let mut fresh_ingredients = Vec::new(); + let mut available_ingredients = Vec::new(); + let mut parsing_ranges = true; + for line in input.lines() { + if line == "" { + parsing_ranges = false; + continue; + } + if parsing_ranges { + let (start, end) = line.split_once(|c| c == '-').unwrap(); + fresh_ingredients.push(RangeInclusive::new( + start.parse().unwrap(), + end.parse().unwrap(), + )); + } else { + available_ingredients.push(line.parse().unwrap()) + } + } + Database { + fresh_ingredients, + available_ingredients, + } +} + +#[aoc(day5, part1)] +fn part1(input: &Database) -> u64 { + input + .available_ingredients + .iter() + .filter(|i| input.fresh_ingredients.iter().any(|r| r.contains(i))) + .count() as u64 +} + +#[aoc(day5, part2)] +fn part2(input: &Database) -> u64 { + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE: &str = "3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32"; + + #[test] + fn part1_example() { + assert_eq!(part1(&parse(EXAMPLE)), 3); + } + + #[test] + fn part2_example() { + assert_eq!(part2(&parse(EXAMPLE)), 0); + } +}