Skip to content

Instantly share code, notes, and snippets.

View cranderveldt's full-sized avatar

Trevor cranderveldt

View GitHub Profile
@cranderveldt
cranderveldt / advent-of-code-2020-day-12.js
Created December 14, 2020 17:37
Advent of Code 2020 Day 12
const puzzleInput = ``
const parseInput = (input) => {
return input.split(/\n/)
}
// Part 1
const nextDir = (facing, going, amount) => {
const clockwise = 'NESW'
const facingIndex = clockwise.indexOf(facing)
@cranderveldt
cranderveldt / advent-of-code-2020-day-11.js
Last active December 11, 2020 14:56
Advent of Code 2020 Day 11
const puzzleInput = ``
// Common
const parseInput = (input) => {
return input.split(/\n/).map(x => x.split(''))
}
const considerSurroundings = (rows, rowIndex, colIndex, comparator, maxDistance, threshold) => {
const range = [-1, 0, 1]
return range.reduce((acc1, yOffset) => (
@cranderveldt
cranderveldt / advent-of-code-2020-day-10.js
Last active December 10, 2020 21:01
Advent of Code 2020 Day 10
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split(/\n/).map(x => parseInt(x))
}
const getDifferences = (adapters) => {
return adapters.map((adapter, index) => {
let prev = adapters[index - 1] || 0
@cranderveldt
cranderveldt / advent-of-code-2020-day-9.js
Created December 9, 2020 20:46
Advent of Code 2020 Day 9
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split(/\n/).map(x => parseInt(x))
}
const findInvalidNumber = (numbers) => {
return numbers.reduce((acc, number, index) => {
if (index < 25) { return acc }
@cranderveldt
cranderveldt / advent-of-code-2020-day-3.js
Last active December 8, 2020 21:24
Advent of Code 2020 Day 3
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split('\n')
}
const traverse = (rows, colInc, rowInc = 1) => {
return rows.reduce((acc, row, index) => {
if (index % rowInc !== 0) { return acc }
@cranderveldt
cranderveldt / advent-of-code-2020-day-8.js
Created December 8, 2020 14:31
Advent of Code 2020 Day 8
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split(/\n/)
}
const interpretInstruction = (line) => {
let [command, amount] = line.split(' ')
return {
@cranderveldt
cranderveldt / advent-of-code-2020-day-2.js
Last active December 8, 2020 13:31
Advent of Code 2020 Day 2
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split('\n')
}
const parseRecord = (record) => {
const regex = /(\d+)\-(\d+) ([a-z]): ([a-z]*)/gi
return [...record.matchAll(regex)][0]
@cranderveldt
cranderveldt / advent-of-code-2020-day-1.js
Last active December 7, 2020 18:00
Advent of Code 2020 Day 1
// Part 1
const input = ``
const findPairsForSum = (numbers, sum) => {
return numbers.reduce((acc, num1) => {
if (acc) { return acc }
const inner = numbers.find((num2) => num1 + num2 === sum)
if (inner) { return [num1, inner] }
}, null)
}
@cranderveldt
cranderveldt / advent-of-code-2020-day-7.js
Created December 7, 2020 15:03
Advent of Code 2020 Day 7
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split(/\n/)
}
const interpretRule = (rule) => {
const matches = [...rule.matchAll(/^([a-z ]+) bags contain ((\d+ [a-z ]+(, )?)+|no other bags)\.$/gi)][0]
const contain = matches[2].split(', ').map(str => {
@cranderveldt
cranderveldt / day6.js
Created December 7, 2020 03:33
Advent of Code Day 6
// Pro tip: watch for google trying to auto-translate the puzzle input lol
const puzzleInput = ``
// Part 1
const parseInput = (input) => {
return input.split(/\s\s/)
}
const groupCountAny = (group) => {