adventofcode-2016/day04/part2.js
2016-12-05 08:59:45 +00:00

9 lines
913 B
JavaScript

var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
var rooms = input.split("\n").filter((a)=>(a)).map((a)=>({n: a.slice(0, -10), c: a.slice(-6, -1), id: parseInt(a.slice(-10, -7))}));
var roomsWithChecksum = rooms.map((r)=>{var f = r.n.replace(/-/g, "").split("").reduce((a, b)=>{if(!a[b]){a[b] = 0}; a[b]++; return a;}, {}); var f2 = []; for(var l in f){if(!f2[f[l]]){f2[f[l]] = [];}f2[f[l]].push(l);}; r.c2 = f2.reverse().map((a)=>(a.sort())).map((a)=>(a.join(""))).join("").substr(0, 5); return r;});
var correctRooms = roomsWithChecksum.filter((r)=>(r.c == r.c2));
var decryptedRooms = correctRooms.map((a)=>{a.n = a.n.split("").map((b)=>((b == "-") ? (" "):String.fromCharCode((((b.charCodeAt(0) - 97)+a.id)%26)+97))).join(""); return a;});
console.log(decryptedRooms);
var possibleRooms = decryptedRooms.filter((a)=>(a.n.indexOf("north") != -1));
console.log(possibleRooms);