Skip to content

Commit 81755d5

Browse files
committed
Year 2024 Day 14
1 parent 13b206d commit 81755d5

File tree

7 files changed

+90
-4
lines changed

7 files changed

+90
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8585
| 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [Source](src/year2024/day11.rs) | 248 |
8686
| 12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [Source](src/year2024/day12.rs) | 289 |
8787
| 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [Source](src/year2024/day13.rs) | 14 |
88+
| 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [Source](src/year2024/day14.rs) | 5830 |
8889

8990
## 2023
9091

benches/benchmark.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,6 @@ benchmark!(year2023
8787
);
8888

8989
benchmark!(year2024
90-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
90+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
91+
day14
9192
);

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@ library!(year2023 "Restore global snow production."
6767
);
6868

6969
library!(year2024 "Locate the Chief Historian in time for the big Christmas sleigh launch."
70-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
70+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
71+
day14
7172
);

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,6 @@ run!(year2023
139139
);
140140

141141
run!(year2024
142-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
142+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
143+
day14
143144
);

src/year2024/day14.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! # Restroom Redoubt
2+
//!
3+
//! For part one we jump straight to the final position by multiplying the velocity by 100.
4+
//! The image appears in part two when the positions of all robots is unqiue.
5+
use crate::util::grid::*;
6+
use crate::util::iter::*;
7+
use crate::util::parse::*;
8+
use crate::util::point::*;
9+
10+
type Robot = (Point, Point);
11+
12+
pub fn parse(input: &str) -> Vec<Robot> {
13+
input
14+
.iter_signed::<i32>()
15+
.chunk::<4>()
16+
.map(|[x, y, dx, dy]| (Point::new(x, y), Point::new(dx, dy)))
17+
.collect()
18+
}
19+
20+
pub fn part1(input: &[Robot]) -> u32 {
21+
let mut q1 = 0;
22+
let mut q2 = 0;
23+
let mut q3 = 0;
24+
let mut q4 = 0;
25+
26+
for (p, v) in input {
27+
let x = (p.x + 100 * v.x).rem_euclid(101);
28+
let y = (p.y + 100 * v.y).rem_euclid(103);
29+
30+
if x < 50 {
31+
if y < 51 {
32+
q1 += 1;
33+
}
34+
if y > 51 {
35+
q2 += 1;
36+
}
37+
}
38+
if x > 50 {
39+
if y < 51 {
40+
q3 += 1;
41+
}
42+
if y > 51 {
43+
q4 += 1;
44+
}
45+
}
46+
}
47+
48+
q1 * q2 * q3 * q4
49+
}
50+
51+
pub fn part2(input: &[Robot]) -> u32 {
52+
let mut robots = input.to_vec();
53+
let mut grid = Grid::new(101, 103, 0);
54+
let mut seconds = 0;
55+
let mut overlap = true;
56+
57+
while overlap {
58+
overlap = false;
59+
60+
for (p, v) in &mut robots {
61+
let next = Point::new((p.x + v.x).rem_euclid(101), (p.y + v.y).rem_euclid(103));
62+
*p = next;
63+
64+
overlap |= grid[next] == seconds;
65+
grid[next] = seconds;
66+
}
67+
68+
seconds += 1;
69+
}
70+
71+
seconds
72+
}

tests/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,6 @@ test!(year2023
8080
);
8181

8282
test!(year2024
83-
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13
83+
day01, day02, day03, day04, day05, day06, day07, day08, day09, day10, day11, day12, day13,
84+
day14
8485
);

tests/year2024/day14.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[test]
2+
fn part1_test() {
3+
// No test
4+
}
5+
6+
#[test]
7+
fn part2_test() {
8+
// No test
9+
}

0 commit comments

Comments
 (0)