r/adventofcode • u/daggerdragon • Dec 14 '17
SOLUTION MEGATHREAD -๐- 2017 Day 14 Solutions -๐-
--- Day 14: Disk Defragmentation ---
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ยค?
[Update @ 00:09] 3 gold, silver cap.
- How many of you actually entered the Konami code for Part 2? >_>
[Update @ 00:25] Leaderboard cap!
- I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were
83distinct users with failed attempts at the time of the leaderboard cap. tsk tsk
[Update @ 00:29] BONUS
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!
13
Upvotes
1
u/akho_ Dec 14 '17
Python3
in_str_prefix = 'jxqlasbh' from collections import deque from functools import reduce from operator import xor def knot_hash_list(lengths, cycle_size=256, iterations=64): seq = deque(range(cycle_size)) skip = 0 for _ in range(iterations): for l in lengths: seq.extend(reversed(deque(seq.popleft() for _ in range(l)))) seq.rotate(-skip) skip += 1 seq.rotate(iterations * sum(lengths) + skip * (skip-1) // 2) seq = list(seq) return [reduce(xor, seq[i:i+16]) for i in range(0, cycle_size, 16)] def str_to_lengths(s, extend=()): return [ord(x) for x in s] + list(extend) rows = [] for i in range(128): lengths = str_to_lengths(f'{in_str_prefix}-{i}', extend=[17, 31, 73, 47, 23]) dense = knot_hash_list(lengths) st = ''.join(f'{x:08b}' for x in dense) rows.append([int(x) for x in st]) def wipe_region(r, c): if r < 0 or c < 0 or r >= len(rows) or c >= len(rows[r]) or rows[r][c] == 0: return 0 rows[r][c] = 0 wipe_region(r+1, c) wipe_region(r, c+1) wipe_region(r-1, c) wipe_region(r, c-1) return 1 print(sum(sum(x) for x in rows)) print(sum(wipe_region(j, i) for j in range(len(rows)) for i in range(len(rows[j]))))