solve_1.ml 619 B

1234567891011121314151617181920212223
  1. open Stdio
  2. open Str
  3. let parse_mult_instruction line=
  4. let r = regexp {|mul( *\([0-9]+\) *, *\([0-9]+\) *)|} in
  5. let rec aux pos accum=
  6. try
  7. let _ = search_forward r line pos in
  8. let x = float_of_string (matched_group 1 line) in
  9. let y = float_of_string (matched_group 2 line) in
  10. aux (match_end ()) (accum + int_of_float (x *. y))
  11. with Not_found -> accum
  12. in
  13. aux 0 0
  14. ;;
  15. let rec solve accum =
  16. let line = In_channel.input_line In_channel.stdin in
  17. match line with
  18. | None -> accum
  19. | Some x -> solve (accum + parse_mult_instruction x)
  20. ;;
  21. let () = printf "Total: %d\n" (solve 0)