day 13
This commit is contained in:
55
13/main.ml
Normal file
55
13/main.ml
Normal 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
|
||||
|
||||
|
Reference in New Issue
Block a user