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))})).map((a)=>{a.n = a.n.replace(/-/g, ""); return a;}); var roomsWithChecksum = rooms.map((r)=>{var f = r.n.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 sum = correctRooms.reduce((a, r)=>(a + r.id), 0); console.log(sum);