simplify day 15 parsing with regex

This commit is contained in:
ryan 2023-11-20 14:26:01 -08:00
parent b69f867423
commit ff4bb0ae89

View File

@ -13,17 +13,11 @@ let taxicab_dist a b =
;;
let parse_sensor line =
let open Utils.Parse in
let parse =
many1 (not_int *> int)
>>= function
| [ sx; sy; bx; by ] ->
let pos = Vec2.of_tuple (sx, sy) in
let beacon = Vec2.of_tuple (bx, by) in
return { pos; beacon; dist = taxicab_dist pos beacon }
| _ -> failwith "should be four ints"
in
Result.get_or_failwith @@ parse_string ~consume:All parse line
match List.map int_of_string Re.(matches (Pcre.regexp {|-?\d+|}) line) with
| [ sx; sy; bx; by ] ->
let pos, beacon = Vec2.(of_tuple (sx, sy), of_tuple (bx, by)) in
{ pos; beacon; dist = taxicab_dist pos beacon }
| _ -> failwith "parse error"
;;
let example_lines =