Skip to content

Commit 80775db

Browse files
committed
8th day
1 parent 7a5ec01 commit 80775db

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

data/examples/08.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

src/bin/08.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
advent_of_code::solution!(8);
2+
3+
use advent_of_code::maneatingape::hash::*;
4+
use advent_of_code::maneatingape::point::*;
5+
6+
struct MapSize {
7+
min_x: i32,
8+
max_x: i32,
9+
min_y: i32,
10+
max_y: i32,
11+
}
12+
13+
struct DataType {
14+
data: FastMap<u8, Vec<Point>>,
15+
map_size: MapSize,
16+
}
17+
18+
impl MapSize {
19+
fn contains(&self, point: &Point) -> bool {
20+
point.x >= self.min_x
21+
&& point.x <= self.max_x
22+
&& point.y >= self.min_y
23+
&& point.y <= self.max_y
24+
}
25+
}
26+
27+
fn parse_data(input: &str) -> DataType {
28+
let mut data = FastMap::new();
29+
30+
let width = input.lines().next().unwrap().len() as i32;
31+
let height = input.lines().count() as i32;
32+
33+
for (y, line) in input.lines().enumerate() {
34+
for (x, c) in line.bytes().enumerate() {
35+
if c != b'.' {
36+
(*data.entry(c).or_insert(vec![])).push(Point::new(x as i32, y as i32));
37+
}
38+
}
39+
}
40+
41+
DataType {
42+
data: data,
43+
map_size: MapSize {
44+
min_x: 0,
45+
max_x: width - 1,
46+
min_y: 0,
47+
max_y: height - 1,
48+
},
49+
}
50+
}
51+
52+
fn part_x(data: DataType, unlimited: bool) -> u32 {
53+
let DataType { data, map_size } = data;
54+
55+
let mut antinodes = FastSet::new();
56+
for (_, v) in data {
57+
for i in 0..v.len() {
58+
for j in 0..v.len() {
59+
if i == j {
60+
continue;
61+
}
62+
63+
let p1 = v[i];
64+
let p2 = v[j];
65+
66+
let diff = p2 - p1;
67+
if unlimited {
68+
let mut new_point = p2;
69+
while map_size.contains(&new_point) {
70+
antinodes.insert(new_point);
71+
new_point += diff;
72+
}
73+
} else {
74+
let new_point = p2 + diff;
75+
if map_size.contains(&new_point) {
76+
antinodes.insert(new_point);
77+
}
78+
}
79+
}
80+
}
81+
}
82+
83+
antinodes.len() as u32
84+
}
85+
86+
pub fn part_one(input: &str) -> Option<u32> {
87+
let data = parse_data(input);
88+
89+
let result = part_x(data, false);
90+
91+
Some(result)
92+
}
93+
94+
pub fn part_two(input: &str) -> Option<u32> {
95+
let data = parse_data(input);
96+
97+
let result = part_x(data, true);
98+
99+
Some(result)
100+
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
106+
#[test]
107+
fn test_part_one() {
108+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
109+
assert_eq!(result, Some(14));
110+
}
111+
112+
#[test]
113+
fn test_part_two() {
114+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
115+
assert_eq!(result, Some(34));
116+
}
117+
}

0 commit comments

Comments
 (0)