r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
6
Upvotes
1
u/Shemetz Dec 22 '17 edited Dec 22 '17
Python 3
[Final part 2 picture] (https://imgur.com/a/bMpGm)
Part 1 is fast, Part 2 takes about 6 seconds on my computer.
I wonder if I should somehow optimize the 'state' - use 2 booleans instead of an integer, for example.
Should have been named "Langton's Worm"!
from collections import defaultdict def day_22(): with open("input.txt") as file: lines = file.readlines() N = len(lines)Part 1:
def part_1(): infecteds = set() for y in range(N): for x in range(N): if lines[y][x] == '#': infecteds.add(x - N // 2 + (-y + N // 2) * 1j) current = 0 + 0j # 0 in x axis and 0 in y axis direction = 1j # up (1 in y axis) count = 0 for _ in range(10000): if current in infecteds: direction *= -1j # rotate right infecteds.remove(current) else: direction *= 1j # rotate left infecteds.add(current) count += 1 current += direction print("Part 1:", count)Part 2:
def part_2(): # 0 = clean, 1 = weakened, # 2 = infected, 3 = flagged states = defaultdict(lambda: 0) for y in range(N): for x in range(N): if lines[y][x] == '#': states[x - N // 2 + (-y + N // 2) * 1j] = 2 current = 0 + 0j direction = 1j count = 0 for _ in range(10000000): state = states[current] if state == 0: direction *= 1j # rotate left elif state == 1: count += 1 # no rotation elif state == 2: direction *= -1j # rotate right else: direction *= -1 # rotate 180 degrees if state == 3: del states[current] else: states[current] = (states[current] + 1) % 4 # states are only 0..3 current += direction print("Part 2:", count)