Advent 2025 Day 5, Part 1 & 2
This commit is contained in:
parent
4d1a0ff8c6
commit
8d5d5fd758
6 changed files with 1278 additions and 2 deletions
1187
advent_of_code_2025/src/day5/input.txt
Normal file
1187
advent_of_code_2025/src/day5/input.txt
Normal file
File diff suppressed because it is too large
Load diff
2
advent_of_code_2025/src/day5/mod.rs
Normal file
2
advent_of_code_2025/src/day5/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod part1;
|
||||
pub mod part2;
|
||||
23
advent_of_code_2025/src/day5/part1.rs
Normal file
23
advent_of_code_2025/src/day5/part1.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
pub fn solve(input: &str) {
|
||||
let (fresh_ids, inventory) = input.split_once("\r\n\r\n").unwrap();
|
||||
|
||||
let fresh_ids = fresh_ids
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let (start, end) = line.split_once("-").unwrap();
|
||||
(start.parse::<u64>().unwrap(), end.parse::<u64>().unwrap())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let inventory = inventory.lines().map(|line| line.parse::<u64>().unwrap()).collect::<Vec<_>>();
|
||||
|
||||
let mut fresh_count: u64 = 0;
|
||||
|
||||
for produce in inventory.iter() {
|
||||
if fresh_ids.iter().any(|(start, end)| produce >= start && produce <= end) {
|
||||
fresh_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("fresh count: {fresh_count}");
|
||||
}
|
||||
53
advent_of_code_2025/src/day5/part2.rs
Normal file
53
advent_of_code_2025/src/day5/part2.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
pub fn solve(input: &str) {
|
||||
let (fresh_ids, _) = input.split_once("\r\n\r\n").unwrap();
|
||||
|
||||
let mut fresh_ids = fresh_ids
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let (start, end) = line.split_once("-").unwrap();
|
||||
(start.parse::<u64>().unwrap(), end.parse::<u64>().unwrap())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut merged_fresh_ids: Vec<(u64, u64)> = Vec::new();
|
||||
|
||||
loop {
|
||||
fresh_ids.sort_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
|
||||
|
||||
let mut idx = 0usize;
|
||||
while idx < fresh_ids.len() {
|
||||
let first = fresh_ids[idx];
|
||||
|
||||
if idx == fresh_ids.len() - 1 {
|
||||
merged_fresh_ids.push(first);
|
||||
break;
|
||||
}
|
||||
|
||||
let second = fresh_ids[idx + 1];
|
||||
|
||||
if first.1 >= second.0 || first.1 >= second.1 {
|
||||
let merged = (first.0, first.1.max(second.1));
|
||||
merged_fresh_ids.push(merged);
|
||||
idx += 1;
|
||||
} else {
|
||||
merged_fresh_ids.push(first);
|
||||
}
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
if fresh_ids.len() == merged_fresh_ids.len() {
|
||||
break;
|
||||
}
|
||||
|
||||
fresh_ids = merged_fresh_ids.clone();
|
||||
merged_fresh_ids.clear();
|
||||
}
|
||||
|
||||
let mut number_of_fresh: u64 = 0;
|
||||
for (start, end) in merged_fresh_ids {
|
||||
number_of_fresh += (end + 1) - start;
|
||||
}
|
||||
|
||||
println!("number of fresh: {number_of_fresh}");
|
||||
}
|
||||
11
advent_of_code_2025/src/day5/test.txt
Normal file
11
advent_of_code_2025/src/day5/test.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
use paste::paste;
|
||||
mod utility;
|
||||
|
||||
solve_day!{4}
|
||||
// test_day!{4}
|
||||
solve_day!{5}
|
||||
// test_day!{5}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue