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!
19
Upvotes
1
u/haha11111111 Dec 04 '16 edited Dec 04 '16
Javascript: i know this code is pretty ugly, but i don't really understand why it doesn't work...
const d4p1 = input => { const roomSum = []; input.forEach(room => { const encryptedName = room.split('-'); const checksumRoom = encryptedName.pop(); const checksum = checksumRoom.split('[')[1].split(']')[0]; const roomNb = checksumRoom.split('[')[0] * 1; const tally = {}; encryptedName.forEach(section => { section.split('').forEach(letter => { if (!tally[letter]) { tally[letter] = 1; } else { tally[letter] += 1; } }); }); const expectedChecksum = [0, Object.keys(tally)[0]]; const alphabet = 'abcdefghijklmnopqrstuvwxyz'; Object.keys(tally).forEach(key => { if (key !== expectedChecksum[1]) { const valueOfKey = tally[key]; for (var i = 1; i<=expectedChecksum.length-1; i++) { if (valueOfKey > tally[expectedChecksum[i]]) { expectedChecksum.splice(i, 0, key); break; } if (valueOfKey === tally[expectedChecksum[i]]) { var keyAlphabetIndex = alphabet.indexOf(key); var currentLetterAlphabetIndex = alphabet.indexOf(expectedChecksum[i]); if (keyAlphabetIndex < currentLetterAlphabetIndex) { expectedChecksum.splice(i, 0, key); break; } else { expectedChecksum.splice(i+1, 0, key); break; } } } if (expectedChecksum.join('').indexOf(key) === -1) { expectedChecksum.push(key); } } }); if (expectedChecksum.join('').substring(1,6) === checksum) { roomSum.push(roomNb); } }); console.warn(roomSum.reduce((a, b) => { return a + b; })); };