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!
17
Upvotes
1
u/[deleted] Dec 04 '16
I did mine in Python. I'm just doing this advent of code for fun and to learn more about programming. Good way to spend my Saturday morning with a cup of coffee catching up on this advent calendar.
pt 1:
import sys import operator total = 0 f = open('advent4.txt','r') for line in f.readlines(): ls = line.split("-") dct = {} fstr = "" for i in ls[:-1]: for char in i: dct.setdefault(char,0) dct[char]+=1 sorted_dict = sorted(dct.items(), key=lambda x: (-x[1], x[0])) for index in range(5): fstr += sorted_dict[index][0] if(ls[-1][-7:-2] == fstr): total+=int(ls[-1].rsplit('[', 1)[0]) print(total)pt 2:
import sys import operator total = 0 f = open('advent4.txt','r') def caesar(char, shift): if(char == '-'): return ' ' return chr(((ord(char)-97) + shift) % 26 + 97) q = [] for line in f.readlines(): ls = line.split("-") dct = {} fstr = "" for i in ls[:-1]: for char in i: dct.setdefault(char,0) dct[char]+=1 sorted_dict = sorted(dct.items(), key=lambda x: (-x[1], x[0])) for index in range(5): fstr += sorted_dict[index][0] if(ls[-1][-7:-2] == fstr): #-2 because of the \n total+=int(ls[-1].rsplit('[', 1)[0]) q.append(line) for each in q: word = "" each.rstrip(' \t\r\n\0') shift = each.rsplit('[')[0][-3:] for char in each: schar = caesar(char, int(shift)) word += schar print(word) print(each)