parse day 15
This commit is contained in:
parent
724576d120
commit
dcb1338aa3
36
inputs/day15.tt
Normal file
36
inputs/day15.tt
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
Sensor at x=13820, y=3995710: closest beacon is at x=1532002, y=3577287
|
||||
Sensor at x=3286002, y=2959504: closest beacon is at x=3931431, y=2926694
|
||||
Sensor at x=3654160, y=2649422: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=3702414, y=2602790: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=375280, y=2377181: closest beacon is at x=2120140, y=2591883
|
||||
Sensor at x=3875726, y=2708666: closest beacon is at x=3931431, y=2926694
|
||||
Sensor at x=3786107, y=2547075: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=2334266, y=3754737: closest beacon is at x=2707879, y=3424224
|
||||
Sensor at x=1613400, y=1057722: closest beacon is at x=1686376, y=-104303
|
||||
Sensor at x=3305964, y=2380628: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=1744420, y=3927424: closest beacon is at x=1532002, y=3577287
|
||||
Sensor at x=3696849, y=2604845: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=2357787, y=401688: closest beacon is at x=1686376, y=-104303
|
||||
Sensor at x=2127900, y=1984887: closest beacon is at x=2332340, y=2000000
|
||||
Sensor at x=3705551, y=2604421: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=1783014, y=2978242: closest beacon is at x=2120140, y=2591883
|
||||
Sensor at x=2536648, y=2910642: closest beacon is at x=2707879, y=3424224
|
||||
Sensor at x=3999189, y=2989409: closest beacon is at x=3931431, y=2926694
|
||||
Sensor at x=3939169, y=2382534: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=2792378, y=2002602: closest beacon is at x=2332340, y=2000000
|
||||
Sensor at x=3520934, y=3617637: closest beacon is at x=2707879, y=3424224
|
||||
Sensor at x=2614525, y=1628105: closest beacon is at x=2332340, y=2000000
|
||||
Sensor at x=2828931, y=3996545: closest beacon is at x=2707879, y=3424224
|
||||
Sensor at x=2184699, y=2161391: closest beacon is at x=2332340, y=2000000
|
||||
Sensor at x=2272873, y=1816621: closest beacon is at x=2332340, y=2000000
|
||||
Sensor at x=1630899, y=3675405: closest beacon is at x=1532002, y=3577287
|
||||
Sensor at x=3683190, y=2619409: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=180960, y=185390: closest beacon is at x=187063, y=-1440697
|
||||
Sensor at x=1528472, y=3321640: closest beacon is at x=1532002, y=3577287
|
||||
Sensor at x=3993470, y=2905566: closest beacon is at x=3931431, y=2926694
|
||||
Sensor at x=1684313, y=20931: closest beacon is at x=1686376, y=-104303
|
||||
Sensor at x=2547761, y=2464195: closest beacon is at x=2120140, y=2591883
|
||||
Sensor at x=3711518, y=845968: closest beacon is at x=3702627, y=2598480
|
||||
Sensor at x=3925049, y=2897039: closest beacon is at x=3931431, y=2926694
|
||||
Sensor at x=1590740, y=3586256: closest beacon is at x=1532002, y=3577287
|
||||
Sensor at x=1033496, y=3762565: closest beacon is at x=1532002, y=3577287
|
||||
65
src/day15.ml
Normal file
65
src/day15.ml
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
open Containers
|
||||
|
||||
type sensor =
|
||||
{ pos : Vec2.t
|
||||
; beacon : Vec2.t
|
||||
; dist : int
|
||||
}
|
||||
[@@deriving show { with_path = false }]
|
||||
|
||||
let taxicab_dist a b =
|
||||
let x, y = Vec2.(abs (a - b) |> to_tuple) in
|
||||
x + y
|
||||
;;
|
||||
|
||||
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
|
||||
;;
|
||||
|
||||
let example_lines =
|
||||
[ "Sensor at x=2, y=18: closest beacon is at x=-2, y=15"
|
||||
; "Sensor at x=9, y=16: closest beacon is at x=10, y=16"
|
||||
; "Sensor at x=13, y=2: closest beacon is at x=15, y=3"
|
||||
; "Sensor at x=12, y=14: closest beacon is at x=10, y=16"
|
||||
; "Sensor at x=10, y=20: closest beacon is at x=10, y=16"
|
||||
; "Sensor at x=14, y=17: closest beacon is at x=10, y=16"
|
||||
; "Sensor at x=8, y=7: closest beacon is at x=2, y=10"
|
||||
; "Sensor at x=2, y=0: closest beacon is at x=2, y=10"
|
||||
; "Sensor at x=0, y=11: closest beacon is at x=2, y=10"
|
||||
; "Sensor at x=20, y=14: closest beacon is at x=25, y=17"
|
||||
; "Sensor at x=17, y=20: closest beacon is at x=21, y=22"
|
||||
; "Sensor at x=16, y=7: closest beacon is at x=15, y=3"
|
||||
; "Sensor at x=14, y=3: closest beacon is at x=15, y=3"
|
||||
; "Sensor at x=20, y=1: closest beacon is at x=15, y=3"
|
||||
]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 15 example" =
|
||||
example_lines |> List.map parse_sensor |> List.iter @@ Format.printf "%a@ " pp_sensor;
|
||||
[%expect
|
||||
{|
|
||||
{ pos = { Vec2.x = 2; y = 18 }; beacon = { Vec2.x = -2; y = 15 }; dist = 7 }
|
||||
{ pos = { Vec2.x = 9; y = 16 }; beacon = { Vec2.x = 10; y = 16 }; dist = 1 }
|
||||
{ pos = { Vec2.x = 13; y = 2 }; beacon = { Vec2.x = 15; y = 3 }; dist = 3 }
|
||||
{ pos = { Vec2.x = 12; y = 14 }; beacon = { Vec2.x = 10; y = 16 }; dist = 4 }
|
||||
{ pos = { Vec2.x = 10; y = 20 }; beacon = { Vec2.x = 10; y = 16 }; dist = 4 }
|
||||
{ pos = { Vec2.x = 14; y = 17 }; beacon = { Vec2.x = 10; y = 16 }; dist = 5 }
|
||||
{ pos = { Vec2.x = 8; y = 7 }; beacon = { Vec2.x = 2; y = 10 }; dist = 9 }
|
||||
{ pos = { Vec2.x = 2; y = 0 }; beacon = { Vec2.x = 2; y = 10 }; dist = 10 }
|
||||
{ pos = { Vec2.x = 0; y = 11 }; beacon = { Vec2.x = 2; y = 10 }; dist = 3 }
|
||||
{ pos = { Vec2.x = 20; y = 14 }; beacon = { Vec2.x = 25; y = 17 }; dist = 8 }
|
||||
{ pos = { Vec2.x = 17; y = 20 }; beacon = { Vec2.x = 21; y = 22 }; dist = 6 }
|
||||
{ pos = { Vec2.x = 16; y = 7 }; beacon = { Vec2.x = 15; y = 3 }; dist = 5 }
|
||||
{ pos = { Vec2.x = 14; y = 3 }; beacon = { Vec2.x = 15; y = 3 }; dist = 1 }
|
||||
{ pos = { Vec2.x = 20; y = 1 }; beacon = { Vec2.x = 15; y = 3 }; dist = 7 } |}]
|
||||
;;
|
||||
13
src/utils.ml
13
src/utils.ml
|
|
@ -13,11 +13,20 @@ let lines_of_input day =
|
|||
module Parse = struct
|
||||
include Angstrom
|
||||
|
||||
let int =
|
||||
let sign = option 1 (char '-' >>= fun _ -> return (-1))
|
||||
|
||||
let digits =
|
||||
take_while1 (function
|
||||
| '0' .. '9' -> true
|
||||
| _ -> false)
|
||||
>>| int_of_string
|
||||
<?> "int"
|
||||
;;
|
||||
|
||||
let int = map2 sign digits ~f:( * )
|
||||
|
||||
let not_int =
|
||||
skip_while (function
|
||||
| '0' .. '9' | '-' -> false
|
||||
| _ -> true)
|
||||
;;
|
||||
end
|
||||
|
|
|
|||
|
|
@ -17,3 +17,4 @@ let to_tuple { x; y } = x, y
|
|||
let ( + ) a b = { x = a.x + b.x; y = a.y + b.y }
|
||||
let ( - ) a b = { x = a.x - b.x; y = a.y - b.y }
|
||||
let ( = ) a b = a.x = b.x && a.y = b.y
|
||||
let abs a = Int.{ x = abs a.x; y = abs a.y }
|
||||
|
|
|
|||
|
|
@ -15,3 +15,4 @@ val to_tuple : t -> int * int
|
|||
val ( + ) : t -> t -> t
|
||||
val ( - ) : t -> t -> t
|
||||
val ( = ) : t -> t -> bool
|
||||
val abs : t -> t
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user