Skip to content

Commit 820eb61

Browse files
committed
Add 2024 day14 part2 solution
1 parent a3b7da4 commit 820eb61

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

2024/day14/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ $ cargo run --release -- --input 2024/day14/input.txt 2024 14 part1
2424

2525
```bash
2626
$ cargo run --release -- --input 2024/day14/input.txt 2024 14 part2
27-
TODO
27+
7572
2828
```

src/year2024/day14.rs

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::collections::HashMap;
1+
use std::collections::{HashMap, HashSet};
2+
use std::fmt::Display;
23
use std::ops::{Deref, Rem};
34
use std::str::FromStr;
45
use std::sync::LazyLock;
@@ -52,23 +53,57 @@ fn part2(input: String) -> Result<()> {
5253
let mut robots = Robots::from_str(&input).unwrap();
5354
info!("robots: {robots:?}");
5455

55-
let max_iterations = 1_000_000;
56-
let mut iterations = 0;
57-
loop {
58-
if iterations == max_iterations {
59-
panic!("max iterations ({max_iterations}) reached without a solution");
60-
}
61-
62-
iterations += 1;
56+
let max_iterations = 10_000;
57+
let mut list: Vec<_> = (0..max_iterations)
58+
.map(|i| {
59+
if i != 0 {
60+
robots.move_once();
61+
}
62+
// I got this idea from reddit.. still very satisfying!
63+
//
64+
// ###############################
65+
// #.............................#
66+
// #.............................#
67+
// #.............................#
68+
// #.............................#
69+
// #..............#..............#
70+
// #.............###.............#
71+
// #............#####............#
72+
// #...........#######...........#
73+
// #..........#########..........#
74+
// #............#####............#
75+
// #...........#######...........#
76+
// #..........#########..........#
77+
// #.........###########.........#
78+
// #........#############........#
79+
// #..........#########..........#
80+
// #.........###########.........#
81+
// #........#############........#
82+
// #.......###############.......#
83+
// #......#################......#
84+
// #........#############........#
85+
// #.......###############.......#
86+
// #......#################......#
87+
// #.....###################.....#
88+
// #....#####################....#
89+
// #.............###.............#
90+
// #.............###.............#
91+
// #.............###.............#
92+
// #.............................#
93+
// #.............................#
94+
// #.............................#
95+
// #.............................#
96+
// ###############################
97+
let neighboor_prob_score = robots.compute_neighboor_prob_score();
98+
(neighboor_prob_score, robots.clone(), i)
99+
})
100+
.collect();
101+
list.sort_by(|a, b| b.0.cmp(&a.0));
63102

64-
robots.move_once();
65-
let quadrants = robots.compute_quadrants();
66-
if quadrants.is_christmas_tree(robots.width, robots.height) {
67-
break;
68-
}
69-
}
103+
let candidate = list.first().unwrap();
104+
info!("iteration {}:\n{}", candidate.0, candidate.1);
70105

71-
println!("{iterations}");
106+
println!("{}", candidate.2);
72107
Ok(())
73108
}
74109

@@ -137,6 +172,23 @@ impl Deref for Robots {
137172
}
138173
}
139174

175+
impl Display for Robots {
176+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177+
let positions: HashSet<(u32, u32)> = HashSet::from_iter(self.robots.iter().map(|r| r.position));
178+
for y in 0..self.height {
179+
for x in 0..self.width {
180+
if positions.contains(&(x, y)) {
181+
write!(f, "#")?;
182+
} else {
183+
write!(f, ".")?;
184+
}
185+
}
186+
writeln!(f)?;
187+
}
188+
Ok(())
189+
}
190+
}
191+
140192
#[derive(Debug, Default)]
141193
struct Quadrants(
142194
HashMap<(u32, u32), usize>,
@@ -154,18 +206,6 @@ impl Quadrants {
154206
self.3.values().sum(),
155207
)
156208
}
157-
158-
fn compute_uniques(&self) -> (usize, usize, usize, usize) {
159-
(self.0.len(), self.1.len(), self.2.len(), self.3.len())
160-
}
161-
162-
fn is_christmas_tree(&self, width: u32, height: u32) -> bool {
163-
let center = (width / 2, height / 2);
164-
let size = center.0 * center.1;
165-
let (q1, q2, q3, q4) = self.compute_uniques();
166-
// TODO
167-
false
168-
}
169209
}
170210

171211
impl Robots {
@@ -193,4 +233,23 @@ impl Robots {
193233
acc
194234
})
195235
}
236+
237+
fn compute_neighboor_prob_score(&self) -> u32 {
238+
let positions: HashSet<(u32, u32)> = HashSet::from_iter(self.robots.iter().map(|r| r.position));
239+
240+
let directions = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)];
241+
self.robots
242+
.iter()
243+
.flat_map(|r| {
244+
directions.iter().map(|d| {
245+
let pos = (r.position.0 as i32 + d.0, r.position.1 as i32 + d.1);
246+
if pos.0 >= 0 && pos.1 >= 0 && positions.contains(&(pos.0 as u32, pos.1 as u32)) {
247+
1
248+
} else {
249+
0
250+
}
251+
})
252+
})
253+
.sum()
254+
}
196255
}

0 commit comments

Comments
 (0)