From db2686c798378738020a0951a521358586e21aa0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 9 Feb 2024 11:42:04 +0100 Subject: Add day2 in OCaml --- .../bin/.main.eobjs/byte/dune__exe__Main.cmi | Bin 0 -> 205 bytes .../bin/.main.eobjs/byte/dune__exe__Main.cmti | Bin 0 -> 2368 bytes .../bin/.main.eobjs/native/dune__exe__Main.cmx | Bin 0 -> 552 bytes .../bin/.main.eobjs/native/dune__exe__Main.o | Bin 0 -> 15200 bytes .../day2/_build/default/bin/.merlin-conf/exe-main | Bin 0 -> 620 bytes 2023/ocaml/day2/_build/default/bin/main.exe | Bin 0 -> 2254592 bytes 2023/ocaml/day2/_build/default/bin/main.ml | 39 +++++++++++++++++++++ 2023/ocaml/day2/_build/default/bin/main.mli | 1 + 8 files changed, 40 insertions(+) create mode 100644 2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmi create mode 100644 2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmti create mode 100644 2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.cmx create mode 100644 2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.o create mode 100644 2023/ocaml/day2/_build/default/bin/.merlin-conf/exe-main create mode 100755 2023/ocaml/day2/_build/default/bin/main.exe create mode 100644 2023/ocaml/day2/_build/default/bin/main.ml create mode 100644 2023/ocaml/day2/_build/default/bin/main.mli (limited to '2023/ocaml/day2/_build/default/bin') diff --git a/2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmi b/2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmi new file mode 100644 index 0000000..1886df9 Binary files /dev/null and b/2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmi differ diff --git a/2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmti b/2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmti new file mode 100644 index 0000000..87d60ff Binary files /dev/null and b/2023/ocaml/day2/_build/default/bin/.main.eobjs/byte/dune__exe__Main.cmti differ diff --git a/2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.cmx b/2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.cmx new file mode 100644 index 0000000..fdc4cf6 Binary files /dev/null and b/2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.cmx differ diff --git a/2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.o b/2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.o new file mode 100644 index 0000000..7bbc169 Binary files /dev/null and b/2023/ocaml/day2/_build/default/bin/.main.eobjs/native/dune__exe__Main.o differ diff --git a/2023/ocaml/day2/_build/default/bin/.merlin-conf/exe-main b/2023/ocaml/day2/_build/default/bin/.merlin-conf/exe-main new file mode 100644 index 0000000..647fe64 Binary files /dev/null and b/2023/ocaml/day2/_build/default/bin/.merlin-conf/exe-main differ diff --git a/2023/ocaml/day2/_build/default/bin/main.exe b/2023/ocaml/day2/_build/default/bin/main.exe new file mode 100755 index 0000000..1b3c926 Binary files /dev/null and b/2023/ocaml/day2/_build/default/bin/main.exe differ diff --git a/2023/ocaml/day2/_build/default/bin/main.ml b/2023/ocaml/day2/_build/default/bin/main.ml new file mode 100644 index 0000000..6dba854 --- /dev/null +++ b/2023/ocaml/day2/_build/default/bin/main.ml @@ -0,0 +1,39 @@ +let input_file = "input.txt" + +let read_file f = + let ic = open_in f in + try + let lines = really_input_string ic (in_channel_length ic) in + let lines = String.trim lines in + close_in ic ; + lines + with e -> + close_in_noerr ic; + raise e + +let rec parse ?(result=0) body = + match body with + | [] -> result + | x :: tail -> + let plays = String.split_on_char ':' x + |> List.rev + |> List.hd + |> String.split_on_char ';' + |> List.map (String.split_on_char ',') + |> List.map (List.map @@ String.trim) + |> List.map (List.map (fun x -> (List.hd (String.split_on_char ' ' x) |> int_of_string, String.get (List.nth (String.split_on_char ' ' x) 1 ) 0))) + in + + + let points = List.map (List.map (fun (p,c) -> if (c = 'r' && p > 12) || (c = 'g' && p > 13) || (c = 'b' && p > 14) then 0 else 1)) plays in + let add = if List.length (List.filter (fun x -> List.length x > 0) @@ List.map (List.filter (fun x -> x = 0)) points) = 0 then + String.split_on_char ':' x |> List.hd |> String.split_on_char ' ' |> List.rev |> List.hd |> int_of_string + else + 0 in + + parse tail ~result:(result+add) + + +let () = + let result = parse (String.split_on_char '\n' (read_file input_file)) in + Printf.printf "%d" result diff --git a/2023/ocaml/day2/_build/default/bin/main.mli b/2023/ocaml/day2/_build/default/bin/main.mli new file mode 100644 index 0000000..335ae1f --- /dev/null +++ b/2023/ocaml/day2/_build/default/bin/main.mli @@ -0,0 +1 @@ +(* Auto-generated by Dune *) \ No newline at end of file -- cgit v1.2.3-18-g5258