Skip to content

Instantly share code, notes, and snippets.

@ZeikJT
ZeikJT / adventOfCode2023Day01.js
Last active December 17, 2023 07:16
Advent of Code 2023
function adventOfCode2023_Day01_Part1(str) {
return str.trim().split('\n').map((line) => {
const firstNum = /\d/.exec(line)[0]
const lastNum = /(\d)[a-zA-Z]*?$/.exec(line)[1]
return Number(firstNum + lastNum)
}).reduce((total, num) => total + num, 0)
}
function adventOfCode2023_Day01_Part2(str) {
const CONVERSION = new Map([
@ZeikJT
ZeikJT / adventOfCode2022Day01.js
Last active February 5, 2023 19:11
Advent of Code 2022
function getElfCalories(str) {
return str.trim().split('\n').reduce((arr, val) => {
if (val === '') {
arr.push(0)
} else {
const maxIndex = arr.length - 1
arr[maxIndex] = arr[maxIndex] + Number(val)
}
return arr
}, [0])
@ZeikJT
ZeikJT / MagicString.js
Last active December 6, 2023 04:43
Concatenating and substringing strings without doing native string operations in JS
class MagicString {
static #convertStringToData(str) {
return {str, start: 0, length: str.length}
}
static #makeDataCopy(data) {
return data.map((data) => ({...data}))
}
static #splitData(index, data) {
let offset = index
const offsetIndex = data.findIndex(({length}) => {
@ZeikJT
ZeikJT / 2048-Vector-Movement.js
Created February 5, 2022 10:07
Fun experiment based on idea from https://www.twitch.tv/lunatic_mag
function vectorMove(vector) {
for (let i = vector.length - 2; i >= 0; i--) {
if (vector[i] === 0) continue
for (let j = i + 1; j < vector.length; j++) {
if (vector[i] !== vector[j] && vector[j] !== 0) break
if (vector[j] === 0) {
vector[j] = vector[i]
} else {
vector[j] *= 2
}
@ZeikJT
ZeikJT / adventOfCode2015Day01.js
Last active December 18, 2017 16:35
Advent of Code 2015 - Days 1 - 12 (#9 unsolved)
function adventOfCode2015_Day01_Part1(str) {
return Array.prototype.reduce.call(str, (floor, char) => (floor + (char === '(' ? 1 : -1)), 0)
}
function adventOfCode2015_Day01_Part2(str) {
let floor = 0
let position = 1
for (const char of str) {
floor += char === '(' ? 1 : -1
if (floor === -1) {
@ZeikJT
ZeikJT / adventOfCode2017Day01.js
Last active December 27, 2017 18:09
Advent of Code 2017 - Days 1 through 22 (skipped 21)
function adventOfCode2017_Day01_Part1(str) {
str += str[0]
let sum = 0
for (let i = 0; i < str.length - 1; i++) {
if (str[i] == str[i + 1]) {
sum += Number(str[i])
}
}
return sum
}