Skip to content

Commit c1d3ecd

Browse files
committed
2025 Day 7 cleaned
1 parent 58f2d5d commit c1d3ecd

File tree

2 files changed

+32
-64
lines changed

2 files changed

+32
-64
lines changed

src/bin/2025_07/main.rs

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::HashMap;
22

33
use aoc::{
4-
common,
4+
common::{self, HashMapCount},
55
grid::{CellIndex, Grid},
66
};
7+
use itertools::Itertools;
78

8-
fn solve2(input: &str) -> usize {
9+
fn solve(input: &str) -> (usize, usize) {
10+
let mut ans1 = 0;
911
let mut ans2 = 0;
1012
let grid = Grid::from_str(input, |c| c);
1113
let mut pos = grid.positions(&'S');
@@ -22,28 +24,16 @@ fn solve2(input: &str) -> usize {
2224
for (p, t) in space_time {
2325
if empty_spaces.contains(&p) {
2426
let new_p = (p.0 + 1, p.1);
25-
if let Some(x) = new_space_time.get_mut(&new_p) {
26-
*x += t;
27-
} else {
28-
new_space_time.insert(new_p, t);
29-
}
27+
new_space_time.insert_with_count(&new_p, t);
3028
}
3129
if tachyons.contains(&p) {
3230
if p.1 > 0 {
3331
let new_p = (p.0 + 1, p.1 - 1);
34-
if let Some(x) = new_space_time.get_mut(&new_p) {
35-
*x += t;
36-
} else {
37-
new_space_time.insert(new_p, t);
38-
}
32+
new_space_time.insert_with_count(&new_p, t);
3933
}
4034
if p.1 + 1 < grid.cols {
4135
let new_p = (p.0 + 1, p.1 + 1);
42-
if let Some(x) = new_space_time.get_mut(&new_p) {
43-
*x += t;
44-
} else {
45-
new_space_time.insert(new_p, t);
46-
}
36+
new_space_time.insert_with_count(&new_p, t);
4737
}
4838
}
4939
}
@@ -52,59 +42,21 @@ fn solve2(input: &str) -> usize {
5242
break;
5343
}
5444

45+
ans1 += tachyons
46+
.iter()
47+
.filter(|x| new_space_time.keys().contains(x))
48+
.count();
5549
ans2 = new_space_time.values().sum::<usize>();
5650
space_time = new_space_time;
5751
}
5852

5953
//grid.print();
60-
ans2
61-
}
62-
63-
fn solve1(input: &str) -> usize {
64-
let mut ans = 0;
65-
let grid = Grid::from_str(input, |c| c);
66-
let mut pos = grid.positions(&'S');
67-
pos[0].0 += 1;
68-
let tachyons = grid.positions(&'^');
69-
let empty_spaces = grid.positions(&'.');
70-
71-
while !pos.is_empty() {
72-
let cnt_empty_spaces: Vec<CellIndex> = empty_spaces
73-
.iter()
74-
.filter(|x| pos.contains(x))
75-
.map(|x| *x)
76-
.collect();
77-
let cnt: Vec<CellIndex> = tachyons
78-
.iter()
79-
.filter(|x| pos.contains(x))
80-
.map(|x| *x)
81-
.collect();
82-
let mut new_pos = HashSet::new();
83-
84-
for c in &cnt_empty_spaces {
85-
new_pos.insert((c.0 + 1, c.1));
86-
}
87-
88-
for c in &cnt {
89-
if c.1 > 0 {
90-
new_pos.insert((c.0 + 1, c.1 - 1));
91-
}
92-
if c.1 + 1 < grid.cols {
93-
new_pos.insert((c.0 + 1, c.1 + 1));
94-
}
95-
}
96-
pos = new_pos.into_iter().collect::<Vec<CellIndex>>();
97-
ans += cnt.len();
98-
}
99-
100-
//grid.print();
101-
ans
54+
(ans1, ans2)
10255
}
10356

10457
fn main() {
10558
if let Some(input) = common::get_input() {
106-
common::timed(&input, solve1, true);
107-
common::timed(&input, solve2, false);
59+
common::timed(&input, solve, true);
10860
}
10961
}
11062

@@ -115,7 +67,6 @@ mod tests {
11567
#[test]
11668
fn test_samples() {
11769
let sample_input = ".......S.......\n...............\n.......^.......\n...............\n......^.^......\n...............\n.....^.^.^.....\n...............\n....^.^...^....\n...............\n...^.^...^.^...\n...............\n..^...^.....^..\n...............\n.^.^.^.^.^...^.\n...............";
118-
assert_eq!(solve1(sample_input), 21);
119-
assert_eq!(solve2(sample_input), 40);
70+
assert_eq!(solve(sample_input), (21, 40));
12071
}
12172
}

src/common.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ impl<K: Copy + Hash + Eq + PartialEq, V, S: BuildHasher> HashMapVector<K, V>
4040
}
4141
}
4242

43+
pub trait HashMapCount<K: Copy + Hash + Eq + PartialEq> {
44+
fn insert_with_count(&mut self, key: &K, value: usize);
45+
}
46+
47+
impl<K: Copy + Hash + Eq + PartialEq> HashMapCount<K> for HashMap<K, usize> {
48+
fn insert_with_count(&mut self, key: &K, value: usize) {
49+
match self.get_mut(key) {
50+
Some(vals) => {
51+
*vals += value;
52+
}
53+
None => {
54+
self.insert(*key, value);
55+
}
56+
}
57+
}
58+
}
59+
4360
pub fn minmax<T: Ord + Copy>(x: &T, y: &T) -> (T, T) {
4461
(*x.min(y), *x.max(y))
4562
}

0 commit comments

Comments
 (0)