tighten up day 13
List.compare is neat!
This commit is contained in:
1 parent
fa8970b0de
commit
06f695b34c
1 file changed
+18
-40
+18
-40
@@ -11,33 +11,25 @@ let packet_of_str line =
|
|||||||
take_while1 (function
|
take_while1 (function
|
||||||
| '0' .. '9' -> true
|
| '0' .. '9' -> true
|
||||||
| _ -> false)
|
| _ -> false)
|
||||||
>>= fun s -> return (I (int_of_string s)) <?> "int"
|
>>= fun s -> return @@ I (int_of_string s) <?> "int"
|
||||||
in
|
in
|
||||||
let packet =
|
let packet =
|
||||||
fix (fun packet ->
|
fix (fun packet ->
|
||||||
let list =
|
let lb, rb = char '[', char ']' in
|
||||||
char '[' *> sep_by (char ',') packet
|
let delim = sep_by (char ',') in
|
||||||
<* char ']'
|
let list = lb *> delim packet <* rb >>= fun p -> return @@ L p <?> "list" in
|
||||||
>>= fun s -> return (L s) <?> "list"
|
|
||||||
in
|
|
||||||
choice [ int; list ])
|
choice [ int; list ])
|
||||||
<?> "packet"
|
<?> "packet"
|
||||||
in
|
in
|
||||||
Result.get_or_failwith @@ parse_string ~consume:All packet line
|
Result.get_or_failwith @@ parse_string ~consume:All packet line
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let rec is_ordered = function
|
let rec is_ordered a b =
|
||||||
| I a, I b when a = b -> None
|
match a, b with
|
||||||
| I a, I b -> Some (a < b)
|
| I a, I b -> Int.compare a b
|
||||||
| (I _ as a), (L _ as b) -> is_ordered (L [ a ], b)
|
| L a, L b -> List.compare is_ordered a b
|
||||||
| (L _ as a), (I _ as b) -> is_ordered (a, L [ b ])
|
| (I _ as a), (L _ as b) -> is_ordered (L [ a ]) b
|
||||||
| L [], L [] -> None
|
| (L _ as a), (I _ as b) -> is_ordered a (L [ b ])
|
||||||
| L [], _ -> Some true
|
|
||||||
| _, L [] -> Some false
|
|
||||||
| L (a :: a_tl), L (b :: b_tl) ->
|
|
||||||
(match is_ordered (a, b) with
|
|
||||||
| None -> is_ordered (L a_tl, L b_tl)
|
|
||||||
| result -> result)
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let%expect_test "Day 13.1" =
|
let%expect_test "Day 13.1" =
|
||||||
@@ -48,14 +40,9 @@ let%expect_test "Day 13.1" =
|
|||||||
|> List.chunks 2
|
|> List.chunks 2
|
||||||
|> List.mapi (fun i packets ->
|
|> List.mapi (fun i packets ->
|
||||||
match packets with
|
match packets with
|
||||||
| [ a; b ] -> i + 1, is_ordered (a, b)
|
| [ a; b ] when is_ordered a b < 0 -> i + 1
|
||||||
| _ -> failwith "List.chunks didnt work lol")
|
| _ -> 0)
|
||||||
|> List.fold_left
|
|> List.reduce_exn ( + )
|
||||||
(fun result item ->
|
|
||||||
match item with
|
|
||||||
| i, Some true -> result + i
|
|
||||||
| _, _ -> result)
|
|
||||||
0
|
|
||||||
in
|
in
|
||||||
Printf.printf "%i" result;
|
Printf.printf "%i" result;
|
||||||
[%expect {| 6272 |}]
|
[%expect {| 6272 |}]
|
||||||
@@ -65,20 +52,11 @@ let%expect_test "Day 13.2" =
|
|||||||
let result =
|
let result =
|
||||||
Utils.lines_of_input 13
|
Utils.lines_of_input 13
|
||||||
|> List.filter Fun.(not % String.is_empty)
|
|> List.filter Fun.(not % String.is_empty)
|
||||||
|> List.append [ "[[2]]"; "[[6]]" ]
|
|> List.map (fun line -> false, packet_of_str line)
|
||||||
|> List.map (fun line -> line, packet_of_str line)
|
|> List.append [ true, packet_of_str "[[2]]"; true, packet_of_str "[[6]]" ]
|
||||||
|> List.sort (fun (_, a) (_, b) ->
|
|> List.sort (fun (_, a) (_, b) -> is_ordered a b)
|
||||||
match is_ordered (a, b) with
|
|> List.mapi (fun i (is_divider, _) -> if is_divider then i + 1 else 1)
|
||||||
| None -> 0
|
|> List.reduce_exn ( * )
|
||||||
| Some true -> -1
|
|
||||||
| Some false -> 1)
|
|
||||||
|> List.mapi (fun i (line, _) -> i + 1, line)
|
|
||||||
|> List.fold_left
|
|
||||||
(fun result (i, line) ->
|
|
||||||
match line with
|
|
||||||
| "[[2]]" | "[[6]]" -> result * i
|
|
||||||
| _ -> result)
|
|
||||||
1
|
|
||||||
in
|
in
|
||||||
Printf.printf "%i" result;
|
Printf.printf "%i" result;
|
||||||
[%expect {| 22288 |}]
|
[%expect {| 22288 |}]
|
||||||
|
|||||||
Reference in new issue
Block a user