This repository was archived by the owner on Aug 1, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.rs
144 lines (111 loc) · 3.76 KB
/
21.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::collections::{VecDeque, HashSet};
advent_of_code::solution!(21);
#[derive(PartialEq, Eq, Clone, Copy)]
enum Tile {
Rock,
Garden,
}
type Map = Vec<Vec<Tile>>;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct Coord {
x: isize,
y: isize
}
impl Coord {
fn neighbours(&self, map: &Map) -> Vec<Coord> {
let mut neighs: Vec<Coord> = vec![];
// No need to check for boundaries in part 1 because
// they are not reachable in 64 steps.
// For part 2 there are no boundaries.
neighs.push(Coord { x: self.x - 1, y: self.y });
neighs.push(Coord { x: self.x + 1, y: self.y });
neighs.push(Coord { x: self.x, y: self.y - 1 });
neighs.push(Coord { x: self.x, y: self.y + 1 });
neighs.into_iter()
.filter(|n| n.is_garden(map))
.collect()
}
fn is_garden(&self, map: &Map) -> bool {
match self.get_infinite(map) {
Tile::Garden => true,
Tile::Rock => false,
}
}
fn get_infinite(&self, map: &Map) -> Tile {
let (width, height) = (map[0].len() as isize, map.len() as isize);
map[self.y.rem_euclid(height) as usize][self.x.rem_euclid(width) as usize]
}
}
fn parse(input: &str) -> (Coord, Map) {
let mut starting: Coord = Coord { x: 0, y: 0 };
let map = input.lines()
.enumerate()
.map(|(row, l)| l.chars()
.enumerate()
.map(|(col, c)| match c {
'.' => Tile::Garden,
'#' => Tile::Rock,
'S' => { starting = Coord { x: col as isize, y: row as isize }; Tile::Garden },
_ => panic!("Invalid char")
})
.collect())
.collect();
(starting, map)
}
fn bfs(map: &Map, start: Coord, steps: usize) -> usize {
let mut queue: VecDeque<(Coord, usize)> = VecDeque::new();
queue.push_back((start, steps));
let mut seen: HashSet<Coord> = HashSet::new();
let mut answer: Vec<Coord> = vec![];
while let Some((current_coord, remaining)) = queue.pop_front() {
if remaining % 2 == 0 {
answer.push(current_coord);
}
if remaining == 0 {
continue;
}
for n in current_coord.neighbours(map) {
if seen.insert(n) && n.get_infinite(map) != Tile::Rock {
queue.push_back((n, remaining - 1));
}
}
}
answer.len() - if steps % 2 != 0 { 0 } else { 1 }
}
fn the_part_one(input: &str, steps: usize) -> Option<u32> {
let (starting_point, map) = parse(input);
let coords = bfs(&map, starting_point, steps);
Some(coords as u32)
}
pub fn part_one(input: &str) -> Option<u32> {
the_part_one(input, 64)
}
pub fn part_one_test(input: &str) -> Option<u32> {
the_part_one(input, 6)
}
pub fn part_two(input: &str) -> Option<u64> {
let (starting_point, map) = parse(input);
let coords1 = bfs(&map, starting_point, 65);
let coords2 = bfs(&map, starting_point, 65+131);
let coords3 = bfs(&map, starting_point, 65+131*2);
let c = coords1;
let b = (4 * coords2 - 3 * coords1 - coords3) / 2;
let a = coords2 - coords1 - b;
let x = (26501365 - map.len() / 2) / map.len();
let result = a * x.pow(2) + b * x + c;
Some(result as u64)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one_test(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(16));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}