This commit is contained in:
2024-12-13 23:57:48 +01:00
parent fa3e6d8d88
commit 66acbc2986

55
13/main.ml Normal file
View File

@@ -0,0 +1,55 @@
open Printf;;
open Buffer;;
open Str;;
let read_whole_file f =
let s = really_input_string f (in_channel_length f) in
close_in f;
s
let rec find_all_numbers pos str =
try
let re = Str.regexp "[0-9]+" in
let new_pos = Str.search_forward re str pos in
let n_str = Str.matched_group 0 str in
let n = int_of_string n_str in
n :: find_all_numbers (new_pos + String.length n_str) str
with Not_found -> []
let rec to_sextuples lst =
match lst with
| n1 :: n2 :: n3 :: n4 :: n5 :: n6 :: tail ->
(n1, n2, n3, n4, n5, n6) :: to_sextuples tail
| _ -> []
let rec get_cost (ax, ay, bx, by, cx, cy) =
let det = ax * by - ay * bx in
if det = 0 then
raise (Invalid_argument "Linearly dependent")
else
let det_a = cx * by - cy * bx in
let det_b = ax * cy - ay * cx in
if det_a mod det = 0 && det_b mod det = 0 then
(det_a / det) * 3 + (det_b / det)
else
0
let correct_position (ax, ay, bx, by, cx, cy) =
(ax, ay, bx, by, cx + 10000000000000, cy + 10000000000000)
let () =
let f = open_in "input.txt" in
let numbers = f
|> read_whole_file
|> find_all_numbers 0
|> to_sextuples in
let result = numbers
|> List.map get_cost
|> List.fold_left Int.add 0 in
let result2 = numbers
|> List.map correct_position
|> List.map get_cost
|> List.fold_left Int.add 0 in
printf "%d\n%d\n" result result2