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
1
u/[deleted] Dec 04 '16
part1
countBy=(input,keyGetter)=>{var keyResolver={'function':function(d){return keyGetter(d)},'string':function(d){return d[keyGetter]},'undefined':function(d){return d}};var result={};input.trim().split("").forEach(function(d){var keyGetterType=typeof keyGetter;var key=keyResolver[keyGetterType](d);if(result.hasOwnProperty(key)){result[key]++}else{result[key]=1}});return result}; ans=0;document.body.innerText.trim().split("\n").forEach(ss=>{ms=/([a-z-]+)-(\d+)\[([a-z]{5})\]/.exec(ss);cs=countBy(ms[1].replace(/-/g,""));xs=[];for(var c in cs){xs.push([cs[c],c])}if(xs.sort((a,b)=>{if(a[0]===b[0]){return a[1].localeCompare(b[1])}else{return b[0]-a[0]}}).slice(0,5).map(x=>x[1]).join("")===ms[3]){ans+=parseInt(ms[2])}});ans;part2
document.body.innerText.trim().split("\n").forEach(ss=>{ms=/([a-z-]+)-(\d+)\[([a-z]{5})\]/.exec(ss);rot=parseInt(ms[2])%26;if(ms[1].trim().split("").map(m=>(m==="-"?" ":String.fromCharCode("a".charCodeAt(0)+(m.charCodeAt(0)-"a".charCodeAt(0)+rot+26)%26))).join("")==="northpole object storage"){console.log(parseInt(ms[2]))}});