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-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 / 2019-advent-of-code-7.js
Created December 27, 2019 03:38
Advent of Code 2019 - Day 7
// Part 1
const parseCommand = (opcode) => {
const str = `${opcode}`
let modes = []
if (str.length > 2) {
opcode = +str.substr(-2)
modes = str.substr(0, str.length - 2).split('').reverse().map(x => +x)
}
return { opcode, modes }
}
@cranderveldt
cranderveldt / 2019-advent-of-code-5.js
Last active December 25, 2019 03:02
Advent of Code 2019 - Day 5
// Part 1
const parseCommand = (opcode) => {
const str = `${opcode}`
let modes = []
if (str.length > 2) {
opcode = +str.substr(-2)
modes = str.substr(0, str.length - 2).split('').reverse().map(x => +x)
}
return { opcode, modes }
}
@cranderveldt
cranderveldt / 2019-advent-of-code-6.js
Last active December 20, 2019 21:27
Advent of Code 2019 - Day 6
// Part 1
const searchForSatellite = (area, name, payload) => {
let foundChild = false
if (typeof area[name] === 'boolean') {
area[name] = payload
foundChild = true
return foundChild
}
Object.keys(area).filter(x => typeof area[x] === 'object').forEach(key => {
foundChild = searchForSatellite(area[key], name, payload) || foundChild
@cranderveldt
cranderveldt / 2019-advent-of-code-4.js
Created December 17, 2019 21:30
Advent of Code 2019 - Day 4
// Part 1
const isNumberGood = (num) => {
const digits = num.toString().split('').map(x => +x)
return digits.every((digit, index, arr) => digit >= (arr[index - 1] || 0)) &&
digits.some((digit, index, arr) => digit === arr[index - 1])
}
const countPasswordOptions = (input) => {
const range = input.split('-').map(x => +x)
let count = 0
@cranderveldt
cranderveldt / 2019-advent-of-code-3.js
Last active December 17, 2019 21:03
Advent of Code 2019 - Day 3
// Part 1
const parsePoint = (move) => {
const num = parseInt(move.match(/\d+/gi), 10)
switch(move[0]) {
case 'R': return { num, axis: 'x' }
case 'L': return { num: num * -1, axis: 'x' }
case 'D': return { num, axis: 'y' }
case 'U': return { num: num * -1, axis: 'y' }
}
}
@cranderveldt
cranderveldt / 2019-advent-of-code-2.js
Last active December 24, 2019 23:25
Advent of Code 2019 - Day 2
// Part 1
const handleInstruction = ([command, src1, src2, dest], integers) => {
switch(command) {
case 1:
integers[dest] = integers[src1] + integers[src2]
break
case 2:
integers[dest] = integers[src1] * integers[src2]
break
case 99: return true
@cranderveldt
cranderveldt / advent-of-code-1.js
Last active December 16, 2019 16:21
Advent of Code 2019 - Day 1
// Part 1
const fillEmUp = (modules) => {
return modules.reduce((acc, mass) => {
return acc + (Math.floor(mass / 3) - 2)
}, 0)
}
// Part 2
const fuelForMass = (mass) => {
const fuel = (Math.floor(mass / 3) - 2)
@cranderveldt
cranderveldt / ion-selects.html
Last active August 12, 2018 10:06
Example markup for odd behavior in Ionic v4
<ion-header>
<ion-toolbar>
<ion-title>About</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item>
<ion-label position="floating">This is a label for a text field</ion-label>