Skip to content

Instantly share code, notes, and snippets.

View warriordog's full-sized avatar

Hazel K warriordog

  • United States
View GitHub Profile
@warriordog
warriordog / AOC_2020-16.js
Created December 16, 2020 14:03
Solution to Advent of Code 2020 Day 16
function parseInput(path) {
// split input into main sections
const [ fields, myTicket, nearTickets ] = require('fs').readFileSync(path, 'utf-8').split('\r\n\r\n');
// parse into single object
return {
// extract list of fields and requirements
fields: Array.from(fields.matchAll(/^([\w ]+): ([\w\d -]+)$/gm)).map(([, name, reqs]) => ({
name,
options: reqs.split(' or ').map(req => {
@warriordog
warriordog / AOC_2020_Day15_ArrayTree.js
Last active December 16, 2020 02:07
Solution to Advent of Code 2020 Day 15 using an Array Tree for high performance
// @ts-ignore
const input = [ 6, 4, 12, 1, 20, 0, 16 ];
/**
* @template TValue Type of the value held by this ArrayTree
*/
class ArrayTree {
constructor(scale) {
this._lShift = scale;
this._rShift = 32 - scale;
@warriordog
warriordog / AOC_2020_Day15.js
Created December 15, 2020 13:29
Solution to Advent of Code 2020 Day 15 in JavaScript
const input = [ 6, 4, 12, 1, 20, 0, 16 ];
function part1(debug) {
const memory = Array.from(input);
for (let i = input.length; i < 2020; i++) {
const lastNumber = memory[memory.length - 1];
const lastNumberIndex = memory.lastIndexOf(lastNumber, memory.length - 1);
const lastNumberLastIndex = memory.lastIndexOf(lastNumber, lastNumberIndex - 1);
@warriordog
warriordog / AOC_2020_Day14_Part2.js
Created December 14, 2020 18:08
Solution to Advent of Code 2020 Day 14 Part 2
const { parseInputFile } = require('../utils/parser');
/** @typedef {'0' | '1' | 'X'} Trit */
/**
* @param {string} addr
*/
function parseAddr(addr) {
const arr = Array.from((parseInt(addr) >>> 0).toString(2));
while (arr.length < 36) {
@warriordog
warriordog / AOC_2020_Day14_Part1.js
Created December 14, 2020 18:07
Solution to Advent of Code 2020 Day 14 Part 1
const { parseInputFile } = require('../utils/parser');
const inputs = parseInputFile('day14-input.txt', /^(mask|mem)(?:\[(\d+)\])? = ([0-9X]+)$/gm)
.map(([, ins, addr, maskOrValue]) => {
switch (ins) {
case 'mask': return {
ins,
mask: maskOrValue,
maskOR: BigInt(parseInt(maskOrValue.replace(/X/g, '0'), 2)),
maskAND: BigInt(parseInt(maskOrValue.replace(/X/g, '1'), 2))
@warriordog
warriordog / AOC_2020_Day13_Part2_EEA.js
Created December 13, 2020 19:38
Solution for Advent of Code 2020 Day 13 Part 2 using Euler's Extended Algorithm to solve very large inputs
const fs = require('fs');
/**
*
* @param {bigint} a
* @param {bigint} n
*/
function solveMMI(a, n) {
let t = 0n;
let newT = 1n;
@warriordog
warriordog / AOC_2020_Day13_Part2_Sieve.js
Last active December 15, 2020 15:11
Solution to Advent of Code 2020 Day 13 Part 2 that uses a sieve algorithm
const { parseInputFile } = require('../utils/parser');
/**
* Performs a negative-aware modulus operation.
* @param {number} a
* @param {number} n
* @returns {number}
*/
function absmod(a, n) {
while (a < 0) {
@warriordog
warriordog / AOC_2020_Day13_Part2.js
Created December 13, 2020 07:38
Solution to Advent of Code 2020 Day 13 Part 2
const fs = require('fs');
/**
* @param {bigint} a
* @param {bigint} mod
*/
function solveMMI(a, mod) {
const b = a % mod;
for (let x = 1n; x < mod; x++) {
if ((b * x) % mod === 1n) {
@warriordog
warriordog / AOC_2020_Day13_Part1.js
Created December 13, 2020 07:37
Solution to Advent of Code 2020 Day 3 Part 1
const fs = require('fs');
const inputParts = fs.readFileSync('day13-input.txt', 'utf-8').split('\r\n');
const earliestTimestamp = parseInt(inputParts[0].trim());
function findEarliestArrival(id) {
let wait = id;
while (wait < earliestTimestamp) {
wait += id;
}
return wait;
@warriordog
warriordog / AOC_2020_Day12_Part2.js
Created December 12, 2020 05:52
Solutions to Advent of Code 2020 Day 12 Part 2
const { parseInputFile } = require('../utils/parser');
const inputSteps = parseInputFile('day12-input.txt', /^(\w)(\d+)$/gm)
.map(([, action, num]) => ({
action,
amount: parseInt(num)
}));
// +east -west
// +north -south