simplify day 15 parsing with regex
This commit is contained in:
parent
b69f867423
commit
ff4bb0ae89
16
src/day15.ml
16
src/day15.ml
|
|
@ -13,17 +13,11 @@ let taxicab_dist a b =
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let parse_sensor line =
|
let parse_sensor line =
|
||||||
let open Utils.Parse in
|
match List.map int_of_string Re.(matches (Pcre.regexp {|-?\d+|}) line) with
|
||||||
let parse =
|
| [ sx; sy; bx; by ] ->
|
||||||
many1 (not_int *> int)
|
let pos, beacon = Vec2.(of_tuple (sx, sy), of_tuple (bx, by)) in
|
||||||
>>= function
|
{ pos; beacon; dist = taxicab_dist pos beacon }
|
||||||
| [ sx; sy; bx; by ] ->
|
| _ -> failwith "parse error"
|
||||||
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
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let example_lines =
|
let example_lines =
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user