2025: day3: ocaml
This commit is contained in:
3
AdventOfCode2025/day3/bin/dune
Normal file
3
AdventOfCode2025/day3/bin/dune
Normal file
@ -0,0 +1,3 @@
|
||||
(executable
|
||||
(public_name day3)
|
||||
(name main))
|
||||
86
AdventOfCode2025/day3/bin/main.ml
Normal file
86
AdventOfCode2025/day3/bin/main.ml
Normal file
@ -0,0 +1,86 @@
|
||||
let get_highest_number_and_index l =
|
||||
let (t, _) = List.fold_left (
|
||||
fun ((highest, highest_index), index) i ->
|
||||
if i > highest then
|
||||
((i, index), index + 1)
|
||||
else
|
||||
((highest, highest_index), index + 1)
|
||||
) ((0, 0), 0) (List.rev l) in
|
||||
t
|
||||
|
||||
let rec first_n k xs = match xs with
|
||||
| [] -> failwith "firstk"
|
||||
| x::xs -> if k=1 then [x] else x::first_n (k-1) xs
|
||||
|
||||
let slice_at_index l index =
|
||||
let rec loop (i, acc) =
|
||||
match acc with
|
||||
| [] -> failwith "too far"
|
||||
| _ :: tail -> if i == index then tail else loop (i + 1, tail)
|
||||
in
|
||||
loop (0, List.rev l)
|
||||
|
||||
let highest_n n l =
|
||||
let rec loop acc n l =
|
||||
match n with
|
||||
| 0 -> acc
|
||||
| n ->
|
||||
let list_first_n_items = first_n (List.length l - n + 1) (List.rev l) |> List.rev in
|
||||
let (highest, highest_index) = get_highest_number_and_index list_first_n_items in
|
||||
let remaining_list = slice_at_index l highest_index in
|
||||
loop (highest :: acc) (n - 1) (List.rev remaining_list)
|
||||
in
|
||||
loop [] n l
|
||||
|
||||
let string_to_int_list l =
|
||||
l
|
||||
|> String.to_seq
|
||||
|> List.of_seq
|
||||
|> List.fold_left (fun acc i -> ((int_of_char i) - (int_of_char '0')) :: acc) []
|
||||
|
||||
let num_from_int_list l =
|
||||
let rec loop acc index l =
|
||||
match l with
|
||||
| [] -> acc
|
||||
| head :: tail -> loop (acc + index * head) (index * 10) tail
|
||||
in
|
||||
loop 0 1 l
|
||||
|
||||
|
||||
|
||||
let solve lines =
|
||||
let nums_part1 = List.fold_left (
|
||||
fun acc line -> (line |> string_to_int_list |> (highest_n 2)) :: acc
|
||||
) [] lines in
|
||||
|
||||
let part1 = List.fold_left (+) 0 (List.map num_from_int_list nums_part1) in
|
||||
|
||||
let nums_part2 = List.fold_left (
|
||||
fun acc line -> (line |> string_to_int_list |> (highest_n 12)) :: acc
|
||||
) [] lines in
|
||||
|
||||
let part2 = List.fold_left (+) 0 (List.map num_from_int_list nums_part2) in
|
||||
|
||||
(part1, part2)
|
||||
|
||||
let read_lines name =
|
||||
let ic = open_in name in
|
||||
let try_read () = try Some (input_line ic) with End_of_file -> None in
|
||||
let rec loop acc =
|
||||
match try_read () with
|
||||
| Some s -> loop (s :: acc)
|
||||
| None ->
|
||||
close_in ic;
|
||||
List.rev acc
|
||||
in
|
||||
loop []
|
||||
|
||||
let () =
|
||||
print_endline "Day 3";
|
||||
let part1, part2 =
|
||||
read_lines "./input.txt" |> solve
|
||||
in
|
||||
Printf.printf "Part 1: %s\nPart 2: %s\n" (string_of_int part1)
|
||||
(string_of_int part2)
|
||||
|
||||
|
||||
32
AdventOfCode2025/day3/day3.opam
Normal file
32
AdventOfCode2025/day3/day3.opam
Normal file
@ -0,0 +1,32 @@
|
||||
# This file is generated by dune, edit dune-project instead
|
||||
opam-version: "2.0"
|
||||
synopsis: "A short synopsis"
|
||||
description: "A longer description"
|
||||
maintainer: ["Maintainer Name <maintainer@example.com>"]
|
||||
authors: ["Author Name <author@example.com>"]
|
||||
license: "LICENSE"
|
||||
tags: ["add topics" "to describe" "your" "project"]
|
||||
homepage: "https://github.com/username/reponame"
|
||||
doc: "https://url/to/documentation"
|
||||
bug-reports: "https://github.com/username/reponame/issues"
|
||||
depends: [
|
||||
"dune" {>= "3.20"}
|
||||
"ocaml"
|
||||
"odoc" {with-doc}
|
||||
]
|
||||
build: [
|
||||
["dune" "subst"] {dev}
|
||||
[
|
||||
"dune"
|
||||
"build"
|
||||
"-p"
|
||||
name
|
||||
"-j"
|
||||
jobs
|
||||
"@install"
|
||||
"@runtest" {with-test}
|
||||
"@doc" {with-doc}
|
||||
]
|
||||
]
|
||||
dev-repo: "git+https://github.com/username/reponame.git"
|
||||
x-maintenance-intent: ["(latest)"]
|
||||
26
AdventOfCode2025/day3/dune-project
Normal file
26
AdventOfCode2025/day3/dune-project
Normal file
@ -0,0 +1,26 @@
|
||||
(lang dune 3.20)
|
||||
|
||||
(name day3)
|
||||
|
||||
(generate_opam_files true)
|
||||
|
||||
(source
|
||||
(github username/reponame))
|
||||
|
||||
(authors "Author Name <author@example.com>")
|
||||
|
||||
(maintainers "Maintainer Name <maintainer@example.com>")
|
||||
|
||||
(license LICENSE)
|
||||
|
||||
(documentation https://url/to/documentation)
|
||||
|
||||
(package
|
||||
(name day3)
|
||||
(synopsis "A short synopsis")
|
||||
(description "A longer description")
|
||||
(depends ocaml)
|
||||
(tags
|
||||
("add topics" "to describe" your project)))
|
||||
|
||||
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/reference/dune-project/index.html
|
||||
Reference in New Issue
Block a user