import birl import gleam/int import gleam/io import gleam/string import internal/llm/day01 as llm_day01 import internal/mine/day01 as mine_day01 pub type TimedResult { TimedResult(result: Int, microseconds: Int) } fn time_fn(f: fn() -> Int) -> TimedResult { let start = birl.now() |> birl.to_unix_micro let result = f() let end = birl.now() |> birl.to_unix_micro TimedResult(result: result, microseconds: end - start) } fn format_time(microseconds: Int) -> String { case microseconds { us if us < 1000 -> int.to_string(us) <> "µs" us if us < 1_000_000 -> { let ms = us / 1000 let remainder = us % 1000 int.to_string(ms) <> "." <> string.pad_start(int.to_string(remainder / 100), 1, "0") <> "ms" } us -> { let s = us / 1_000_000 let ms = { us % 1_000_000 } / 1000 int.to_string(s) <> "." <> string.pad_start(int.to_string(ms), 3, "0") <> "s" } } } fn print_comparison(part: String, mine: TimedResult, llm: TimedResult) -> Nil { io.println("") io.println("--- " <> part <> " ---") io.println( "Mine: " <> int.to_string(mine.result) <> " (" <> format_time(mine.microseconds) <> ")", ) io.println( "LLM: " <> int.to_string(llm.result) <> " (" <> format_time(llm.microseconds) <> ")", ) let match = case mine.result == llm.result { True -> "✓ Results match" False -> "✗ Results differ!" } io.println(match) let speedup = case mine.microseconds, llm.microseconds { 0, 0 -> "Both too fast to measure" m, _l if m == 0 -> "Mine too fast to measure" _m, l if l == 0 -> "LLM too fast to measure" m, l if m < l -> "Mine " <> format_speedup(l, m) <> " faster" m, l if l < m -> "LLM " <> format_speedup(m, l) <> " faster" _, _ -> "Same speed" } io.println(speedup) } fn format_speedup(slower: Int, faster: Int) -> String { let ratio = { slower * 100 } / faster let whole = ratio / 100 let frac = ratio % 100 int.to_string(whole) <> "." <> string.pad_start(int.to_string(frac), 2, "0") <> "x" } pub fn day01(input: List(String)) -> Nil { io.println("Comparing implementations...") let mine_p1 = time_fn(fn() { mine_day01.part1(input) }) let llm_p1 = time_fn(fn() { llm_day01.part1(input) }) print_comparison("Part 1", mine_p1, llm_p1) let mine_p2 = time_fn(fn() { mine_day01.part2(input) }) let llm_p2 = time_fn(fn() { llm_day01.part2(input) }) print_comparison("Part 2", mine_p2, llm_p2) }