Skip to content

Commit 9d8eb37

Browse files
committed
Year 2024 Day 14
1 parent 13b206d commit 9d8eb37

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-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) | 1604 |
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 = [i32; 4];
11+
12+
pub fn parse(input: &str) -> Vec<Robot> {
13+
input.iter_signed().chunk::<4>() .collect()
14+
}
15+
16+
pub fn part1(input: &[Robot]) -> i32 {
17+
let mut q1 = 0;
18+
let mut q2 = 0;
19+
let mut q3 = 0;
20+
let mut q4 = 0;
21+
22+
for [x, y, dx, dy] in input {
23+
let x = (x + 100 * dx).rem_euclid(101);
24+
let y = (y + 100 * dy).rem_euclid(103);
25+
26+
if x < 50 {
27+
if y < 51 {
28+
q1 += 1;
29+
}
30+
if y > 51 {
31+
q2 += 1;
32+
}
33+
}
34+
if x > 50 {
35+
if y < 51 {
36+
q3 += 1;
37+
}
38+
if y > 51 {
39+
q4 += 1;
40+
}
41+
}
42+
}
43+
44+
q1 * q2 * q3 * q4
45+
}
46+
47+
pub fn part2(robots: &[Robot]) -> i32 {
48+
let mut grid = Grid::new(101, 103, 0);
49+
let mut time = 0;
50+
let mut overlap = true;
51+
52+
while overlap {
53+
time += 1;
54+
overlap = false;
55+
56+
for [x, y, dx, dy] in robots {
57+
let next = Point::new((x + dx * time).rem_euclid(101), (y + dy * time).rem_euclid(103));
58+
59+
if grid[next] == time {
60+
overlap = true;
61+
break;
62+
}
63+
64+
grid[next] = time;
65+
}
66+
}
67+
68+
time
69+
}

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)