attempt for day2

This commit is contained in:
Mylloon 2024-12-04 22:32:49 +01:00
parent bd8a0bc9e8
commit e101e5550e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 1070 additions and 1 deletions

View file

@ -1 +0,0 @@
profile = janestreet

1
2024/day1/.ocamlformat Symbolic link
View file

@ -0,0 +1 @@
../.ocamlformat

4
2024/day2/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.out
*.cmi
*.cmx
*.o

1
2024/day2/.ocamlformat Normal file
View file

@ -0,0 +1 @@
profile = janestreet

19
2024/day2/Makefile Normal file
View file

@ -0,0 +1,19 @@
CC = ocamlopt
RM = rm -rf
NAME = main
EXE = a.out
LIB =
all: run
compilation:
$(CC) -o $(EXE) $(LIB) $(NAME).ml
run: compilation
./$(EXE)
EXTS = cmi cmx o
clean:
$(RM) $(EXE) $(foreach ext,$(EXTS),$(NAME).$(ext))

1000
2024/day2/input.txt Normal file

File diff suppressed because it is too large Load diff

45
2024/day2/main.ml Normal file
View file

@ -0,0 +1,45 @@
(** Get input *)
let from input =
let ic = open_in input in
(* Read a file *)
let rec read_line acc =
try
let line = input_line ic in
read_line (line :: acc)
with
| End_of_file ->
close_in ic;
acc
in
(* Lis une ligne et la découpe proprement *)
let scan line = String.split_on_char ' ' line in
(* Convertis les string en nombre *)
let convert l = List.map int_of_string l in
let list = List.map scan (read_line []) in
List.map convert list |> List.rev
;;
let first data =
let rec is_safe_adj = function
| x :: y :: tl -> if Int.abs (x - y) > 2 then true else is_safe_adj (y :: tl)
| _ :: [] | [] -> false
in
let is_safe_order l =
let sorted = List.fast_sort compare l in
List.equal (fun a b -> a = b) l sorted
|| List.equal (fun a b -> a = b) l (List.rev sorted)
in
let res = List.filter (fun x -> is_safe_order x && is_safe_adj x) data in
List.length res
;;
let second data =
(* todo *)
0
;;
let _ =
let data = from "input.txt" in
print_endline ("first: " ^ Int.to_string (first data));
print_endline ("second: " ^ Int.to_string (second data))
;;