summaryrefslogtreecommitdiff
path: root/2025/ocaml/lib/application/d01p02.ml
blob: 6f935a459447744b1cd1a3b5804ca317be38245b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
open Domain.D01p02

let rec input_to_lines input =
  let lines =
    String.split_on_char '\n' input |> List.filter (fun l -> l <> "")
  in
  pp_lines lines ; lines

and pp_lines = function
  | [] ->
      ()
  | h :: t ->
      Logs.debug (fun m -> m "Line=%s" h) ;
      pp_lines t

let parse_line line =
  Scanf.sscanf line "%c%d" (fun d n ->
      match d with
      | 'L' ->
          Dial.Left n
      | 'R' ->
          Dial.Right n
      | _ ->
          failwith "invalid direction" )

let lines_to_turns =
  List.map (fun (l : string) ->
      Logs.debug (fun m -> m "parsing %s into turn" l) ;
      let turn = parse_line l in
      Logs.debug (fun m -> m "%s" (Dial.pp_direction turn)) ;
      turn )

let rec turn_aux count dial = function
  | [] ->
      count
  | h :: t -> (
      let dial_and_count = Turner.turn dial count h in
      match dial_and_count with
      | dial', count' ->
          Logs.debug (fun m ->
              m "(dial=%d, count=%d)" (Dial.to_int dial') count' ) ;
          turn_aux count' dial' t )

let turn current = turn_aux 0 current

let solve input () =
  let lines = input_to_lines input in
  let turns = lines_to_turns lines in
  let start = Dial.start in
  let answer = turn start turns in
  answer