day 24
This commit is contained in:
parent
904e3ac703
commit
ded004093c
347
data/day24/input
Normal file
347
data/day24/input
Normal file
@ -0,0 +1,347 @@
|
||||
enwwsenweswweseseswnenewwswseswenw
|
||||
nwnwneswwnenwnwnwenenwwnesenenwsenenee
|
||||
eneswwenwneeswneenenw
|
||||
swwswswwwswswsweeswww
|
||||
swewnwneeewnewwwswwnwwswswnwww
|
||||
seswnwenesenwsenenwwenwsenwswnwseswnw
|
||||
nwswwenwwwswwnwnewswnwnwwwswnee
|
||||
seswseswseseseseeseseseswnwsesw
|
||||
swswseswseswswnweneswswseswse
|
||||
wenwswwswwwswwnwsesewwenwseswwne
|
||||
nenewswsenenenenenwnenesewswnenenewe
|
||||
eeneneswswnenwnenenwnwnwnwnwnwswenwnesw
|
||||
nwsweneeeeweenenwneeseeesenesw
|
||||
nwnewsesesesesesesewsweseenwsesesese
|
||||
nenwneenenwneneneswsenenenenewnenwnenewse
|
||||
wswnwseseneswswswenewswneswenw
|
||||
wwswsewswswwwnewnwwswswewsewwnw
|
||||
nwnwnwsenenwnwnwneenwwnwnwswwnwnwenwnw
|
||||
seenweeswenwwseeeeeesweesese
|
||||
neswwewnewwwnewseswwwwwnwseswnew
|
||||
swseseesweenwswnwswswnwsesewseswene
|
||||
nwsenenenwsenenenesenewneswwnenenenesenene
|
||||
ewnenwneswsenenwnwsenewsenwwnenwnesesene
|
||||
sesenenwswsesesweseseswnwsesenesenewe
|
||||
wwenwnewewswwswwswwsewwenww
|
||||
neweeseseswswwweenwnenwweeneenew
|
||||
neseneseneneneneneswnenwswnewnenenenene
|
||||
wswwwnwewwwnwneswswsewewwweww
|
||||
swseseswwnwswswswnwnew
|
||||
eseseesewnwseseseewseneseesesenwsw
|
||||
swsweswswnesenwswsesw
|
||||
seeeswnenweenewseeesweeenenewsw
|
||||
swnwsewesewnenwswneweneswseenwsew
|
||||
wsesweswnwswswnesweswwwswswswswsww
|
||||
wseenwnwswneewnwswsweenenwnwswneew
|
||||
seswnwseeeseseseseseswswneswseesenwsew
|
||||
nwnwnenwnwwnwsenwenwneneneswnwnwnw
|
||||
swseeseswswnwswsesesenwsesenwswnwnw
|
||||
nwsewnwwnwwewwneswnwenenwseneswsw
|
||||
wwsewswenwwwnweenwnwnenwswswwnw
|
||||
wneeneeneeeeseenesewneneeswee
|
||||
wnwenwswneneenwswnenesenwnenwwneswnwse
|
||||
sewsenwseswsewewswneswnwneseseswseee
|
||||
wnwwwswnwnwnwwsenwneww
|
||||
swswnwweseeswneswswswswswnwswswswswswne
|
||||
neeneneeeneeeswenwnenenwswneneswe
|
||||
wneeseeeseeseswsenwweeeeseee
|
||||
nenweesenesenewwwsweswnwnwweswne
|
||||
senewseswsewswneseesenwsenenwsesenwsw
|
||||
ewenwnwseeeenwesenwnewswwswe
|
||||
nwnwnesesewnwnwnewneenwnwnwnwnwswnwne
|
||||
enwwewnwnesenenenwnwnenenweneseswwswne
|
||||
nwswnenwswnwwnwseswnwnwewnwwwnenew
|
||||
wweeswweneseewnewwwewwewse
|
||||
swseseswswseseneseswneswseswneswseswnwsw
|
||||
wnwnwnesenwwnwnwnwwsewnwswnenwwnw
|
||||
sewsenwswsenewneneewnwswwnesw
|
||||
wswseneeswsenwsesenwwneswnenwewsesesene
|
||||
nwnwnwwnwnwnwenwsenwwnwenwnww
|
||||
esenweeswseseenwe
|
||||
nwnenwwnwnwneweswswwnwsweesewsew
|
||||
swwswseeseseneseseeswswwswnesesenwwswsw
|
||||
seseesewseseeeswnwseseenwseesesenwsese
|
||||
wwnwwwswwwwwnewww
|
||||
swseenewseneneeeswswnwswsenwswenwsene
|
||||
swwswwseewnwnenwwnewwnenwsewwnw
|
||||
nwnwenwswnwnwnenwnwwnenwsenenenwnwwsenw
|
||||
nwenwwnwnwnwnenwnwneswnwnwnwseenwnwnw
|
||||
seswnenenewwneeneneesesewenwenwse
|
||||
nenenenewwnenesee
|
||||
senwwwwenwwnwwwnesenwnwsenwnwnwnw
|
||||
senwneswwewwseswwnwswesewnenwwwww
|
||||
nenenenenenwneneewneeneswneswsewene
|
||||
swseneswswwswswnwswnewswseseseswswswswswne
|
||||
wwenwewnwwnwnwnwwwwswneswsewnww
|
||||
swnwnwwswswseswsenweneseswswswwswsee
|
||||
wnwswsenwwwswsweswwswswswewwww
|
||||
neeeswneeeeeeeeneeneeswwenesw
|
||||
newwwnewwwnwnwsewnwswsew
|
||||
enwswnwnesenewwneenenwnesw
|
||||
eneewwneswnwneeneeswe
|
||||
seseesesesesesesesenwsesenwesewswnenew
|
||||
wseswswseswnesweswswswsw
|
||||
seeeeeenweeeeweseenenenewwe
|
||||
nwsenewsenweswsewwnwenwwenwse
|
||||
nwwnwnwwnenwwswsewsenenwnwnwewwse
|
||||
eeneeweeeeeenee
|
||||
seswneneewswswswswswswswswneswwwwsw
|
||||
senwnewnwnwenwnwnwnwnwnwnwnwnw
|
||||
wwsenwnwwenwwwneewwnwswseneswe
|
||||
nenenenenenwnenenesenewneneewnwseewsw
|
||||
wsenwswswnwnwnwnwnenwwnwnwwnwneeenw
|
||||
neeenwsewsesweeeenweneswenenwswwe
|
||||
nwwnwenwwnwnenweswwenwesenwnwwnw
|
||||
seneswnwwswwswwswwwwwnewswneswe
|
||||
eeneeweweeeenee
|
||||
wneseseweneseeewsesenesenwwwwnwse
|
||||
swwnweseswneesesenewseseeswswnwwswne
|
||||
swnwsenwsewseseseswnwseneseswswswswene
|
||||
swwseswswseseneseswseswseswswneneswnwsw
|
||||
sesesenweswswsweswsewseseseseswsw
|
||||
seswnwewwwsewnwwnenwwnwwnwnwnwnw
|
||||
enweewseeseenweneseseseenweeee
|
||||
wwwwwwswwwwneswswsww
|
||||
sweswnwnwsewswsewnwswwwwneseneesw
|
||||
nwseswnwnenwsewnwenw
|
||||
nwnesenewnesweenenenwneneneneenewsene
|
||||
swswnwwswswseswenwswswwswswe
|
||||
nenwnenwnwnwnwwnwenenwswnesewnenwnwe
|
||||
swseeseswweswswswseneswswswswswswww
|
||||
neswnwnenenwnwneneneneneeswnwnenenwneesw
|
||||
nenwnwesewnwsenenenwnwswwnwnwnesene
|
||||
seeeseswewnwneseseenwweeeseneesw
|
||||
swswswnenwswnwneswseswesewnwseswsenwswsw
|
||||
senesewseseseseseseeswesenwsewsenesese
|
||||
nwnwnwwnwnwnwnwsenwnewnwnwww
|
||||
neswneseeseneswwwnewswwwwwwswwsww
|
||||
nweesesenwesesesenweeeseseeseswse
|
||||
neneeneswneswneenesenwwnwsenwneswwnene
|
||||
enewewswnwnweesweswseeeeeenwe
|
||||
eswseeseneeseseseseeee
|
||||
sesesewsesewseesenwsesesenwswesenweswse
|
||||
nwnwswnesweenwswneseeseswneewnenenenwsw
|
||||
nwseswswswseeewswswwenwne
|
||||
swneewswnwswswswseswwseeeswswswneswwsw
|
||||
eneneeseneewneneenenesenwnewnenene
|
||||
nwsweseswswswswswswswsw
|
||||
nwnenwnwwwnwseneswnwenwnwswswwweswnee
|
||||
nenwnwwnwwesenwseswenenwwnesewnww
|
||||
sesenwseeweeenweseeeeeenwesee
|
||||
eeswwesewseseeeesewnw
|
||||
nwenwnwenwnwnwnwswnwwnwewnwnwswswnw
|
||||
neweswnwsweneneweneeneneswnenesenwsw
|
||||
seseseswswsesenesewsesese
|
||||
seeeeeewesenwenenwewsenwseee
|
||||
eneneweewwneeneneesene
|
||||
swnwnweeswswsweesenwsewseswswnwswsewse
|
||||
neneseneswneneneneswneneswneneneenenwnee
|
||||
nenewnenewwneneneneneeseneneneseneswse
|
||||
nwswsweeeseeewwnwswnwswnwne
|
||||
newwnwsewswwwnewwwsewwwewww
|
||||
wwwwwnwwwwswwenwnwnwnwwesweew
|
||||
nwneneneseneeeneswswneweseeeneene
|
||||
enesesenwnwseseewneswswseesweseseene
|
||||
wwnwenewwwnwnwnwsenwnwwwnenwnwsw
|
||||
swwwewwweww
|
||||
wnewwwseeesewsenwnwnwnwswnwewwnw
|
||||
eswnwnesenwnwnwseseswnwswseseewswsew
|
||||
swneneseswnenwnenwenenweswneenwnenwne
|
||||
wwnewnewsewsww
|
||||
nwwenwnwneseseewwwnesenwswswswnenw
|
||||
seenwnwswwsenwseswwsewneswseseesee
|
||||
eseeseeswseeseeesenenewese
|
||||
eseseseseeseeseseswesesenenwnewesw
|
||||
neeswnwswsweeewnweesweswnewnwene
|
||||
newwnwnwneswwwenwwwsewwwsenww
|
||||
nenwswnwnwnwswnwneseewswnwnwnee
|
||||
sesewswwswwsewswnwnwnew
|
||||
sweswswnwnwnweswswsesw
|
||||
senesenweneneswsewnenwnenenenenenwenee
|
||||
enwwnwswnwneenwnwnwnwswnwnwseswnw
|
||||
newswnwnwswewneneneneswwwseneneesene
|
||||
seswsesesewswneseseseseewswswsenwsese
|
||||
newnwnenwnenenwsenwseneneneswnwnenwnene
|
||||
nwnwnenwnwnwnwnwswnwnwnwwwewnweswnw
|
||||
nwnenwnwnenenwnwwsenenenwnweswnene
|
||||
nwwnenwnenenesenewseneneneswnwenwnwnenw
|
||||
nwnwnwnenwnwwnwsenewnenwsenwnwnwnenwnwsw
|
||||
swsenwswswswswneswswswsweswswswsewswnw
|
||||
wwswwswsweswnwswwnwewwsewwwew
|
||||
sewwnenwnwswwwnwwewnwwnwnenwneswnw
|
||||
ewneeenweneeseeeswenw
|
||||
nwnwneeswneseewneneneseeeeee
|
||||
wsweswewswwnewswnwwwnwnwswwesewe
|
||||
enwnwnwsenwnwnenwneswnwsenenwneswnwnwnw
|
||||
nenesenewnenwswnewwsesewswsenewsww
|
||||
wnenenenenenenenenenenenesenwnesesewnene
|
||||
wswnwwwwnwnwewnwsew
|
||||
nesenwwswseenwseswnesesesenweseseseese
|
||||
nwewnenenwswsenwnewnwneneesenwnwwnwnwne
|
||||
nwnwnwwnwneneswenenwnwnwsenwneswneeswne
|
||||
seseseseeeseswnwsenwswneseeeewsese
|
||||
sewnenweswneeswenesenwenwwswwneswnw
|
||||
wsesesesewsesesesesesesenwseseeeew
|
||||
seseeeesesesweewewwesewwnew
|
||||
nwnwswswenwnenenenwnwnenwnwweneswnenwe
|
||||
neeneneenwneneneneesweswneseneneew
|
||||
weewwwwwnewwwwwsewewswnw
|
||||
swseseenweeeeeeee
|
||||
seseseeseseseneseseeswseee
|
||||
nwewnwneenenenwnwnwwswsewnwsenenwne
|
||||
nenwnesenewnenenenenenenenesenenw
|
||||
seeesweenwsesese
|
||||
nenwnenwsesenenenenenwwnene
|
||||
eseneseswnwsewesesenwseseeeeswsese
|
||||
swseseesweseneswneesesesenwsee
|
||||
swnenewwswwwwwnwwewnewswwsesw
|
||||
eswnwsesenwsesenwenwnesesesweswwnwnenw
|
||||
nesenwneseseswswseseswswseweswneswswsese
|
||||
eseswsewnwnewenwseneeeseenesw
|
||||
eneeeneeneweneewneneeneewsee
|
||||
sewesesewseswseseseseseswnesenwneseswsw
|
||||
eneesenwneeseenenwwseneeneeee
|
||||
neweneswnenweeneeseeneewnesenenwe
|
||||
wsesewswwwwnwenweswnwneneswnww
|
||||
eswnwenwwweswwsewswswnwsenwwsww
|
||||
wnewnwwsenenwnwnwnwnesenenwsesewnene
|
||||
seswnwwwseneswnenewwnwesesewne
|
||||
eenwesweweeneeeeweeee
|
||||
neneenewswwseenenwnenenwnwnesenenenene
|
||||
nwsenwnwsesenwseseseeseswseseseseseswsese
|
||||
sesenwseswewswswsenweswnwswswseenesese
|
||||
swsewswsenwswwnwsweenwnwseseseeswwe
|
||||
seeseswswneseseswwsesenwswsesesenesese
|
||||
sweneswnenwswswseswseswswseewnwnenwesw
|
||||
wswseswwnewwwswswswswsewswswnwwenw
|
||||
seneenweswnwswneeseseeswsesenwesesee
|
||||
wwenwsewwwwwww
|
||||
swseswswwseseseseeswsw
|
||||
wswneneswswnwwseswwwswswewwsewesw
|
||||
esesweeeesenenenwnwnew
|
||||
sesesenweseseeseseseeesee
|
||||
esewseseseseswsesenesewsewswsesesee
|
||||
swwswwwswwnwwswwsenewneswswsenwwe
|
||||
seseswsesesesesewsesenesenesesenwseswsenw
|
||||
senwwwwenwseneswseneewnwnwenewww
|
||||
seswsenwsweseswseseneeseeseenwseww
|
||||
seseswwneneswswwwswnwneswswswsewnwswwsw
|
||||
eweewneeenewwseewewseene
|
||||
neeneswnenenenenenene
|
||||
wnenwwwwnenwswwwswsenwwenwweww
|
||||
wnenwnenenesweeneneenenenwneenwsese
|
||||
neewsenenesesenweseseswsweswne
|
||||
neneeneneeeeneesw
|
||||
eseneswseenwswneesweseeseeesenwnwee
|
||||
wwsesenwneswneseseneswnwseseneseswsene
|
||||
wewsenwewwwnwwwwwwwwesene
|
||||
wsewnwnwwseeswwsw
|
||||
swwswwswswswwswsweswwsw
|
||||
eeeseeneswnwseseneseenwesweswnwswswne
|
||||
eseswwsenenesesewsesesesesese
|
||||
eseeswesesesesewseenwsesewnwsesesese
|
||||
newwwwwsewswnwnwneseswwnwswwwwne
|
||||
nwesenwnwwswwweweewwwneswwsw
|
||||
eeeeeeeeeweese
|
||||
eeneewweswweeneenwseseseeswnenw
|
||||
swewswwwwnwsweswswswneewseswwww
|
||||
swwswwsweswwswwnesw
|
||||
seeseseneesewseseswnwswsesweseneseese
|
||||
swswswewneseseseseseswnwswnesesesenwse
|
||||
weseeeeeeeee
|
||||
swswsewseseeswsesw
|
||||
seseseswnweneswsenenenwwwswswsenweeese
|
||||
swnenwneswenwnwsenwenenenenenenewswnene
|
||||
nenewwwwwswweswewewnwwsewnwse
|
||||
senwewswswnwenwswseneeswswse
|
||||
eseeeweweeeeenwsweneeeneseee
|
||||
enwseeeseeneeweseeseeeeswenwe
|
||||
nwnwsenwnwnwnwsenwnw
|
||||
swswwswswweewwwsww
|
||||
seeeeeeewsee
|
||||
ewwwnwwnwwsenesewneswswswswswseew
|
||||
nesenwwnwnwseswswnwneswnwnwnwnwnwwnenw
|
||||
nenwsweseswseewneswwseeenwnenese
|
||||
wswswswswswswswwswnese
|
||||
swnenwswswnwswnesenwnwseewseneseenwnene
|
||||
nwsesenesenwnwswesenwnenenesewwnwnwnw
|
||||
nenwswswwewnwseswwwswneewnewese
|
||||
enenenenewwneneesesenenewenenenesew
|
||||
nwnwsenwsenwnenwnesenenenwwesenwwsenesww
|
||||
nwsenwswswsesesenesewswsesesesesesenene
|
||||
eeneweenwenwenwsesesweseeswsenw
|
||||
seseseswswswswswnwwesweswneswnw
|
||||
seseswsesesenwseswswsee
|
||||
nwwnwnewwwwwwwewsewnwswnww
|
||||
seswnewswnwseneseneswseseeswswsenesewse
|
||||
sewswneswnwswswswswneweswswswswswswswsw
|
||||
wenesenenenwnwnwnew
|
||||
nenwnwnwnwnwwweswwenwnwnwnwswnw
|
||||
seneneswneneneswnwsenwnesenw
|
||||
wswswnwsweswnwsesewsenwnenewsewsene
|
||||
eswnwseeseewseenwe
|
||||
neenwsweswnenewsenwnwswne
|
||||
wnwewnwwnwswwnwwseewsenwnesenwnw
|
||||
nwneneenewsesweseneeneneewsenwnenene
|
||||
sesewsesenewswseseenwseneswswsesesese
|
||||
wwwsewwnwwnesenwwnwnesewwwnwnww
|
||||
seseseseswnewseswnewne
|
||||
neswsweneeneneeneswenwnenenewneenene
|
||||
wsesesesesesesenwseeseseeseseneswwne
|
||||
swwenwnwnwenwsenwwwne
|
||||
swnenenenenenewnenenwneesenwswnenenenene
|
||||
wswneseneswenewswswnwswneewenwweswe
|
||||
nenewseenwsenesenwswwsewwnwewsewnw
|
||||
wseeseeweneswsenweseneesenenwswwsw
|
||||
neswswneswsewwswwsewsweeneswwnwsw
|
||||
sesenwswnwseswseeswswseswneseseseswwswsw
|
||||
seswneseneswwnwnwnesenwseenw
|
||||
eesesewsewnwnenenenwnweseswwwnee
|
||||
wneenewnenenwneneseneesenesenwwwsenese
|
||||
seseseseseeweseswsesewseseesesenwe
|
||||
nwenwwenwnwnwwnenwnwnwsenw
|
||||
eseeeneeseneswewseseeesesenweesw
|
||||
swsewswswnwsewnene
|
||||
nwnenwneswnwnwnenenenwnwnenenw
|
||||
eeeseeewseeeseeesee
|
||||
swnenwswnwnwnenwnenwnwnwnwnwenwnwenwnesw
|
||||
eeseseeswwneeeesesesesesenewsee
|
||||
swnenenenesenenwnesenenwnenesenenenenewne
|
||||
neewwwwewwesewwswwnwwwne
|
||||
nwsewnwwswwnwwwwswneneswneswnwnwnew
|
||||
wswswseeneswwwwsweswswswwnenwesw
|
||||
nwswewnwsenwswnenweewnwnwenwsw
|
||||
seeenwweseeeeenenweeesenw
|
||||
swnwwswswswswwswswneswswneesw
|
||||
sweseneneswwswswewswwwnenwseneswnese
|
||||
eneenwewseeeeesenwseeeseseswsee
|
||||
nwnewnwnwsenwnwwneesenwnwnenewnw
|
||||
newswswwswwsewsewwnene
|
||||
eneseswenewwneseneneneneswneneneswnene
|
||||
nweneeseneeeesesweeneeenwewnee
|
||||
seswsewnesenenwnenwswwwwnweweswnw
|
||||
nwneseneswswseseseswnewneswseseseesene
|
||||
eeewseswweswenwesenwsewswswnenwne
|
||||
nwnwnwnwwnewnwswnwnwsenwnwnwnewenwnw
|
||||
seseswswsweswwseswswsweswwsesesenwnesw
|
||||
neeswnwswswswseswswwneswswswseseswswsesw
|
||||
swswseeneswswswwswswswnwnwseswenwew
|
||||
swswswseswsweswswewswnwseseswswswnwnee
|
||||
nwwwnwnenwneneeeenwswnenw
|
||||
senenwswnwneewsewswwweneeeeesesw
|
||||
nwswenwsweseneesesewnwwnwseweenenese
|
||||
swesenwseeneseeseswswseswseeseenwnwse
|
||||
wneseweweneneswsenwnenenenenwnenwnew
|
||||
swswswswswswswswnwswwwwswse
|
||||
neeseseenwsweneneeesenwweewwew
|
||||
swswswsweeswewnwwewsweswnenwswswnw
|
||||
enwwneneswnenenwnwnenenenwwneenenenese
|
||||
nwwwenwwenwwwnwnwnwnwseswnwwnww
|
||||
sweswwwsweweswswweswwnwswswnenwese
|
||||
neseseseseseseseseseseseswsese
|
||||
nwsesenwneseseseseswseese
|
||||
swneswnwwwwenwwwe
|
||||
nwnwnwnwnewnwwwnwnwnenenwsenwsesenwnww
|
||||
nwwswneseseswsenweswswswneswwswwswswsw
|
||||
neenwsenenenenenenwswswnenenwsenenene
|
||||
newnwnwnwnwwnweswwewsewenwwnwnwnwnw
|
140
src/bin/day24.rs
Normal file
140
src/bin/day24.rs
Normal file
@ -0,0 +1,140 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
const INPUT: &str = include_str!("../../data/day24/input");
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
enum Direction {
|
||||
East,
|
||||
SouthEast,
|
||||
SouthWest,
|
||||
West,
|
||||
NorthWest,
|
||||
NorthEast,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
const ALL: [Direction; 6] = [
|
||||
Self::East,
|
||||
Self::SouthEast,
|
||||
Self::SouthWest,
|
||||
Self::West,
|
||||
Self::NorthWest,
|
||||
Self::NorthEast,
|
||||
];
|
||||
|
||||
fn symbol(self) -> &'static str {
|
||||
match self {
|
||||
Self::East => "e",
|
||||
Self::SouthEast => "se",
|
||||
Self::SouthWest => "sw",
|
||||
Self::West => "w",
|
||||
Self::NorthWest => "nw",
|
||||
Self::NorthEast => "ne",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_list(line: &str) -> Vec<Self> {
|
||||
let mut line = line.trim();
|
||||
let mut result = Vec::new();
|
||||
'outer: while !line.is_empty() {
|
||||
for &d in Self::ALL.iter() {
|
||||
if let Some(rem) = line.strip_prefix(d.symbol()) {
|
||||
result.push(d);
|
||||
line = rem;
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
panic!("Invalid directions: {:?}", line);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
// -> (row, col)
|
||||
fn as_offset(self) -> (i32, i32) {
|
||||
match self {
|
||||
Self::East => (0, 1),
|
||||
Self::SouthEast => (1, 1),
|
||||
Self::SouthWest => (1, 0),
|
||||
Self::West => (0, -1),
|
||||
Self::NorthWest => (-1, -1),
|
||||
Self::NorthEast => (-1, 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default, Debug)]
|
||||
struct Position {
|
||||
row: i32,
|
||||
// the "0"-column goes from south-west to north-east through (0/0)
|
||||
col: i32,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
fn adjacent(self) -> impl Iterator<Item=Self> {
|
||||
Direction::ALL.iter().map(move |&d| self + d)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<Direction> for Position {
|
||||
type Output = Position;
|
||||
|
||||
fn add(self, rhs: Direction) -> Self::Output {
|
||||
let (row, col) = rhs.as_offset();
|
||||
Self {
|
||||
row: self.row + row,
|
||||
col: self.col + col,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn daily_flip(black_tiles: HashSet<Position>) -> HashSet<Position> {
|
||||
let mut new_black_tiles: HashSet<Position> = HashSet::new();
|
||||
let mut dont_flip_to_black: HashSet<Position> = HashSet::new();
|
||||
for &tile in &black_tiles {
|
||||
let mut count_adj_black = 0;
|
||||
for adj_tile in tile.adjacent() {
|
||||
if black_tiles.contains(&adj_tile) {
|
||||
count_adj_black += 1;
|
||||
} else if !new_black_tiles.contains(&adj_tile) && !dont_flip_to_black.contains(&adj_tile) {
|
||||
// white adjacent: maybe needs to be flipped to black
|
||||
let mut white_count_adj_black = 0;
|
||||
for adj_tile_2 in adj_tile.adjacent() {
|
||||
if black_tiles.contains(&adj_tile_2) {
|
||||
white_count_adj_black += 1;
|
||||
if white_count_adj_black > 2 { break; }
|
||||
}
|
||||
}
|
||||
if white_count_adj_black == 2 {
|
||||
new_black_tiles.insert(adj_tile);
|
||||
} else {
|
||||
// don't check again
|
||||
dont_flip_to_black.insert(adj_tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
if count_adj_black == 1 || count_adj_black == 2 {
|
||||
new_black_tiles.insert(tile);
|
||||
}
|
||||
}
|
||||
new_black_tiles
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let directions = INPUT.lines().map(Direction::parse_list).collect::<Vec<_>>();
|
||||
let flip_tiles = directions.iter().map(|tile| {
|
||||
tile.iter().fold(Position::default(), |tile, &dir| tile + dir)
|
||||
}).collect::<Vec<_>>();
|
||||
let mut black_tiles = HashSet::new();
|
||||
for &tile in &flip_tiles {
|
||||
if black_tiles.contains(&tile) {
|
||||
black_tiles.remove(&tile);
|
||||
} else {
|
||||
black_tiles.insert(tile);
|
||||
}
|
||||
}
|
||||
println!("Black tiles: {}", black_tiles.len());
|
||||
for _ in 0..100 {
|
||||
black_tiles = daily_flip(black_tiles);
|
||||
}
|
||||
println!("Black tiles: {}", black_tiles.len());
|
||||
}
|
Loading…
Reference in New Issue
Block a user