-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ml
65 lines (54 loc) · 2.04 KB
/
part1.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open Utils
let day = D13
(*****************************************************************************)
(* SOLUTION *)
(*****************************************************************************)
type coords = { x : int; y : int }
type crane_game = { button_a : coords; button_b : coords; prize : coords }
let coefficients { button_a; button_b; prize } =
let a =
((prize.x * button_b.y) - (button_b.x * prize.y))
/ ((button_a.x * button_b.y) - (button_b.x * button_a.y))
in
let b =
((button_a.x * prize.y) - (prize.x * button_a.y))
/ ((button_a.x * button_b.y) - (button_b.x * button_a.y))
in
if
(a * button_a.x) + (b * button_b.x) = prize.x
&& (a * button_a.y) + (b * button_b.y) = prize.y
then Some (a, b)
else None
let solve input : string =
List.filter_map coefficients input
|> List.map (fun (a, b) -> (3 * a) + b)
|> sum |> string_of_int
(*****************************************************************************)
(* INPUT PROCESSING *)
(*****************************************************************************)
let button_of_string s =
let s = split_on_chars [ '+'; ',' ] s in
let x = int_of_string @@ List.nth s 1 in
let y = int_of_string @@ List.nth s 3 in
{ x; y }
let prize_of_string s =
let s = split_on_chars [ ','; '=' ] s in
let x = int_of_string @@ List.nth s 1 in
let y = int_of_string @@ List.nth s 3 in
{ x; y }
let rec cranes lines : crane_game list =
match lines with
| [] -> []
| "" :: tl -> cranes tl
| a :: b :: prize :: tl ->
let button_a = button_of_string a in
let button_b = button_of_string b in
let prize = prize_of_string prize in
{ button_a; button_b; prize } :: cranes tl
| _ -> assert false
let parse (lines : string list) = cranes lines
(* Main function to read input and run the solution *)
let () =
test1 day parse solve "480";
let solution = get_input day Input |> parse |> solve in
print_endline solution