5

Advent of Code 2020: Python Solution Day 24

 3 years ago
source link: https://dev.to/qviper/advent-of-code-2020-python-solution-day-24-53lg
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Advent of Code 2020: Python Solution (25 Part Series)

I knew crab can also navigate but I could not find my best solution to today's challenge so I had to head over to reddit and take help. Credit goes to the author.

import re
from collections import defaultdict

with open("day24.txt") as fp:
    lines = fp.read().splitlines()

curr_floor = defaultdict(lambda: False)
for line in lines:
    coo = re.findall(r"e|se|sw|w|nw|ne", line)
    ns = coo.count("se") + coo.count("sw") - coo.count("ne") - coo.count("nw")
    we = coo.count("e") + coo.count("ne") - coo.count("w") - coo.count("sw")

    curr_floor[((ns, we))] = not curr_floor[(ns, we)]
print(f"Total tiles with back side up: {sum(list(curr_floor.values()))}")

floor = curr_floor

for _ in range(100):
    new_floor = defaultdict(lambda: False)

    # Add the outer ring of the current floor for consideration.
    for k, v in floor.items():
        for o in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]:
            tile = (k[0] + o[0], k[1] + o[1])
            if tile not in new_floor:
                new_floor[tile] = floor.get(tile, False)
    for k, v in new_floor.items():

        neighbors = sum([floor[(k[0] + o[0], k[1] + o[1])] for o in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]])
        if v:
            if not neighbors or neighbors > 2:
                new_floor[k] = False
        else:
            if neighbors == 2:
                new_floor[k] = True

    floor = new_floor


print(f"Total tiles with back side up: {sum(list(new_floor.values()))}")

Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK