A day_10.ml => day_10.ml +41 -0
@@ 0,0 1,41 @@
+open Printf
+
+let bool_to_int b = match b with | true -> 1 | false -> 0
+
+let rec read_input chan =
+ match (try Some (input_line chan) with End_of_file -> None) with
+ | Some line -> int_of_string line :: read_input chan
+ | None -> []
+
+let rec count_diff lst =
+ match lst with
+ | f::s::tail ->
+ printf "%d %d -> %d\n" f s (s-f);
+ let (d1, d3) = count_diff (s::tail) in
+ (d1 + bool_to_int (s - f = 1), d3 + bool_to_int (s - f = 3))
+ | _ -> (1,1)
+
+let rec count_arr lst =
+ match lst with
+ | f::s::tail when s-f < 3 -> (count_arr (f::tail) + count_arr (s::tail))
+ | f::s::tail when s-f = 3 -> (count_arr (s::tail))
+ | [] -> 1
+ | _ -> 0
+
+let _ =
+ let in_chan = open_in "day_10.in" in
+ let lst = read_input in_chan |> List.sort compare in
+ close_in in_chan;
+
+ let (d1, d3)= lst |> count_diff in
+ let ways = lst |> count_arr in
+ printf "%d,%d,%d\n" ways d1 d3;
+
+ let out_chan1 = open_out "day_10_1.out" in
+ (d1 * d3) |> fprintf out_chan1 "%d\n";
+ close_out out_chan1;
+
+ let out_chan2 = open_out "day_10_2.out" in
+ ways |> Printf.fprintf out_chan2 "%d\n";
+ close_out out_chan2;
+ ()
A day_5.ml => day_5.ml +42 -0
@@ 0,0 1,42 @@
+open Printf
+
+let bool_to_int b = match b with | true -> 1 | false -> 0
+
+let rec read_input chan =
+ match (try Some (input_line chan) with End_of_file -> None) with
+ | Some line -> line :: read_input chan
+ | None -> []
+
+(* y u no bitshift ocaml? *)
+let rec pow a b = match b with 0 -> 1 | _ -> a * pow a (b-1)
+
+let to_number (str : string) =
+ let rec aux num =
+ match num with
+ | 10 -> 0
+ | _ ->
+ let chr = if num <= 6 then 'B' else 'R' in
+ (bool_to_int (str.[num] = chr) * pow 2 (9 - num)) + (aux (num + 1))
+ in
+ aux 0
+
+let rec missing l =
+ match l with
+ | head::next::_-> if head + 1 <> next then head + 1 else missing (List.tl l)
+ | _ -> failwith "err, wut?"
+
+let _ =
+ let in_chan = open_in "day_5.in" in
+ let lst = read_input in_chan in
+ close_in in_chan;
+
+ let numbers = lst |> List.map to_number |> List.sort compare in
+
+ let out_chan1 = open_out "day_5_1.out" in
+ numbers |> List.fold_left max 0 |> fprintf out_chan1 "%d\n";
+ close_out out_chan1;
+
+ let out_chan2 = open_out "day_5_2.out" in
+ numbers |> List.sort compare |> missing |> Printf.fprintf out_chan2 "%d\n";
+ close_out out_chan2;
+ ()
A day_6.ml => day_6.ml +40 -0
@@ 0,0 1,40 @@
+open Printf
+
+module S = Set.Make(Char)
+
+let rec rest_of_entry chan =
+ match (try (input_line chan) with End_of_file -> "") with
+ | "" -> []
+ | line -> line :: (rest_of_entry chan)
+
+let set_of_string (str: string) =
+ let (lst: char list) = (List.init (String.length str) (String.get str)) in
+ lst |> S.of_list
+
+let rec read_from chan =
+ match (try Some (input_line chan) with End_of_file -> None) with
+ | Some line ->
+ let ln = ((line :: rest_of_entry chan) |> List.map set_of_string) in
+ ln :: read_from chan
+ | _ -> []
+
+let _ =
+ let in_chan = open_in "day_6.in" in
+
+ let (lst: S.t list list) = read_from in_chan in
+ close_in in_chan;
+
+ let get_inter (sets: S.t list) = List.fold_left S.inter (List.hd sets) sets in
+
+ let union =
+ lst |> List.map (List.fold_left S.union S.empty) |> List.map S.cardinal in
+ let inter = lst |> List.map get_inter |> List.map S.cardinal in
+
+ let out_chan1 = open_out "day_6_1.out" in
+ union |> List.fold_left (+) 0 |> fprintf out_chan1 "%d\n";
+ close_out out_chan1;
+
+ let out_chan2 = open_out "day_6_2.out" in
+ inter |> List.fold_left (+) 0 |> fprintf out_chan2 "%d\n";
+ close_out out_chan2;
+ ()
A day_7.ml => day_7.ml +36 -0
@@ 0,0 1,36 @@
+open Printf
+
+let bool_to_int b = match b with | true -> 1 | false -> 0
+
+module Mp = Map.Make(String)
+
+let build_graph chan =
+ let rec read_input chan tr =
+ match (try Some (input_line chan) with End_of_file -> None) with
+ | Some line ->
+ let first::tail = line |> String.split_on_char ',' in
+ let (src, dest1) = split first in
+ read_input chan (Mp.add src (dest1::other_dest tail) tr)
+ | None -> tr
+ in
+ read_input chan Mp.empty
+
+
+
+let _ =
+ let in_chan = open_in "day_7.in" in
+ let g = build_graph in_chan in
+ close_in in_chan;
+
+ let numbers = lst |> List.map to_number |> List.sort compare in
+
+ let out_chan1 = open_out "day_7_1.out" in
+ numbers |> List.fold_left max 0 |> fprintf out_chan1 "%d\n";
+ close_out out_chan1;
+
+ (*
+ let out_chan2 = open_out "day_7_2.out" in
+ numbers |> List.sort compare |> missing |> Printf.fprintf out_chan2 "%d\n";
+ close_out out_chan2;
+ *)
+ ()
A day_9.ml => day_9.ml +64 -0
@@ 0,0 1,64 @@
+open Printf
+
+let bool_to_int b = match b with | true -> 1 | false -> 0
+
+let rec read_pre chan n =
+ if n = 25 then
+ []
+ else
+ let neki = input_line chan in
+ int_of_string neki :: read_pre chan (n+1)
+
+let rec read_list chan =
+ match try Some (input_line chan) with e -> None with
+ | Some line -> int_of_string line :: read_list chan
+ | None -> []
+
+let rec find_components pre lst =
+ match lst with
+ | [] -> failwith "wUt?"
+ | head :: tail ->
+ if List.exists (fun x -> List.mem (head - x) pre && x <> head) pre then
+ find_components (List.tl pre @ [head]) tail
+ else
+ head
+
+let rec find_cont lst v =
+ let rec slide pre lst =
+ match lst with
+ | [] -> None
+ | head::tail ->
+ printf "%d\n" head;
+ if List.fold_left (+) 0 pre = v then
+ Some (List.fold_left min (List.hd pre) pre + List.fold_left max 0 pre)
+ else
+ slide (List.tl pre @ [head]) tail
+ in
+ let rec inc_len l =
+ if List.length lst <= l then
+ failwith "waat"
+ else
+ let pre = List.init l (List.nth lst) in
+ match slide pre lst with
+ | None -> inc_len (l+1)
+ | Some num -> printf "lllll %d\n" l; num
+ in
+ inc_len 2
+
+let _ =
+ let in_chan = open_in "day_9.in" in
+ let pre = read_pre in_chan 0 in
+ let lst = read_list in_chan in
+ close_in in_chan;
+
+ let val1 = find_components pre lst in
+ let val2 = find_cont lst val1 in
+
+ let out_chan1 = open_out "day_9_1.out" in
+ val1 |> fprintf out_chan1 "%d\n";
+ close_out out_chan1;
+
+ let out_chan2 = open_out "day_9_2.out" in
+ val2 |> Printf.fprintf out_chan2 "%d\n";
+ close_out out_chan2;
+ ()