Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Last active December 4, 2021 06:36
Show Gist options
  • Save jazzyjackson/4dd6942598f65836651a221879cf798f to your computer and use it in GitHub Desktop.
Save jazzyjackson/4dd6942598f65836651a221879cf798f to your computer and use it in GitHub Desktop.
input
.split('\n')
.map((e, i, a) => i && a[i] - a[i - 1])
.filter(n => n > 0)
.length
// 1527
// create a new array, then scan the input, adding each element to its coresponding index and the next two indexes as well
// is the number of additions any different than looking ahead and behind for each of them?
// for 1st strategy, for each element, I read 3 and write once.
// for 2nd strategy, for each element, I read once and write three times.
// 1st: number + undefined = NaN, which will be filtered out when compared to 0
input
.split('\n')
.map(Number)
.map((e, i, a) => a[i] + a[i+1] + a[i+2])
.map((e, i, a) => i && a[i] - a[i - 1])
.filter(n => n > 0)
.length
// 1575
input.reduce((result, command) => {
let [ direction, distance ] = command.match(/([a-z]+)\s(\d+)/).slice(1)
switch(direction){
case "forward": result.x += +distance; break
case "up": result.y -= +distance; break
case "down": result.y += +distance
}
return result
}, {x: 0, y: 0})
// -> Object { x: 1925, y: 879 }
// 1925 * 879 = 1692075
// Part two
input.reduce((result, command) => {
let [ direction, distance ] = command.match(/([a-z]+)\s(\d+)/).slice(1)
switch(direction){
case "up": result.aim -= +distance; break
case "down": result.aim += +distance; break
default: /* 'forward' */
result.x += +distance
result.y += +distance * result.aim
}
return result
}, {x: 0, y: 0, aim: 0})
// -> Object { x: 1925, y: 908844, aim: 879 }
// 1925 * 908844 = 1749524700
// for each of the digits, sum the column, then check if the sum is greater than half the length
// this tells you if the number of ones is greater than the number of zeros
input = input
.split('\n')
.map(e => e.split('').map(Number))
result = input
.reduce((prev,curr) => prev.map((e, i) => e + curr[i]))
.map(e => Number(e > (input.length / 2)))
.join('')
gamma = parseInt(result, 2)
episilon = gamma ^ parseInt("".padEnd(result.length, 1), 2)
candidates = input
cursor = 0
while(candidates.length > 1){
// given a cursor index, decide which bit is more common
let column = candidates.map(each => each[cursor])
let sum = column.reduce((a,b) => a + b)
let result = Number(sum >= (column.length / 2))
// then filter the candidates
candidates = candidates.filter(each => each[cursor] == result)
cursor++
}
o2 = parseInt(candidates.pop().join(''), 2)
console.log("O2", o2)
candidates = input
cursor = 0
while(candidates.length > 1){
// given a cursor index, decide which bit is more common
let column = candidates.map(each => each[cursor])
let sum = column.reduce((a,b) => a + b)
let result = Number(sum < (column.length / 2))
// then filter the candidates
candidates = candidates.filter(each => each[cursor] == result)
cursor++
}
co2 = parseInt(candidates.pop().join(''), 2)
console.log("CO2", co2)
parseInput = input => {
let [maybes, ...boards] = input.split('\n\n')
maybes = maybes.split(',').map(Number)
boards = boards.map(board => board.split('\n').map(e => e.trim().split(/\s+/).map(Number)))
return {maybes, boards}
}
let transpose = m => m[0].map((x,i) => m.map(x => x[i]))
let flatten = m => m.reduce((prev, curr) => prev.concat(curr))
let {maybes, boards} = parseInput(input)
let bingo = new Map()
boards.forEach(board => {
board.map(row => bingo.set(row, board))
transpose(board).map(col => bingo.set(col, board))
})
//
let fives = []
bingo.forEach((_, five) => fives.push(five))
var score = 0
var winner = null
var cursor = 5
while(boards.length > 1 && cursor < maybes.length){
let test = maybes.slice(0, cursor++)
fives.forEach(five => {
if(five.every(one => test.includes(one))){
winner = bingo.get(five)
console.log("WINNER", winner)
console.log("SCORE", flatten(winner).filter(n => !test.includes(n)).reduce((a,b)=>a+b))
console.log("LAST", test[test.length - 1])
console.log("INDEX", boards.indexOf(winner))
boards.splice(boards.indexOf(winner), 1)
}
})
}
console.log(boards)
// given the winning board, fl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment