Skip to content

Instantly share code, notes, and snippets.

@dan-coulter
dan-coulter / aoc01.js
Created December 7, 2023 03:09
Advent of Code 2023 #01
const { readFile } = require('node:fs/promises');
const filename = 'input.txt';
const firstDigitRegex = /\d/;
const lastDigitRegex = /\d[a-z]*$/;
const parseCalibrationFile = async (file) => {
const data = (await readFile('input.txt', 'utf8')).split('\n');
const sum = data.reduce((acc, line) => {
const firstDigit = parseInt(line.match(firstDigitRegex) ?? '0');
export const truncate = (source: string, n: number): string => source.replace(new RegExp(`([a-z]{${n}})[a-z]+`, 'gi'), '$1');
@dan-coulter
dan-coulter / longestPrefix.js
Last active September 1, 2021 06:00
Find longest common string prefix
// Find the longest common prefix
const longestPrefix = (arr) => {
// Handle a couple of invalid inputs
if (!arr || !Array.isArray(arr) || arr.length === 0) {
return '';
// Bypass the loop if there's only one element in the array
} else if (arr.length === 1) {
return arr[0];
}
// Create the test
var pixelRatio = (window.devicePixelRatio >= 1.5) ? "high" : "normal";
..
// Pass it along through GA
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
// --- IMPORTANT LINE!
// params: event method, custom variable slot, variable name, variable value, scope level

Keybase proof

I hereby claim:

  • I am dan-coulter on github.
  • I am danco (https://keybase.io/danco) on keybase.
  • I have a public key whose fingerprint is C277 80F1 FCF5 4474 467B 0457 C51A AC19 EA6E 3928

To claim this, I am signing this object: