Last active
December 24, 2020 08:11
-
-
Save kung-foo/bbf450639f17f48e26ac980893569300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import random | |
import re | |
import numpy as np | |
from collections import defaultdict | |
src = open("input.txt", "r").readlines() | |
src2 = """sesenwnenenewseeswwswswwnenewsewsw | |
neeenesenwnwwswnenewnwwsewnenwseswesw | |
seswneswswsenwwnwse | |
nwnwneseeswswnenewneswwnewseswneseene | |
swweswneswnenwsewnwneneseenw | |
eesenwseswswnenwswnwnwsewwnwsene | |
sewnenenenesenwsewnenwwwse | |
wenwwweseeeweswwwnwwe | |
wsweesenenewnwwnwsenewsenwwsesesenwne | |
neeswseenwwswnwswswnw | |
nenwswwsewswnenenewsenwsenwnesesenew | |
enewnwewneswsewnwswenweswnenwsenwsw | |
sweneswneswneneenwnewenewwneswswnese | |
swwesenesewenwneswnwwneseswwne | |
enesenwswwswneneswsenwnewswseenwsese | |
wnwnesenesenenwwnenwsewesewsesesew | |
nenewswnwewswnenesenwnesewesw | |
eneswnwswnwsenenwnwnwwseeswneewsenese | |
neswnwewnwnwseenwseesewsenwsweewe | |
wseweeenwnesenwwwswnew""".splitlines() | |
src = [r.strip() for r in src if r.strip()] | |
skew = 2 # 1.8 is more "hexagonal" | |
vecs = { | |
"e": np.array([2, 0]), | |
"se": np.array([1, -skew]), | |
"sw": np.array([-1, -skew]), | |
"w": np.array([-2, 0]), | |
"nw": np.array([-1, skew]), | |
"ne": np.array([1, skew]), | |
} | |
tiles = defaultdict(int) | |
for line in src: | |
x = [vecs[m] for m in re.findall(r"(e|se|sw|w|nw|ne)", line)] | |
tiles[tuple(np.sum(x, axis=0).round(1))] += 1 | |
black = set() | |
for t, c in tiles.items(): | |
if c % 2 == 1: | |
black.add(t) | |
print("part1:", len(black)) | |
def get_adj(t): | |
adj = set() | |
for v in vecs.values(): | |
adj.add(tuple(np.sum([t, v], axis=0).round(1))) | |
assert len(adj) == 6 | |
return adj | |
def turn(): | |
global black | |
black_copy = set() | |
for t in black: | |
ta = get_adj(t) | |
c = len(black.intersection(ta)) | |
if c == 1 or c == 2: | |
black_copy.add(t) | |
for w in ta.difference(black): | |
c = len(black.intersection(get_adj(w))) | |
if c == 2: | |
black_copy.add(w) | |
black = black_copy | |
for i in range(100): | |
print(i) | |
turn() | |
print("part2:", len(black)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment