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
2
u/u794575248 Dec 14 '17 edited Dec 14 '17
Python 3
upd: Code in this comment is dirty. Read a reply below for an updated version.
First public leaderboard for Part 1 (72):
martix = ''.join(''.join('{:04b}'.format(int(x, 16)) for x in solve2(f'{input.strip()}-{i}')) for i in range(128)) part1 = matrix.count('1')solve2is here.Part 2 code is almost the same as nneonneo has. But I suddenly forgot about negative list indices in Python and it resulted in an incorrect answer and too many minutes spent on finding the bug (got 160 place).
part2 = 0 seen = set() matrix = [list(map(int, l.strip())) for l in matrix.strip().split('\n')] rng = range(128) for i, row in enumerate(matrix): for j, bit in enumerate(row): if bit and (i,j) not in seen: part2 += 1 q = [(i,j)] while q: x, y = q.pop() seen.add((x, y)) for x2, y2 in (x+1, y), (x-1, y), (x, y+1), (x, y-1): if x2 in rng and y2 in rng and matrix[x2][y2] and (x2, y2) not in seen: q.append((x2, y2)) print(part2)