replace parse_line with CC map_same and take_drop
thank u https://doc.sherlocode.com
This commit is contained in:
1 parent
6a276c3c1b
commit
0a24022629
1 file changed
+8
-14
+8
-14
@@ -10,22 +10,16 @@ let priority char =
|
|||||||
| _ -> failwith @@ Printf.sprintf "'%c' is not a valid item" char
|
| _ -> failwith @@ Printf.sprintf "'%c' is not a valid item" char
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|
||||||
let parse_line line =
|
|
||||||
let half_len = String.length line / 2 in
|
|
||||||
let first_half = String.sub line 0 half_len in
|
|
||||||
let second_half = String.sub line half_len half_len in
|
|
||||||
set_of_string first_half, set_of_string second_half
|
|
||||||
;;
|
|
||||||
|
|
||||||
let part1 lines =
|
let part1 lines =
|
||||||
List.fold_left
|
List.fold_left
|
||||||
(fun acc line ->
|
(fun acc line ->
|
||||||
let first, second = parse_line line in
|
let half_len = String.length line / 2 in
|
||||||
|
let first, second = Pair.map_same set_of_string (String.take_drop half_len line) in
|
||||||
let item =
|
let item =
|
||||||
match CharSet.elements @@ CharSet.inter first second with
|
match CharSet.elements @@ CharSet.inter first second with
|
||||||
| [ item ] -> item
|
| [ item ] -> item
|
||||||
| _ -> failwith "sad"
|
| _ ->
|
||||||
|
failwith "first and second half of each line should only have 1 intersection"
|
||||||
in
|
in
|
||||||
acc + priority item)
|
acc + priority item)
|
||||||
0
|
0
|
||||||
@@ -33,9 +27,9 @@ let part1 lines =
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
(*
|
(*
|
||||||
x thank you https://doc.sherlocode.com/ for helping me find Containers and its
|
shout out https://doc.sherlocode.com/ for helping me find Containers and its
|
||||||
x List.chunks and List.reduce functions
|
List.chunks and List.reduce functions
|
||||||
x*)
|
*)
|
||||||
let part2 lines =
|
let part2 lines =
|
||||||
let groups = List.chunks 3 (List.map set_of_string lines) in
|
let groups = List.chunks 3 (List.map set_of_string lines) in
|
||||||
List.fold_left
|
List.fold_left
|
||||||
@@ -43,7 +37,7 @@ let part2 lines =
|
|||||||
let item =
|
let item =
|
||||||
match CharSet.elements @@ List.reduce_exn CharSet.inter group with
|
match CharSet.elements @@ List.reduce_exn CharSet.inter group with
|
||||||
| [ item ] -> item
|
| [ item ] -> item
|
||||||
| _ -> failwith "sad"
|
| _ -> failwith "each group of 3 lines should only have 1 intersection"
|
||||||
in
|
in
|
||||||
acc + priority item)
|
acc + priority item)
|
||||||
0
|
0
|
||||||
|
|||||||
Reference in new issue
Block a user