-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
190 lines (175 loc) · 5 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
mod intcode;
use intcode::IntcodeComputer;
use itertools::Itertools;
use std::{collections::HashMap, slice::Iter};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Square {
Start,
Wall,
Empty,
OxygenSystem,
}
#[derive(Debug, Clone, Copy)]
enum Direction {
North,
East,
South,
West,
}
use Direction::*;
impl Direction {
fn iter() -> Iter<'static, Direction> {
static DIRS: [Direction; 4] = [North, East, South, West];
DIRS.iter()
}
fn opposite(&self) -> Direction {
match &self {
North => South,
South => North,
East => West,
West => East,
}
}
}
fn move_coords((x, y): (i64, i64), dir: Direction) -> (i64, i64) {
match dir {
North => (x, y + 1),
South => (x, y - 1),
West => (x - 1, y),
East => (x + 1, y),
}
}
#[derive(Debug, Clone, Copy)]
enum Status {
HitWall,
Moved(Square),
}
impl Status {
fn parse(code: i64) -> Self {
match code {
0 => Status::HitWall,
1 => Status::Moved(Square::Empty),
2 => Status::Moved(Square::OxygenSystem),
_ => panic!(),
}
}
}
fn step(computer: &mut IntcodeComputer, dir: Direction) -> Status {
computer.push_input(match dir {
North => 1,
South => 2,
West => 3,
East => 4,
});
Status::parse(computer.run_until_next_output().unwrap())
}
#[allow(unused)]
fn print_map(map: &HashMap<(i64, i64), Square>) {
let (x0, x1) = map.keys().map(|(x, _)| x).minmax().into_option().unwrap();
let (y0, y1) = map.keys().map(|(_, y)| y).minmax().into_option().unwrap();
for y in *y0..=*y1 {
for x in *x0..=*x1 {
print!(
"{}",
match map.get(&(x, y)) {
None => ' ',
Some(&Square::Start) => 'X',
Some(&Square::Empty) => '.',
Some(&Square::Wall) => '█',
Some(&Square::OxygenSystem) => 'O',
}
)
}
println!("");
}
}
fn crawl(
computer: &mut IntcodeComputer,
coords: (i64, i64),
map: &mut HashMap<(i64, i64), Square>,
) {
for dir in Direction::iter() {
let target_coords = move_coords(coords, *dir);
if map.contains_key(&target_coords) {
continue;
}
match step(computer, *dir) {
Status::HitWall => {
map.insert(target_coords, Square::Wall);
}
Status::Moved(square) => {
map.insert(target_coords, square);
let mut fork = computer.clone();
crawl(&mut fork, target_coords, map);
step(computer, dir.opposite());
}
}
}
}
fn solve_maze(
coords: (i64, i64),
last: Option<(i64, i64)>,
map: &HashMap<(i64, i64), Square>,
) -> Option<usize> {
Direction::iter()
.filter_map(|dir| {
let target_coords = move_coords(coords, *dir);
if last != Some(target_coords) {
match map.get(&target_coords).unwrap() {
&Square::Empty | &Square::Start => {
solve_maze(target_coords, Some(coords), map).map(|v| v + 1)
}
&Square::OxygenSystem => Some(1),
&Square::Wall => None,
}
} else {
None
}
})
.min()
}
fn part1(input: &str) -> usize {
let mut computer = IntcodeComputer::from_str(input);
let mut map: HashMap<(i64, i64), Square> = HashMap::new();
map.insert((0, 0), Square::Start);
crawl(&mut computer, (0, 0), &mut map);
solve_maze((0, 0), None, &map).unwrap()
}
fn oxygen_spread_time(coords: (i64, i64), map: &mut HashMap<(i64, i64), Square>) -> usize {
Direction::iter()
.filter_map(|dir| {
let target_coords = move_coords(coords, *dir);
match map.get(&target_coords).unwrap() {
&Square::Empty | &Square::Start => {
map.insert(target_coords, Square::OxygenSystem);
Some(1 + oxygen_spread_time(target_coords, map))
}
&Square::Wall | &Square::OxygenSystem => None,
}
})
.max()
.unwrap_or(0)
}
fn part2(input: &str) -> usize {
let mut computer = IntcodeComputer::from_str(input);
let mut map: HashMap<(i64, i64), Square> = HashMap::new();
map.insert((0, 0), Square::Start);
crawl(&mut computer, (0, 0), &mut map);
let (start_coords, _) = map
.iter()
.find(|(_k, v)| v == &&Square::OxygenSystem)
.unwrap();
oxygen_spread_time(*start_coords, &mut map)
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("in.txt")), 266);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("in.txt")), 274);
}