This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fs from "fs"; | |
| import readline from "readline"; | |
| const fileStream = fs.createReadStream("input.txt"); | |
| const rl = readline.createInterface({ | |
| input: fileStream, | |
| crlfDelay: Infinity, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 () |