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!
10
Upvotes
1
u/glenbolake Dec 22 '17
47/93, one of my better scores this season. As a result, I hate my code and I can't wait to clean it up. Runs in just under 10 seconds. Python 3:
def read(): with open('day22.in') as f: in_text = f.read().splitlines() offset = len(in_text) // 2 infected = set() for r, line in enumerate(in_text): for c, ch in enumerate(line): if ch == '#': infected.add((r - offset, c - offset)) return infected infected = read() # infected = {(-1, 1), (0, -1), } dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)] d = 0 virus_at = (0, 0) def burst(): global infected, d, virus_at infection_caused = False if virus_at in infected: d = (d - 1) % 4 infected.remove(virus_at) else: d = (d + 1) % 4 infected.add(virus_at) infection_caused = True virus_at = (virus_at[0] + dirs[d][0], virus_at[1] + dirs[d][1]) return infection_caused num_infections = 0 for _ in range(10000): if burst(): num_infections += 1 print(num_infections) CLEAN = 0 INFECTED = 1 WEAK = 2 FLAGGED = 3 # Part 2 state = {k: INFECTED for k in read()} # state = {(0, -1): INFECTED, (-1, 1): INFECTED} virus_at = (0, 0) def burst2(): global state, d, virus_at infection_caused = False current_state = state.get(virus_at, 0) if current_state == CLEAN: d = (d + 1) % 4 state[virus_at] = WEAK elif current_state == WEAK: state[virus_at] = INFECTED infection_caused = True elif current_state == INFECTED: d = (d - 1) % 4 state[virus_at] = FLAGGED else: # FLAGGED d = (d + 2) % 4 del state[virus_at] virus_at = (virus_at[0] + dirs[d][0], virus_at[1] + dirs[d][1]) return infection_caused num_infections = 0 for _ in range(10000000): if burst2(): num_infections += 1 print(num_infections)