Skip to content

Instantly share code, notes, and snippets.

@jjclxrk
jjclxrk / 08.mjs
Created December 10, 2023 01:21
[aoc2023] - 08.mjs
import fs from "fs";
import readline from "readline";
const fileStream = fs.createReadStream("input.txt");
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
@jjclxrk
jjclxrk / 01b.mjs
Last active December 2, 2023 04:17
[aoc2023] - 01b.mjs
import fs from "fs";
import readline from "readline";
const digitPattern = "([0-9]|one|two|three|four|five|six|seven|eight|nine)";
// Pattern capturing the first and last "digit" if a line has multiple
// matches.
const linePatternMultiple = new RegExp(`${digitPattern}.*${digitPattern}.*`);
// Pattern capturing the first occurrence of a "digit". Intended to be used
// as a fallback if the `linePatternMultiple` produces no match (because
@jjclxrk
jjclxrk / 01a.hs
Created December 2, 2023 04:09
[aoc2023] 01a.hs
import Data.Char (isDigit)
main = readFile "input.txt" >>= print . solve . parse
parse :: String -> [Int]
parse = map parseLine . lines
parseLine :: String -> Int
parseLine xs = read [firstDigit xs, firstDigit (reverse xs)]
where firstDigit = head . dropWhile (not . isDigit)
import Data.Time
import Data.Time.Clock.System
-- Function that potentially does a lot of unnecessary work in order to chew
-- up time.
f :: Integer -> Integer
f x = if x <= 0 then 0 else f (pred x)
-- The first guard condition will fail, and we are interested in the timing
-- to see if `f a` is computed again.
import Data.List (foldl')
import Data.Time
import Data.Time.Clock.System
main = do
bench "stats6" stats6 [1..10000000]
bench "stats5" stats5 [1..10000000]
bench "stats4" stats4 [1..10000000]
bench :: (Show b) => String -> ([a] -> b) -> [a] -> IO ()