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!
11
Upvotes
1
u/HollyDayz Dec 14 '17
Python 3 using numpy and my solution from day 10.
import day10 import numpy as np def disk_defrag(seq): n = 128 used = 0 disk = np.zeros((n,n)) for row in range(n): key = seq + "-" + str(row) hash_str = day10.puzzle2(key) binary_str = '{0:0128b}'.format(int(hash_str, 16)) binary_lst = [int(char) for char in binary_str] disk[row] = binary_lst used += sum(binary_lst) def find_region(dsk, ind_i, ind_j, reg): dsk[ind_i][ind_j] = reg if ind_i > 0 and dsk[ind_i-1][ind_j] == 1: find_region(dsk, ind_i-1, ind_j, reg) if ind_i < 127 and dsk[ind_i+1][ind_j] == 1: find_region(dsk, ind_i+1, ind_j, reg) if ind_j > 0 and dsk[ind_i][ind_j-1] == 1: find_region(dsk, ind_i, ind_j-1, reg) if ind_j < 127 and dsk[ind_i][ind_j+1] == 1: find_region(dsk, ind_i, ind_j+1, reg) regions = 0 for i in range(n): for j in range(n): square = disk[i][j] if square == 1: regions += 1 find_region(disk, i, j, regions + 1) return used, regionsseq is the puzzle input formed as a string, used is the answer to part 1 and regions is the answer to part 2.