-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
173 lines (159 loc) · 4.85 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
#![feature(test)]
extern crate test;
use anyhow::{anyhow, Result};
use common::point::point;
use common::point::Point;
use common::Answer;
use std::collections::VecDeque;
use std::i128;
use std::{collections::HashSet, io};
pub fn main() -> Result<()> {
let stdin = io::read_to_string(io::stdin())?;
println!("part1: {}", part_one(&stdin, 1024, (71, 71))?);
println!("part2: {}", part_two(&stdin, point!(71, 71))?);
Ok(())
}
pub fn part_one(input: &str, falling: usize, grid_size: (i128, i128)) -> Result<Answer> {
let input = parse_input(input)?;
solve_one(&input, falling, grid_size)
}
pub fn part_two(input: &str, grid_size: Point) -> Result<Answer> {
let input = parse_input(input)?;
solve_two(&input, grid_size)
}
struct Input {
falling_bytes: Vec<Point>,
}
fn parse_input(input: &str) -> Result<Input> {
let falling_bytes = input
.trim()
.lines()
.map(|l| parse_point(l))
.collect::<Result<Vec<_>, _>>()?;
Ok(Input { falling_bytes })
}
fn parse_point(line: &str) -> Result<Point> {
let (x, y) = line
.split_once(",")
.ok_or(anyhow!("not a valid point {}", line))?;
let x = i128::from_str_radix(x, 10)?;
let y = i128::from_str_radix(y, 10)?;
Ok(Point { x, y })
}
struct Grid {
obstructions: HashSet<Point>,
grid_size: Point,
}
impl Grid {
fn new(grid_size: &Point) -> Self {
Self {
obstructions: HashSet::new(),
grid_size: *grid_size,
}
}
fn get_neighbors(&self, at: &Point) -> Vec<Point> {
at.get_4_neighbors(&self.grid_size)
.into_iter()
.filter(|p| !self.obstructions.contains(p))
.collect()
}
fn add_obstruction(&mut self, at: &Point) {
self.obstructions.insert(*at);
}
fn shortest_path(&self, from: &Point, to: &Point) -> Option<i128> {
let mut visited = HashSet::new();
let mut queue = VecDeque::new();
queue.push_back((*from, 0));
visited.insert(*from);
while let Some((p, cost)) = queue.pop_front() {
if p == *to {
return Some(cost);
}
for neighbor in self.get_neighbors(&p).into_iter() {
if !visited.contains(&neighbor) {
queue.push_back((neighbor, cost + 1));
visited.insert(neighbor);
}
}
}
None
}
}
fn solve_one(input: &Input, falling: usize, grid_size: (i128, i128)) -> Result<Answer> {
let Input { falling_bytes } = input;
let mut grid = Grid::new(&point!(grid_size));
for i in 0..falling {
let byte = falling_bytes
.get(i)
.ok_or(anyhow!("no byte left to fall (at {})", i))?;
grid.add_obstruction(byte);
}
let from = point!(0, 0);
let to = point!(grid_size) - point!(1, 1);
let shortest_path = grid
.shortest_path(&from, &to)
.ok_or(anyhow!("there still must be a path"))?;
Ok(Answer::Num(shortest_path))
}
fn solve_two(input: &Input, grid_size: Point) -> Result<Answer> {
let Input { falling_bytes } = input;
let from = point!(0, 0);
let to = grid_size - point!(1, 1);
let mut grid = Grid::new(&grid_size);
let mut fall_idx = 0;
while let Some(byte) = falling_bytes.get(fall_idx) {
grid.add_obstruction(byte);
if grid.shortest_path(&from, &to).is_none() {
break;
}
fall_idx += 1;
}
let last_fallen = falling_bytes
.get(fall_idx)
.ok_or(anyhow!("path was never blocked"))?;
Ok(Answer::Str(format!("{},{}", last_fallen.x, last_fallen.y)))
}
// Quickly obtain answers by running
// cargo test one [-r]
// cargo test two [-r]
#[cfg(test)]
mod day18_tests {
use super::*;
use common::test_utils::*;
use std::sync::LazyLock;
use test::Bencher;
static TEST: LazyLock<String> = local_file!("test");
static INPUT: LazyLock<String> = local_file!("input");
#[test]
fn test_one() -> Result<()> {
let answer = super::part_one(&TEST, 12, (7, 7))?;
assert_eq!(answer, Answer::Num(22));
Ok(())
}
fn part_one_impl() -> Result<()> {
let answer = super::part_one(&INPUT, 1024, (71, 71))?;
assert_eq!(answer, Answer::Num(308));
Ok(())
}
#[bench]
fn part_one(b: &mut Bencher) {
part_one_impl().expect("Error");
b.iter(|| part_one_impl())
}
#[test]
fn test_two() -> Result<()> {
let answer = super::part_two(&TEST, point!(7, 7))?;
assert_eq!(answer, Answer::from("6,1"));
Ok(())
}
fn part_two_impl() -> Result<()> {
let answer = super::part_two(&INPUT, point!(71, 71))?;
assert_eq!(answer, Answer::from("46,28"));
Ok(())
}
#[bench]
fn part_two(b: &mut Bencher) {
part_two_impl().expect("Error");
b.iter(|| part_two_impl())
}
}