Skip to content

Instantly share code, notes, and snippets.

@p1xelHer0
Created December 5, 2023 07:41
Show Gist options
  • Save p1xelHer0/a01b3acf45d77181e4f4fc7c565561a0 to your computer and use it in GitHub Desktop.
Save p1xelHer0/a01b3acf45d77181e4f4fc7c565561a0 to your computer and use it in GitHub Desktop.
aoc.4.1.ml
module Card = struct
type t = { id : int; wins : int list; mine : int list }
[@@deriving show { with_path = false }, eq]
end
module P = struct
open Angstrom
let is_digit = function '0' .. '9' -> true | _ -> false
let int = take_while1 is_digit >>| Int.of_string_exn
let is_whitespace = function ' ' -> true | _ -> false
let whitespace = take_while is_whitespace
let card_id = string "Card" *> whitespace *> int <* string ":" <* whitespace
let score = sep_by whitespace int
let numbers = sep_by (whitespace *> string "|" <* whitespace) score
let game =
let* id = card_id in
let+ numbers = numbers in
match numbers with
| [ wins; mine ] -> Card.{ id; wins; mine }
| _ -> failwith "invalid input - or you suck at parsing :)"
let parse input =
parse_string ~consume:All game input |> Result.get_or_failwith
end
let wins Card.{ wins; mine; _ } =
mine |> List.filter ~f:(fun m -> List.mem m wins)
module Part_1 = struct
let calc_score card =
card |> wins |> List.length |> fun l ->
if l = 0 then 0 else Int.pow 2 (pred l)
let solve input =
let games = input |> String.lines |> List.map ~f:P.parse in
games |> List.map ~f:calc_score |> Util.sum
let%test "sample data" = Test.(run int (solve sample) ~expect:13)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment