Skip to content

Commit ba5e9e7

Browse files
committed
add day8
1 parent 9b47c23 commit ba5e9e7

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

2024/day8/.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/day8/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "day8"
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+
glam = { version = "0.29.2", features = ["debug-glam-assert"] }

2024/day8/src/main.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use glam::Vec2;
2+
use pathfinding::matrix::Matrix;
3+
use std::collections::HashSet;
4+
5+
fn part1(input: &str) -> usize {
6+
let m = Matrix::from_rows(input.lines().map(str::chars)).unwrap();
7+
let mut set = HashSet::new();
8+
for (apos, c) in m.items() {
9+
if *c == '.' {
10+
continue;
11+
}
12+
for (bpos, cb) in m.items() {
13+
if c != cb || apos == bpos {
14+
continue;
15+
}
16+
let avec = Vec2::new(apos.1 as f32, apos.0 as f32);
17+
let bvec = Vec2::new(bpos.1 as f32, bpos.0 as f32);
18+
let anewvec = avec.lerp(bvec, 2.);
19+
let bnewvec = bvec.lerp(avec, 2.);
20+
let anew = (anewvec.y as usize, anewvec.x as usize);
21+
let bnew = (bnewvec.y as usize, bnewvec.x as usize);
22+
if anewvec.x >= 0. && anewvec.y >= 0. && m.within_bounds(anew) {
23+
set.insert(anew);
24+
}
25+
if bnewvec.x >= 0. && bnewvec.y >= 0. && m.within_bounds(bnew) {
26+
set.insert(bnew);
27+
}
28+
}
29+
}
30+
set.len()
31+
}
32+
fn part2(input: &str) -> usize {
33+
let m = Matrix::from_rows(input.lines().map(str::chars)).unwrap();
34+
let mut set = HashSet::new();
35+
for (apos, c) in m.items() {
36+
if *c == '.' {
37+
continue;
38+
}
39+
for (bpos, cb) in m.items().skip(apos.0 + apos.1) {
40+
if c != cb || apos == bpos {
41+
continue;
42+
}
43+
let avec = Vec2::new(apos.1 as f32, apos.0 as f32);
44+
let bvec = Vec2::new(bpos.1 as f32, bpos.0 as f32);
45+
let mut factor = 1.;
46+
loop {
47+
let mut out = [false; 2];
48+
let anewvec = avec + (bvec - avec) * factor;
49+
let bnewvec = bvec + (avec - bvec) * factor;
50+
let anew = (anewvec.y as usize, anewvec.x as usize);
51+
let bnew = (bnewvec.y as usize, bnewvec.x as usize);
52+
if anewvec.x >= 0. && anewvec.y >= 0. && m.within_bounds(anew) {
53+
set.insert(anew);
54+
} else {
55+
out[0] = true;
56+
}
57+
if bnewvec.x >= 0. && bnewvec.y >= 0. && m.within_bounds(bnew) {
58+
set.insert(bnew);
59+
} else {
60+
out[1] = true;
61+
}
62+
if out[0] && out[1] {
63+
break;
64+
}
65+
factor += 1.;
66+
}
67+
}
68+
}
69+
set.len()
70+
}
71+
72+
fn main() {
73+
let input = include_str!("../input.txt");
74+
println!("Part 1: {}", part1(input));
75+
println!("Part 2: {}", part2(input));
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
const INPUT: &str = "............
81+
........0...
82+
.....0......
83+
.......0....
84+
....0.......
85+
......A.....
86+
............
87+
............
88+
........A...
89+
.........A..
90+
............
91+
............";
92+
#[test]
93+
fn part1() {
94+
assert_eq!(super::part1(INPUT), 14);
95+
}
96+
#[test]
97+
fn part2() {
98+
assert_eq!(super::part2(INPUT), 34);
99+
}
100+
}

0 commit comments

Comments
 (0)