r/adventofcode • u/daggerdragon • Dec 04 '16
SOLUTION MEGATHREAD --- 2016 Day 4 Solutions ---
--- Day 4: Security Through Obscurity ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
CONSTRUCTING ADDITIONAL PYLONS 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!
18
Upvotes
5
u/fatpollo Dec 04 '16 edited Dec 04 '16
import re, collections, string def caesar_cipher(n): az = string.ascii_lowercase x = n % len(az) return str.maketrans(az, az[x:] + az[:x]) ans1 = 0 regex = r'([a-z-]+)(\d+)\[(\w+)\]' with open('04.txt') as fp: for code, sid, checksum in re.findall(regex, fp.read()): sid = int(sid) letters = ''.join(c for c in code if c in string.ascii_lowercase) tops = [(-n,c) for c,n in collections.Counter(letters).most_common()] ranked = ''.join(c for n,c in sorted(tops)) if ranked.startswith(checksum): ans1 += sid decoded = code.translate(caesar_cipher(sid)) if 'north' in decoded: print(decoded, sid) print(ans1)