-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.rs
151 lines (137 loc) · 4.18 KB
/
24.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
use std::collections::HashSet;
fn main() {
let input = include_str!("../../input/24.txt");
println!("Part 1: {}", run(input, false));
println!("Part 2: {}", run(input, true));
}
fn find_tile(input: &str, x: i64, y: i64) -> (usize, usize) {
let (x, y, _) = input.chars().fold((x, y, None), |(x, y, prev), c| match c {
'n' => (x, y, Some('n')),
's' => (x, y, Some('s')),
'e' => match prev {
Some('n') => (x, y + 1, None),
Some('s') => (x - 1, y, None),
Some(_) => (x, y, None),
None => (x - 1, y + 1, None),
},
'w' => match prev {
Some('n') => (x + 1, y, None),
Some('s') => (x, y - 1, None),
Some(_) => (x, y, None),
None => (x + 1, y - 1, None),
},
_ => (x, y, prev),
});
(x as usize, y as usize)
}
struct Tile {
black: bool,
neighbors: u8,
timestamp: u8,
}
fn flip_100(input: &str) -> i64 {
const GRID_SIZE: i64 = 150;
let mut tiles: Vec<Vec<_>> = (0..GRID_SIZE)
.map(|_| {
(0..GRID_SIZE)
.map(|_| Tile {
black: false,
neighbors: 0,
timestamp: u8::MAX,
})
.collect()
})
.collect();
let mut black = HashSet::new();
for line in input.lines() {
let (x, y) = find_tile(line, GRID_SIZE / 2, GRID_SIZE / 2);
if !black.insert((x, y)) {
black.remove(&(x, y));
}
tiles[x][y].black = !tiles[x][y].black;
}
let mut black: Vec<_> = black.into_iter().collect();
let mut touched = Vec::new();
let neighbors = vec![(0, 1), (0, -1), (1, 0), (-1, 0), (1, -1), (-1, 1)];
for i in 0..100 {
for &(x, y) in &black {
for (dx, dy) in neighbors.iter().chain([(0, 0)].iter()) {
let (x, y) = ((x as isize + dx) as usize, (y as isize + dy) as usize);
let tile = &mut tiles[x][y];
if tile.timestamp != i {
tile.timestamp = i;
tile.neighbors = 0;
touched.push((x, y));
}
}
for (dx, dy) in &neighbors {
let (x, y) = ((x as isize + dx) as usize, (y as isize + dy) as usize);
tiles[x][y].neighbors += 1;
}
}
black = touched
.drain(0..)
.filter(|&(x, y)| {
let tile = &mut tiles[x][y];
tile.black = if tile.black {
tile.neighbors == 1 || tile.neighbors == 2
} else {
tile.neighbors == 2
};
tile.black
})
.collect();
}
black.len() as i64
}
fn flip_tiles(input: &str) -> i64 {
let mut black = HashSet::new();
for line in input.lines() {
let cooridinates = find_tile(line, 0, 0);
if !black.insert(cooridinates) {
black.remove(&cooridinates);
}
}
black.len() as i64
}
fn run(input: &str, part_two: bool) -> i64 {
if part_two {
flip_100(input)
} else {
flip_tiles(input)
}
}
#[cfg(test)]
mod day_24_tests {
use super::*;
static INPUT: &str = "sesenwnenenewseeswwswswwnenewsewsw
neeenesenwnwwswnenewnwwsewnenwseswesw
seswneswswsenwwnwse
nwnwneseeswswnenewneswwnewseswneseene
swweswneswnenwsewnwneneseenw
eesenwseswswnenwswnwnwsewwnwsene
sewnenenenesenwsewnenwwwse
wenwwweseeeweswwwnwwe
wsweesenenewnwwnwsenewsenwwsesesenwne
neeswseenwwswnwswswnw
nenwswwsewswnenenewsenwsenwnesesenew
enewnwewneswsewnwswenweswnenwsenwsw
sweneswneswneneenwnewenewwneswswnese
swwesenesewenwneswnwwneseswwne
enesenwswwswneneswsenwnewswseenwsese
wnwnesenesenenwwnenwsewesewsesesew
nenewswnwewswnenesenwnesewesw
eneswnwswnwsenenwnwnwwseeswneewsenese
neswnwewnwnwseenwseesewsenwsweewe
wseweeenwnesenwwwswnew";
#[test]
fn test_part_1() {
assert_eq!(run(INPUT, false), 10);
assert_eq!(run(include_str!("../../input/24.txt"), false), 228);
}
#[test]
fn test_part_2() {
assert_eq!(run(INPUT, true), 2208);
assert_eq!(run(include_str!("../../input/24.txt"), true), 3672);
}
}