day 1 part1
This commit is contained in:
parent
de33f3659a
commit
4b36732a0f
5 changed files with 1061 additions and 0 deletions
4
2024/day1/.gitignore
vendored
Normal file
4
2024/day1/.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
*.out
|
||||||
|
*.cmi
|
||||||
|
*.cmx
|
||||||
|
*.o
|
1
2024/day1/.ocamlformat
Normal file
1
2024/day1/.ocamlformat
Normal file
|
@ -0,0 +1 @@
|
||||||
|
profile = janestreet
|
19
2024/day1/Makefile
Normal file
19
2024/day1/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
CC = ocamlopt
|
||||||
|
RM = rm -rf
|
||||||
|
|
||||||
|
NAME = main
|
||||||
|
EXE = a.out
|
||||||
|
LIB = unix.cmxa -I +unix
|
||||||
|
|
||||||
|
|
||||||
|
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/day1/input.txt
Normal file
1000
2024/day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
37
2024/day1/main.ml
Normal file
37
2024/day1/main.ml
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
(** 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
|
||||||
|
(* Split column into couples *)
|
||||||
|
let scan line = Scanf.sscanf line "%d %d" (fun x y -> x, y) in
|
||||||
|
(* Split couple into 2 lists *)
|
||||||
|
let split (acc1, acc2) (a, b) = a :: acc1, b :: acc2 in
|
||||||
|
let zip = List.map scan (read_line []) in
|
||||||
|
List.fold_left split ([], []) zip
|
||||||
|
;;
|
||||||
|
|
||||||
|
let first a b =
|
||||||
|
let sorted_a, sorted_b = List.fast_sort compare a, List.fast_sort compare b in
|
||||||
|
let m = List.map2 (fun a b -> Int.abs (a - b)) sorted_a sorted_b in
|
||||||
|
List.fold_left ( + ) 0 m
|
||||||
|
;;
|
||||||
|
|
||||||
|
let second a b =
|
||||||
|
(* todo *)
|
||||||
|
0
|
||||||
|
;;
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
let a, b = from "input.txt" in
|
||||||
|
print_endline ("first: " ^ Int.to_string (first a b));
|
||||||
|
print_endline ("second: " ^ Int.to_string (second a b))
|
||||||
|
;;
|
Loading…
Reference in a new issue