Last active
December 13, 2017 12:52
-
-
Save Saturate/0097d73d20dc5064820dd67f33aee196 to your computer and use it in GitHub Desktop.
Advent of Code 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
const digits = [...document.body.innerText.trim()].map(Number); | |
function captcha(input, interval = 1) { | |
return digits.reduce((accu, digit, index) => { | |
const nextIndex = (index + interval) % digits.length; | |
return digit === digits[nextIndex] ? accu + digit : accu; | |
}, 0); | |
} | |
const star1 = captcha(digits); | |
const star2 = captcha(digits, digits.length / 2); | |
console.log(`*1 = ${star1}\n*2 = ${star2}`); | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Star one | |
(function() { | |
const rawRows = document.body.textContent.trim().split('\n'); | |
const table = rawRows.map(cell => cell.split('\t').map(Number)); | |
var results = table.map((row) => { | |
const max = Math.max(...row); | |
const min = Math.min(...row); | |
return max - min; | |
}) | |
console.log(results.reduce((a, c) => a + c)); | |
})() | |
// Star two | |
(function() { | |
const rawRows = document.body.textContent.trim().split('\n'); | |
const table = rawRows.map(cell => cell.split('\t').map(Number)); | |
var results = table.map((row) => { | |
for (let i = 0; i < row.length; ++i) { | |
for (let j = 0; j < row.length; ++j) { | |
if (row[i] % row[j] == 0 && i != j) { | |
return row[i] / row[j]; | |
} | |
} | |
} | |
}) | |
console.log(results); | |
console.log(results.reduce((a, c) => a + c)); | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
let programs = document.body.textContent.trim().split('\n').map((programRaw) => { | |
const program = /(.+) \((\d*)\) ?-?>? ?(.*)/gi.exec(programRaw); | |
return { | |
name: program[1], // ID | |
weight: program[2], | |
childs: program[3] ? program[3].split(', ') : null | |
} | |
}); | |
// Set parents | |
programs = programs.map((program) => { | |
const name = program.name; | |
var parent = programs.find((program) => { | |
return program.childs && program.childs.find((programName) => { | |
return name == programName; | |
}); | |
}); | |
program.parent = parent; | |
return program; | |
}); | |
// Find the one with no parents | |
const star1 = programs.find((program) => { | |
return !program.parent; | |
}); | |
console.log('Star #1:', star1.name) | |
console.log('end program', programs) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment