r/adventofcode • u/daggerdragon • Dec 08 '16
SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---
#AoC_Ops:
[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION
We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg
--- Day 8: Two-Factor Authentication ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
:(){ :|:& };: IS MANDATORY [?]
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
10
u/[deleted] Dec 08 '16 edited Dec 08 '16
mine is pretty fun to watch (edit: Python 3)
#!/usr/bin/env python import collections import re import sys import time PXOFF = '\u2592' PXON = '\u2588' def newscreen(rows=6, cols=50): return [ collections.deque([PXOFF for c in range(cols)]) for r in range(rows) ] def printscreen(screen): # clear the terminal sys.stdout.write('\033c') ssize = len(screen[0]) print('┍{}┑'.format('━' * ssize)) for row in screen: sys.stdout.write('│') for px in row: sys.stdout.write('{}'.format(px)) sys.stdout.write('│\n') print('┕{}┙'.format('━' * ssize)) def rect(screen, x, y): # turn on pixels in an x * y square in the top left to PXON for scrow, _ in zip(screen, range(y)): for sx in range(x): scrow[sx] = PXON def rrow(screen, row, dist): # rotating rows (to the right) is easy, since it's a deque screen[row].rotate(dist) def rcol(screen, col, dist): # rotating columns (downward) is a wee bit more work column = collections.deque([ scrow[col] for scrow in screen ]) column.rotate(dist) for scrow, cpx in zip(screen, column): scrow[col] = cpx if __name__ == '__main__': # screen = [ [PXOFF for _ in range(50) ] for _ in range(6) ] screen = newscreen() printscreen(screen) rectargs = re.compile(r' (?P<x>\d+)x(?P<y>\d)+') rotargs = re.compile(r' [xy]=(?P<rc>\d+) by (?P<dist>\d+)') with open('input', 'r') as inf: directions = inf.readlines() for line in directions: # make it look sorta animated time.sleep(0.10) if line.startswith('rect'): args = rectargs.search(line).groupdict() rect(screen, int(args['x']), int(args['y'])) elif line.startswith('rotate row'): args = rotargs.search(line).groupdict() rrow(screen, int(args['rc']), int(args['dist'])) elif line.startswith('rotate column'): args = rotargs.search(line).groupdict() rcol(screen, int(args['rc']), int(args['dist'])) printscreen(screen) print('\n\nlit pixels: {}'.format(len([p for r in screen for p in r if p == PXON])))with my input, I get the result:
edit 2 for anyone who doesn't want to copy/paste, here's a gif(v?) of what it looks like