Skip to main content

advent_of_code/year2020/
day24.rs

1use crate::input::Input;
2use std::collections::{HashMap, HashSet};
3
4/// Using double-width coordinates - see <https://www.redblobgames.com/grids/hexagons//>
5pub fn solve(input: &Input) -> Result<u64, String> {
6    let mut black_tiles = HashSet::new();
7
8    for line_str in input.text.lines() {
9        let mut location = (0_i32, 0_i32);
10        let mut string_position = 0;
11        let line = line_str.as_bytes();
12        while string_position < line.len() {
13            let first_char = line[string_position];
14            let diff = match first_char {
15                b'e' => (2, 0),
16                b'w' => (-2, 0),
17                b's' | b'n' => {
18                    string_position += 1;
19                    match (first_char, line.get(string_position)) {
20                        (b'n', Some(b'e')) => (1, 1),
21                        (b'n', Some(b'w')) => (-1, 1),
22                        (b's', Some(b'e')) => (1, -1),
23                        (b's', Some(b'w')) => (-1, -1),
24                        _ => {
25                            return Err("Invalid input".to_string());
26                        }
27                    }
28                }
29                _ => {
30                    return Err("Invalid input".to_string());
31                }
32            };
33
34            location = (location.0 + diff.0, location.1 + diff.1);
35
36            string_position += 1;
37        }
38
39        if !black_tiles.insert(location) {
40            black_tiles.remove(&location);
41        }
42    }
43
44    if input.is_part_two() {
45        for _day in 1..=100 {
46            let mut adjacent_blacks_count = HashMap::new();
47            let mut new_black_tiles = black_tiles.clone();
48
49            for &black_tile in black_tiles.iter() {
50                for diff in [(2, 0), (1, -1), (-1, -1), (-2, 0), (-1, 1), (1, 1)] {
51                    let adjacent_location = (black_tile.0 + diff.0, black_tile.1 + diff.1);
52                    *adjacent_blacks_count.entry(adjacent_location).or_insert(0) += 1;
53                }
54            }
55
56            for location in black_tiles.iter() {
57                if !adjacent_blacks_count.contains_key(location) {
58                    // "Any black tile with zero or more than 2 black tiles immediately
59                    // adjacent to it is flipped to white."
60                    // We only do the first check here.
61                    new_black_tiles.remove(location);
62                }
63            }
64
65            for (&location, &adjacent_blacks) in adjacent_blacks_count.iter() {
66                let is_black = black_tiles.contains(&location);
67                if is_black && adjacent_blacks > 2 {
68                    // "Any black tile with zero or more than 2 black tiles immediately
69                    // adjacent to it is flipped to white."
70                    // We only do the second check here.
71                    new_black_tiles.remove(&location);
72                } else if !is_black && adjacent_blacks == 2 {
73                    // "Any white tile with exactly 2 black tiles immediately adjacent
74                    // to it is flipped to black."
75                    new_black_tiles.insert(location);
76                }
77            }
78
79            std::mem::swap(&mut black_tiles, &mut new_black_tiles);
80        }
81    }
82
83    Ok(black_tiles.len() as u64)
84}
85
86#[test]
87pub fn tests() {
88    test_part_one!("esew" => 1);
89    test_part_one!("esew\nesew" => 0);
90    test_part_one!("esew\nnwwswee" => 2);
91
92    let example = "sesenwnenenewseeswwswswwnenewsewsw
93neeenesenwnwwswnenewnwwsewnenwseswesw
94seswneswswsenwwnwse
95nwnwneseeswswnenewneswwnewseswneseene
96swweswneswnenwsewnwneneseenw
97eesenwseswswnenwswnwnwsewwnwsene
98sewnenenenesenwsewnenwwwse
99wenwwweseeeweswwwnwwe
100wsweesenenewnwwnwsenewsenwwsesesenwne
101neeswseenwwswnwswswnw
102nenwswwsewswnenenewsenwsenwnesesenew
103enewnwewneswsewnwswenweswnenwsenwsw
104sweneswneswneneenwnewenewwneswswnese
105swwesenesewenwneswnwwneseswwne
106enesenwswwswneneswsenwnewswseenwsese
107wnwnesenesenenwwnenwsewesewsesesew
108nenewswnwewswnenesenwnesewesw
109eneswnwswnwsenenwnwnwwseeswneewsenese
110neswnwewnwnwseenwseesewsenwsweewe
111wseweeenwnesenwwwswnew";
112    test_part_one!(example => 10);
113    test_part_two!(example => 2208);
114
115    let real_input = include_str!("day24_input.txt");
116    test_part_one!(real_input => 549);
117    test_part_two!(real_input => 4147);
118}