-
Notifications
You must be signed in to change notification settings - Fork 7
/
24.rs
33 lines (28 loc) · 1.02 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
use std::{collections::HashSet, io::stdin};
use regex::Regex;
fn adjacent((x, y): &(i32, i32)) -> impl Iterator<Item = (i32, i32)> {
[(x-2, *y), (x+2, *y), (x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)].into_iter()
}
fn main() {
let pat = Regex::new("e|ne|nw|w|sw|se").unwrap();
let mut tiles = HashSet::new();
for line in stdin().lines().filter_map(Result::ok) {
let (mut x, mut y) = (0, 0);
for dir in pat.find_iter(&line).map(|m| m.as_str()) {
x += (dir.contains('e') as i32 * 4 - 2) / dir.len() as i32;
y += dir.contains('n') as i32 - dir.contains('s') as i32;
}
if !tiles.remove(&(x, y)) {
tiles.insert((x, y));
}
}
println!("{}", tiles.len());
for _ in 0..100 {
tiles = tiles.iter()
.flat_map(adjacent)
.filter(|t| (1 + !tiles.contains(t) as usize..=2)
.contains(&adjacent(t).filter(|a| tiles.contains(a)).count()))
.collect();
}
println!("{}", tiles.len());
}