Skip to content

Instantly share code, notes, and snippets.

@pseale
Created December 29, 2016 04:46
Show Gist options
  • Save pseale/6cf9028dca1963a49d9d5b17abf82be5 to your computer and use it in GitHub Desktop.
Save pseale/6cf9028dca1963a49d9d5b17abf82be5 to your computer and use it in GitHub Desktop.
import _ = require("lodash")
let partAInput = "01110110101001000"
function flipItAndReverseIt(input: string): string {
return input.concat("0").concat((_.reverse(input.split(""))).map(x => x === "1" ? "0" : "1").join(""))
}
function fillDisk(input: string, size: number): string {
let data = input
while (data.length < size) {
data = flipItAndReverseIt(data)
}
return data.substr(0, size)
}
function generateChecksum(input: string): string {
let checksum = input
while (true) {
checksum = _.range(0, Math.floor(checksum.length / 2)).map(i => checksum[i*2] === checksum[i*2 + 1] ? "1" : "0").join("")
if (checksum.length % 2 !== 0) {
return checksum
}
}
}
console.log(`Part A: ${generateChecksum(fillDisk(partAInput, 272))}`)
console.log(`Part B: ${generateChecksum(fillDisk(partAInput, 35651584))}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment