Skip to content

Commit 5227351

Browse files
committed
add day14
1 parent 12c7f22 commit 5227351

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

2024/day14/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input.txt
2+
flamegraph.svg
3+
perf.data*
4+
### Rust
5+
# Generated by Cargo
6+
# will have compiled files and executables
7+
debug/
8+
target/
9+
10+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
11+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
12+
Cargo.lock
13+
14+
# These are backup files generated by rustfmt
15+
**/*.rs.bk
16+
17+
# MSVC Windows builds of rustc generate these, which store debugging information
18+
*.pdb
19+

2024/day14/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "day14"
3+
authors = ["mirsella <[email protected]>"]
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
regex = { workspace = true }
11+
itertools = { workspace = true }
12+
pathfinding = { workspace = true }
13+
rayon = { workspace = true }
14+
indexmap = { workspace = true }
15+
hashbrown = { workspace = true }
16+
fix_fn = { workspace = true }

2024/day14/src/main.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use itertools::Itertools;
2+
3+
fn part1(input: &str, wide: usize, high: usize) -> usize {
4+
let wide = wide as i16;
5+
let high = high as i16;
6+
input
7+
.lines()
8+
.map(|l| {
9+
let (mut x, mut y, vx, vy) = l
10+
.split(['p', '=', ',', ' ', 'v'])
11+
.filter(|s| !s.is_empty())
12+
.map(|s| s.parse::<i16>().unwrap())
13+
.collect_tuple()
14+
.unwrap();
15+
(0..100).for_each(|_| {
16+
x = (x + vx).rem_euclid(wide);
17+
y = (y + vy).rem_euclid(high);
18+
});
19+
let mut quadran = 0;
20+
if x > wide / 2 {
21+
quadran += 1;
22+
}
23+
if y > high / 2 {
24+
quadran += 2;
25+
}
26+
if x == wide / 2 || y == high / 2 {
27+
quadran += 4;
28+
}
29+
quadran
30+
})
31+
.fold([0usize; 4], |mut acc, id| {
32+
if id < 4 {
33+
acc[id] += 1;
34+
}
35+
acc
36+
})
37+
.into_iter()
38+
.product()
39+
}
40+
41+
fn part2(input: &str, wide: usize, high: usize) -> usize {
42+
let wide = wide as i16;
43+
let high = high as i16;
44+
let mut robots: Vec<(i16, i16, i16, i16)> = {
45+
input
46+
.lines()
47+
.map(|l| {
48+
l.split(['p', '=', ',', ' ', 'v'])
49+
.filter(|s| !s.is_empty())
50+
.map(|s| s.parse::<i16>().unwrap())
51+
.collect_tuple()
52+
.unwrap()
53+
})
54+
.collect()
55+
};
56+
let mut min_safety_factor = None;
57+
for sec in 1..wide * high {
58+
let safety_factor = robots
59+
.iter_mut()
60+
.map(|(x, y, vx, vy)| {
61+
*x = (*x + *vx).rem_euclid(wide);
62+
*y = (*y + *vy).rem_euclid(high);
63+
64+
let mut quadran = 0;
65+
if *x > wide / 2 {
66+
quadran += 1;
67+
}
68+
if *y > high / 2 {
69+
quadran += 2;
70+
}
71+
if *x == wide / 2 || *y == high / 2 {
72+
quadran += 4;
73+
}
74+
quadran
75+
})
76+
.fold([0usize; 4], |mut acc, id| {
77+
if id < 4 {
78+
acc[id] += 1;
79+
}
80+
acc
81+
})
82+
.into_iter()
83+
.product::<usize>();
84+
if let Some((_, mfactor)) = min_safety_factor {
85+
if safety_factor < mfactor {
86+
min_safety_factor = Some((sec, safety_factor));
87+
}
88+
} else {
89+
min_safety_factor = Some((sec, safety_factor));
90+
};
91+
}
92+
min_safety_factor.unwrap().0 as usize
93+
}
94+
95+
fn main() {
96+
let input = include_str!("../input.txt");
97+
println!("Part 1: {}", part1(input, 101, 103));
98+
println!("Part 2: {}", part2(input, 101, 103));
99+
}
100+
101+
#[cfg(test)]
102+
mod tests {
103+
const INPUT: &str = "p=0,4 v=3,-3
104+
p=6,3 v=-1,-3
105+
p=10,3 v=-1,2
106+
p=2,0 v=2,-1
107+
p=0,0 v=1,3
108+
p=3,0 v=-2,-2
109+
p=7,6 v=-1,-3
110+
p=3,0 v=-1,-2
111+
p=9,3 v=2,3
112+
p=7,3 v=-1,2
113+
p=2,4 v=2,-3
114+
p=9,5 v=-3,-3";
115+
#[test]
116+
fn part1() {
117+
assert_eq!(super::part1(INPUT, 11, 7), 12);
118+
}
119+
}

0 commit comments

Comments
 (0)