-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.rs
161 lines (131 loc) · 3.73 KB
/
day24.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
//! [Day 24: Lobby Layout](https://adventofcode.com/2020/day/24)
use aoc::Coord;
use rustc_hash::{FxHashMap, FxHashSet};
struct Walk(FxHashMap<Coord, bool>);
impl Walk {
fn new() -> Self {
Self(FxHashMap::default())
}
fn tiles(&self) -> FxHashSet<Coord> {
self.0
.iter()
.filter(|(_, v)| **v)
.map(|(k, _)| k)
.copied()
.collect()
}
fn walk(&mut self, line: &str) {
let mut pos = Coord::ZERO;
let mut it = line.chars();
while let Some(ch) = it.next() {
pos += match ch {
'w' => Coord::WEST * 2,
'e' => Coord::EAST * 2,
'n' => match it.next().unwrap() {
'w' => Coord::NORTH_WEST,
'e' => Coord::NORTH_EAST,
_ => panic!(),
},
's' => match it.next().unwrap() {
'w' => Coord::SOUTH_WEST,
'e' => Coord::SOUTH_EAST,
_ => panic!(),
},
_ => break,
};
}
*self.0.entry(pos).or_default() ^= true;
}
}
#[derive(Debug)]
struct Puzzle {
tiles: FxHashSet<Coord>,
}
impl Puzzle {
/// Initialize from the puzzle input.
fn new(data: &str) -> Self {
let mut walk = Walk::new();
for line in data.lines() {
walk.walk(line);
}
Self {
tiles: walk.tiles(),
}
}
/// Solve part one.
fn part1(&self) -> usize {
self.tiles.len()
}
/// Solve part two.
fn part2(&self) -> usize {
let mut tiles = self.tiles.clone();
let neighbors = [
Coord::ZERO,
Coord::NORTH_EAST,
Coord::EAST * 2,
Coord::SOUTH_EAST,
Coord::NORTH_WEST,
Coord::WEST * 2,
Coord::SOUTH_WEST,
];
// Hexagonal Conway's game of life
for _ in 0..100 {
let mut new_tiles = FxHashSet::default();
let mut seen = FxHashSet::default();
// for every visited tiles
for &black in &tiles {
for dxy in neighbors {
let xy = black + dxy;
// but only once
if !seen.insert(xy) {
continue;
}
let is_black = tiles.contains(&xy);
let black_neighbors = neighbors[1..]
.iter()
.filter(|&&nxy| tiles.contains(&(xy + nxy)))
.count();
// evolve rule
let new_is_black = if is_black && (black_neighbors == 0 || black_neighbors > 2)
{
false
} else if !is_black && black_neighbors == 2 {
true
} else {
is_black
};
if new_is_black {
new_tiles.insert(xy);
}
}
}
tiles = new_tiles;
}
tiles.len()
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (usize, usize) {
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::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn part1() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part1(), 10);
}
#[test]
fn part2() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part2(), 2208);
}
}