Skip to content

Commit 8c0588b

Browse files
committed
AoC 2024 Day 14 - rust
1 parent 442c704 commit 8c0588b

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
1111
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | [](src/main/python/AoC2024_06.py) | [](src/main/python/AoC2024_07.py) | [](src/main/python/AoC2024_08.py) | [](src/main/python/AoC2024_09.py) | [](src/main/python/AoC2024_10.py) | [](src/main/python/AoC2024_11.py) | [](src/main/python/AoC2024_12.py) | [](src/main/python/AoC2024_13.py) | [](src/main/python/AoC2024_14.py) | | | | | | | | | | | |
1212
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | [](src/main/java/AoC2024_08.java) | | [](src/main/java/AoC2024_10.java) | [](src/main/java/AoC2024_11.java) | [](src/main/java/AoC2024_12.java) | | [](src/main/java/AoC2024_14.java) | | | | | | | | | | | |
13-
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | | | | [](src/main/rust/AoC2024_13/src/main.rs) | | | | | | | | | | | | |
13+
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | [](src/main/rust/AoC2024_08/src/main.rs) | | | | | [](src/main/rust/AoC2024_13/src/main.rs) | [](src/main/rust/AoC2024_14/src/main.rs) | | | | | | | | | | | |
1414
<!-- @END:ImplementationsTable:2024@ -->
1515

1616
## 2023
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "AoC2024_14"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../aoc" }
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#![allow(non_snake_case)]
2+
3+
use aoc::geometry::{Translate, XY};
4+
use aoc::Puzzle;
5+
6+
const W: usize = 101;
7+
const H: usize = 103;
8+
type InputType = Vec<(XY, XY)>;
9+
10+
struct AoC2024_14;
11+
12+
impl AoC2024_14 {
13+
fn do_move(
14+
&self,
15+
robots: &InputType,
16+
w: usize,
17+
h: usize,
18+
rounds: usize,
19+
) -> usize {
20+
let moved_robots: Vec<XY> = robots
21+
.iter()
22+
.map(|(pos, vec)| pos.translate(vec, rounds as i32))
23+
.collect();
24+
let mut q: [usize; 4] = [0, 0, 0, 0];
25+
let (mid_x, mid_y) = (w / 2, h / 2);
26+
for pos in moved_robots {
27+
let (px, py) = (
28+
pos.x().rem_euclid(w as i32) as usize,
29+
pos.y().rem_euclid(h as i32) as usize,
30+
);
31+
if px < mid_x {
32+
if py < mid_y {
33+
q[0] += 1;
34+
} else if mid_y < py && py < h {
35+
q[1] += 1;
36+
}
37+
} else if mid_x < px && px < w {
38+
if py < mid_y {
39+
q[2] += 1;
40+
} else if mid_y < py && py < h {
41+
q[3] += 1;
42+
}
43+
}
44+
}
45+
q.iter().product()
46+
}
47+
48+
fn solve_1(&self, input: &InputType, w: usize, h: usize) -> usize {
49+
self.do_move(input, w, h, 100)
50+
}
51+
52+
fn sample_part_1(&self, input: &InputType) -> usize {
53+
self.solve_1(input, 11, 7)
54+
}
55+
}
56+
57+
impl aoc::Puzzle for AoC2024_14 {
58+
type Input = InputType;
59+
type Output1 = usize;
60+
type Output2 = usize;
61+
62+
aoc::puzzle_year_day!(2024, 14);
63+
64+
fn parse_input(&self, lines: Vec<String>) -> Self::Input {
65+
lines
66+
.iter()
67+
.map(|line| {
68+
let (p, v) = line.split_once(" ").unwrap();
69+
let (px, py) =
70+
p.split_once("=").unwrap().1.split_once(",").unwrap();
71+
let (vx, vy) =
72+
v.split_once("=").unwrap().1.split_once(",").unwrap();
73+
(
74+
XY::of(
75+
px.parse::<i32>().unwrap(),
76+
py.parse::<i32>().unwrap(),
77+
),
78+
XY::of(
79+
vx.parse::<i32>().unwrap(),
80+
vy.parse::<i32>().unwrap(),
81+
),
82+
)
83+
})
84+
.collect()
85+
}
86+
87+
fn part_1(&self, robots: &Self::Input) -> Self::Output1 {
88+
self.solve_1(robots, W, H)
89+
}
90+
91+
fn part_2(&self, robots: &Self::Input) -> Self::Output2 {
92+
let mut ans = 0;
93+
let mut best = usize::MAX;
94+
for round in 1..=W * H {
95+
let safety_factor = self.do_move(robots, W, H, round);
96+
if safety_factor < best {
97+
best = safety_factor;
98+
ans = round;
99+
}
100+
}
101+
ans
102+
}
103+
104+
fn samples(&self) {
105+
aoc::puzzle_samples! {
106+
self, sample_part_1, TEST, 12
107+
};
108+
}
109+
}
110+
111+
fn main() {
112+
AoC2024_14 {}.run(std::env::args());
113+
}
114+
115+
const TEST: &str = "\
116+
p=0,4 v=3,-3
117+
p=6,3 v=-1,-3
118+
p=10,3 v=-1,2
119+
p=2,0 v=2,-1
120+
p=0,0 v=1,3
121+
p=3,0 v=-2,-2
122+
p=7,6 v=-1,-3
123+
p=3,0 v=-1,-2
124+
p=9,3 v=2,3
125+
p=7,3 v=-1,2
126+
p=2,4 v=2,-3
127+
p=9,5 v=-3,-3
128+
";
129+
130+
#[cfg(test)]
131+
mod tests {
132+
use super::*;
133+
134+
#[test]
135+
pub fn samples() {
136+
AoC2024_14 {}.samples();
137+
}
138+
}

src/main/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)