-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
148 lines (134 loc) · 3.67 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
use std::collections::HashSet;
type Tile = (isize, isize);
#[derive(Debug, Clone)]
enum Dir {
East,
West,
SouthEast,
SouthWest,
NorthWest,
NorthEast,
}
fn parse_directions(line: &str) -> Vec<Dir> {
let mut result = vec![];
let mut it = line.chars();
loop {
match it.next() {
Some(c) => {
let dir = match c {
'e' => Dir::East,
'w' => Dir::West,
's' => match it.next().unwrap() {
'w' => Dir::SouthWest,
'e' => Dir::SouthEast,
_ => panic!(),
},
'n' => match it.next().unwrap() {
'w' => Dir::NorthWest,
'e' => Dir::NorthEast,
_ => panic!(),
},
_ => panic!(),
};
result.push(dir);
}
None => break,
}
}
result
}
fn move_dir(pos: Tile, dir: Dir) -> Tile {
use Dir::*;
let (x, y) = pos;
match dir {
East => (x + 1, y),
West => (x - 1, y),
SouthEast => (x, y + 1),
SouthWest => (x - 1, y + 1),
NorthEast => (x + 1, y - 1),
NorthWest => (x, y - 1),
}
}
fn move_path(pos: Tile, dirs: Vec<Dir>) -> Tile {
dirs.iter().fold(pos, |pos, dir| move_dir(pos, dir.clone()))
}
fn part1(input: &str) -> usize {
let mut black: HashSet<Tile> = HashSet::new();
for line in input.lines() {
let dirs = parse_directions(line);
let pos = move_path((0, 0), dirs);
if black.contains(&pos) {
&black.remove(&pos);
} else {
&black.insert(pos);
}
}
black.len()
}
fn neighbors(pos: Tile) -> Vec<Tile> {
use Dir::*;
vec![East, West, SouthEast, SouthWest, NorthWest, NorthEast]
.iter()
.map(|dir| move_dir(pos, dir.clone()))
.collect::<Vec<Tile>>()
}
fn part2(input: &str) -> usize {
let mut black: HashSet<Tile> = HashSet::new();
for line in input.lines() {
let pos = move_path((0, 0), parse_directions(line));
if black.contains(&pos) {
&black.remove(&pos);
} else {
&black.insert(pos);
}
}
for _ in 0..100 {
let flip_to_white = black
.iter()
.cloned()
.filter(|tile| {
let black_neighbors = neighbors(tile.clone())
.iter()
.filter(|n| *&black.contains(n))
.count();
black_neighbors == 0 || black_neighbors > 2
})
.collect::<Vec<Tile>>();
let flip_to_black = black
.iter()
.cloned()
.flat_map(|b| neighbors(b))
.filter(|p| !black.contains(p))
.filter(|p| {
neighbors(p.clone())
.iter()
.filter(|n| black.contains(n))
.count()
== 2
})
.collect::<Vec<Tile>>();
for t in flip_to_white.iter() {
black.remove(t);
}
for t in flip_to_black.iter() {
black.insert(t.clone());
}
}
black.len()
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_move_path() {
assert_eq!(move_path((0, 0), parse_directions("nwwswee")), (0, 0));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test.txt")), 10);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test.txt")), 2208);
}