-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
148 lines (112 loc) · 4.09 KB
/
main.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
145
146
147
148
use std::collections::{HashSet, VecDeque};
use utils_rust::utils::wrapper;
const DAY: i32 = 21;
const YEAR: i32 = 2023;
#[warn(unused_variables)]
fn main() {
wrapper(part_1, YEAR, DAY, 1);
wrapper(part_2, YEAR, DAY, 2);
}
#[allow(unused_variables)]
fn part_1(input: &String) -> String {
let steps: i64;
if input.lines().count() == 11 {
steps = 6; // test input
} else {
steps = 64;
}
let (map, start) = parse(input);
distances(&map, start, steps).to_string()
}
#[allow(unused_variables)]
fn part_2(input: &String) -> String {
let (map, start) = parse(input);
let width = input.lines().next().unwrap().len() as i64;
let height = input.lines().count() as i64;
assert_eq!(width, height);
let size = width;
if size == 11 {
// ignore test input
return "0".to_string();
}
let steps = 26501365;
let grids = steps / size - 1;
assert_eq!((grids + 1) * size + size / 2, steps);
let odd_grids = (grids / 2 * 2 + 1).pow(2) as u64;
let even_grids = ((grids + 1) / 2 * 2).pow(2) as u64;
let mut reachable: u64 = distances(&map, start, size * 2 + 1) * odd_grids;
reachable += distances(&map, start, size * 2) * even_grids;
let top = distances(&map, (start.0, size - 1), size - 1);
let right = distances(&map, (0, start.1), size - 1);
let bottom = distances(&map, (start.0, 0), size - 1);
let left = distances(&map, (size - 1, start.1), size - 1);
reachable += top + right + bottom + left;
let top_right_1 = distances(&map, (0, size - 1), size / 2 - 1);
let top_left_1 = distances(&map, (size - 1, size - 1), size / 2 - 1);
let bottom_right_1 = distances(&map, (0, 0), size / 2 - 1);
let bottom_left_1 = distances(&map, (size - 1, 0), size / 2 - 1);
reachable += (top_right_1 + top_left_1 + bottom_right_1 + bottom_left_1) * (grids + 1) as u64;
let top_right_2 = distances(&map, (0, size - 1), size * 3 / 2 - 1);
let top_left_2 = distances(&map, (size - 1, size - 1), size * 3 / 2 - 1);
let bottom_right_2 = distances(&map, (0, 0), size * 3 / 2 - 1);
let bottom_left_2 = distances(&map, (size - 1, 0), size * 3 / 2 - 1);
reachable += (top_right_2 + top_left_2 + bottom_right_2 + bottom_left_2) * grids as u64;
reachable.to_string()
}
fn get_neighbors(map: &HashSet<(i64, i64)>, pos: (i64, i64)) -> HashSet<(i64, i64)> {
let mut neighbors: HashSet<(i64, i64)> = HashSet::new();
let (x, y) = pos;
if map.contains(&(x - 1, y)) {
neighbors.insert((x - 1, y));
}
if map.contains(&(x + 1, y)) {
neighbors.insert((x + 1, y));
}
if map.contains(&(x, y - 1)) {
neighbors.insert((x, y - 1));
}
if map.contains(&(x, y + 1)) {
neighbors.insert((x, y + 1));
}
neighbors
}
fn distances(map: &HashSet<(i64, i64)>, start: (i64, i64), steps: i64) -> u64 {
let mut queue: VecDeque<(i64, i64, i64)> = VecDeque::new();
let mut seen: HashSet<(i64, i64)> = HashSet::new();
seen.insert(start);
queue.push_back((start.0, start.1, steps));
let mut reachable: HashSet<(i64, i64)> = HashSet::new();
while !queue.is_empty() {
let (x, y, steps) = queue.pop_front().unwrap();
if steps < 0 {
continue;
}
if steps % 2 == 0 {
reachable.insert((x, y));
}
for neighbor in get_neighbors(&map, (x, y)) {
if !seen.contains(&neighbor) {
seen.insert(neighbor);
queue.push_back((neighbor.0, neighbor.1, steps - 1));
}
}
}
reachable.len() as u64
}
fn parse(input: &String) -> (HashSet<(i64, i64)>, (i64, i64)) {
let mut map: HashSet<(i64, i64)> = HashSet::new();
let mut start: (i64, i64) = (-1, -1);
for (y, line) in input.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
let x = x as i64;
let y = y as i64;
if c == '.' {
map.insert((x, y));
} else if c == 'S' {
map.insert((x, y));
start = (x, y);
}
}
}
(map, start)
}