import argv import compare import gleam/int import gleam/io import gleam/list import gleam/option.{type Option, None, Some} import gleam/result import gleam/string import internal/mine/day01 as mine_day01 import utils const available_days = [1] pub fn main() -> Nil { let args = argv.load().arguments case parse_args(args) { Error(msg) -> io.println("Error: " <> msg) Ok(#(day, example, cmp)) -> case day { Some(d) -> run_day(d, example, cmp) None -> list.each(available_days, fn(d) { run_day(d, example, cmp) }) } } } pub type Args { Args(day: Option(Int), example: Bool, compare: Bool) } fn parse_args( args: List(String), ) -> Result(#(Option(Int), Bool, Bool), String) { let example = list.contains(args, "--example") || list.contains(args, "-e") let no_cmp = list.contains(args, "--no-compare") || list.contains(args, "-n") let day_args = list.filter(args, fn(a) { !string.starts_with(a, "-") }) use day <- result.try(case day_args { [day_str, ..] -> case int.parse(day_str) { Ok(d) if d >= 1 && d <= 25 -> Ok(Some(d)) Ok(_) -> Error("day must be between 1 and 25") Error(_) -> Error("invalid day number: " <> day_str) } [] -> Ok(None) }) Ok(#(day, example, !no_cmp)) } fn run_day(day: Int, example: Bool, cmp: Bool) -> Nil { case utils.read_input(day, example) { Error(e) -> io.println(e) Ok(input) -> { let mode = case example { True -> " (example)" False -> "" } io.println("=== Day " <> int.to_string(day) <> mode <> " ===") case cmp { True -> run_comparison(day, input) False -> run_parts(day, input) } } } } fn run_parts(day: Int, input: List(String)) -> Nil { case day { 1 -> { io.println("Part 1: " <> int.to_string(mine_day01.part1(input))) io.println("Part 2: " <> int.to_string(mine_day01.part2(input))) } _ -> io.println("Day " <> int.to_string(day) <> " not implemented yet") } } fn run_comparison(day: Int, input: List(String)) -> Nil { case day { 1 -> compare.day01(input) _ -> io.println("Day " <> int.to_string(day) <> " comparison not available") } }