Skip to content

Instantly share code, notes, and snippets.

@dnnsmnstrr
Created December 4, 2021 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnnsmnstrr/cbc8da3fd478f954afab65fc98854135 to your computer and use it in GitHub Desktop.
Save dnnsmnstrr/cbc8da3fd478f954afab65fc98854135 to your computer and use it in GitHub Desktop.
  • Day 1
  • Day 2
  • Day 3
  • Day 4
const testReport = [
199,
200,
208,
210,
200,
207,
240,
269,
260,
263
]
const getDepthIncreases = (report = []) => {
let lastDepth = report[0]
const increases = report.reduce((previous, current) => {
console.log('previous, current, lastDepth', previous, current, lastDepth)
if (current > lastDepth) {
lastDepth = current
return previous + 1
}
lastDepth = current
return previous
}, 0)
console.log('increases', increases)
return increases
}
getDepthIncreases(testReport)
const windowExample = [
199,
200,
208,
210,
200,
207,
240,
269,
260,
263
]
const getWindowDepthIncreases = (report = []) => {
const getWindowSum = (startIndex) => {
return report[startIndex] + report[startIndex + 1] + report[startIndex + 2]
}
let lastWindow = getWindowSum(0)
const increases = report.reduce((previous, current, index) => {
const currentWindow = getWindowSum(index)
if (currentWindow > lastWindow) {
previous = previous + 1
}
lastWindow = currentWindow
return previous
}, 0)
console.log('increases', increases)
return increases
}
getWindowDepthIncreases(windowExample)
const testCommands = [
'forward 5',
'down 5',
'forward 8',
'up 3',
'down 8',
'forward 2',
]
function getFile(filePath, separator = '\n') {
let result = require('fs')
.readFileSync(filePath)
.toString()
.split(separator)
.filter((a) => a != '');
if (result.includes('\r')) {
result = result.replaceAll(/\r/, '');
}
return result;
}
const inputCommands = getFile('input.txt')
// console.log(inputCommands.length)
const getFinalPosition = (commands = []) => {
return commands.reduce((previous, currentCommand) => {
const [direction, value] = currentCommand.split(' ')
const numberValue = parseInt(value)
switch (direction) {
case 'down':
previous.depth = previous.depth + numberValue
break;
case 'up':
previous.depth = previous.depth - numberValue
break;
default:
previous.horizontal = previous.horizontal + numberValue
}
return previous
}, { horizontal: 0, depth: 0})
}
const finalPosition = getFinalPosition(inputCommands)
console.log('finalPosition', finalPosition)
console.log(finalPosition.horizontal * finalPosition.depth)
const getFinalPositionWithAim = (commands) => {
return commands.reduce((previous, currentCommand) => {
const [direction, value] = currentCommand.split(' ')
const numberValue = parseInt(value)
switch (direction) {
case 'down':
previous.aim = previous.aim + numberValue
break;
case 'up':
previous.aim = previous.aim - numberValue
break;
default:
previous.horizontal = previous.horizontal + numberValue
previous.depth = previous.depth + numberValue * previous.aim
}
return previous
}, { horizontal: 0, depth: 0, aim: 0})
}
const finalPositionWithAim = getFinalPositionWithAim(inputCommands)
console.log('finalPositionWithAim', finalPositionWithAim)
console.log(finalPositionWithAim.horizontal * finalPositionWithAim.depth)
import { getFile, binaryArrayToNumber } from './helper'
const inputReport = getFile('day3')
console.log('inputReport.length', inputReport.length)
const testReport = [
'00100',
'11110',
'10110',
'10111',
'10101',
'01111',
'00111',
'11100',
'10000',
'11001',
'00010',
'01010'
]
const getRate = (report = [], type = 'gamma') => {
let rateArray = []
const positionCount = report[0].length
console.log('positionCount', positionCount)
const getMostCommonAtPosition = (position = 0) => {
const [zero, one] = report.reduce((previous, current) => {
if (current[position] === '1') {
previous[1] += 1
} else {
previous[0] += 1
}
return previous
}, [0, 0])
return zero > one ? 0 : 1
}
for (var i = 0; i < positionCount; i++) {
const mostCommon = getMostCommonAtPosition(i)
const leastCommon = mostCommon === 1 ? 0 : 1
rateArray.push(type === 'gamma' ? mostCommon : leastCommon)
}
return binaryArrayToNumber(rateArray)
}
const calculateResult = (report = []) => {
if (!report || !report.length) {
throw new Error('missing report')
}
const gammaRate = getRate(report)
const epsilonRate = getRate(report, 'epsilon')
console.log('gammaRate', gammaRate)
console.log('epsilonRate', epsilonRate)
const result = gammaRate * epsilonRate
console.log(result)
return result
}
calculateResult(inputReport)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment