-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rs
80 lines (65 loc) · 1.71 KB
/
day18.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
//! [Day 18: Like a Rogue](https://adventofcode.com/2016/day/18)
struct Puzzle {
tiles: Vec<u32>,
}
impl Puzzle {
fn new(data: &str) -> Self {
Self {
tiles: data.trim_ascii().chars().map(|c| u32::from(c == '.')).collect(),
}
}
fn guess(tiles: &mut [u32]) {
let prev = tiles.to_vec();
for i in 0..prev.len() {
let left = if i == 0 { 1 } else { prev[i - 1] };
let center = prev[i];
let right = if i == prev.len() - 1 { 1 } else { prev[i + 1] };
tiles[i] = match (left, center, right) {
// (0, 0, 1) | (1, 0, 0) | (0, 1, 1) | (1, 1, 0) => 0,
(0, 0 | 1, 1) | (1, 0 | 1, 0) => 0,
_ => 1,
};
}
}
fn solve(&self, rows: usize) -> u32 {
let mut result = self.tiles.iter().sum::<u32>();
let mut tiles = self.tiles.clone();
for _ in 1..rows {
Self::guess(&mut tiles);
result += tiles.iter().sum::<u32>();
}
result
}
/// Solve part one.
fn part1(&self) -> u32 {
self.solve(40)
}
/// Solve part two.
fn part2(&self) -> u32 {
self.solve(400_000)
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u32, u32) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let puzzle = Puzzle::new("..^^.");
assert_eq!(puzzle.solve(3), 6);
}
#[test]
fn test02() {
let puzzle = Puzzle::new(".^^.^.^^^^");
assert_eq!(puzzle.solve(10), 38);
}
}