day5: part1

This commit is contained in:
2025-12-04 21:07:02 -08:00
parent d49157dd79
commit 0ee1e316d7
3 changed files with 79 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN -->
<h1 align="center">
2025 - 8 ⭐ - Rust
2025 - 9 ⭐ - Rust
</h1>
<a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
@@ -14,4 +14,7 @@
<a href="src/day4.rs">
<img src=".aoc_tiles/tiles/2025/04.png" width="161px">
</a>
<a href="src/day5.rs">
<img src=".aoc_tiles/tiles/2025/05.png" width="161px">
</a>
<!-- AOC TILES END -->

75
src/day5.rs Normal file
View File

@@ -0,0 +1,75 @@
use std::ops::RangeInclusive;
use aoc_runner_derive::{aoc, aoc_generator};
struct Database {
fresh_ingredients: Vec<RangeInclusive<u64>>,
available_ingredients: Vec<u64>,
}
#[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);
}
}