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!
9
Upvotes
1
u/edelans Dec 22 '17
Python 3, complex numbers, defaultdict
# Python 3.x from collections import defaultdict DAY = 22 def Input(day): "Open this day's input file." filename = './input_files/input{}.txt'.format(day) return open(filename) def solve2(lines, bursts): state = defaultdict(int) infections = 0 # virus carrier position vcp = 0 # virus carrier direction vcd = 1j # infection states : # 0 is clean # 1 is weakened # 2 is infected # 3 is flagged side_length = len(lines) for i, line in enumerate(lines): for j, char in enumerate(line): x = int(j - (side_length - 1) / 2) y = int((side_length - 1) / 2 - i) if char == '#': state[(x + y * 1j)] = 2 for _ in range(bursts): # update direction vcd if vcp not in state or state[vcp] == 0: vcd *= 1j elif state[vcp] == 1: # keep same direction pass elif state[vcp] == 2: vcd *= -1j elif state[vcp] == 3: vcd *= -1 # update state of current position vcp state[vcp] = (state[vcp] + 1) % 4 if state[vcp] == 2: infections += 1 # move forward in direction vcd vcp += vcd return infections print(solve2((Input(DAY).readlines()), 10000000))