Compare commits
2 Commits
3ed91e2424
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3396a87262 | |||
| 12a56a01f3 |
4
AdventOfCode2025/day5/bin/dune
Normal file
4
AdventOfCode2025/day5/bin/dune
Normal file
@ -0,0 +1,4 @@
|
||||
(executable
|
||||
(public_name day5)
|
||||
(name main)
|
||||
(libraries day5))
|
||||
144
AdventOfCode2025/day5/bin/main.ml
Normal file
144
AdventOfCode2025/day5/bin/main.ml
Normal file
@ -0,0 +1,144 @@
|
||||
let get_range str =
|
||||
let list = (String.split_on_char '-' str)
|
||||
|> List.map int_of_string in
|
||||
|
||||
assert (List.length list == 2);
|
||||
|
||||
let lower = List.nth list 0 in
|
||||
let upper = List.nth list 1 in
|
||||
|
||||
(lower, upper)
|
||||
|
||||
let is_item_in_range i (lower, upper) =
|
||||
lower <= i && i <= upper
|
||||
|
||||
let rec list_some l fn =
|
||||
match l with
|
||||
| [] -> false
|
||||
| head :: tail -> if fn head then true else list_some tail fn
|
||||
|
||||
let is_item_in_ranges ranges i =
|
||||
list_some ranges (is_item_in_range i)
|
||||
|
||||
|
||||
let is_overlapping (lower1, upper1) (lower2, upper2) =
|
||||
lower1 <= lower2 && lower2 <= upper1 ||
|
||||
lower2 <= lower1 && lower1 <= upper2
|
||||
|
||||
let get_combined_ranges (lower1, upper1) (lower2, upper2) =
|
||||
let lower = min lower1 lower2 in
|
||||
let upper = max upper1 upper2 in
|
||||
(lower, upper)
|
||||
|
||||
let non_overlapping_ranges_work ranges =
|
||||
let rec loop acc changes ranges =
|
||||
match ranges with
|
||||
| [] -> (acc, changes)
|
||||
| head :: tail ->
|
||||
let overlap_fn = is_overlapping head in
|
||||
let overlapping_index = List.find_index overlap_fn tail in
|
||||
match overlapping_index with
|
||||
| Some r ->
|
||||
let (l, u) = List.nth tail r in
|
||||
let tail_without_index = List.filter
|
||||
(fun (lower, upper) -> not (lower == l && upper == u)) tail in
|
||||
let combined_range = get_combined_ranges head (l, u) in
|
||||
loop (combined_range :: acc) true (tail_without_index)
|
||||
| None -> loop (head :: acc) changes tail
|
||||
in
|
||||
loop [] false ranges
|
||||
|
||||
(* let rec print_ranges l = *)
|
||||
(* match l with *)
|
||||
(* | [] -> print_endline ""; *)
|
||||
(* | (lower, upper) :: tail -> *)
|
||||
(* Printf.printf "lower=%d,upper=%d\n" lower upper; *)
|
||||
(* print_ranges tail *)
|
||||
|
||||
|
||||
let rec non_overlapping_ranges ranges =
|
||||
let (ranges, changes) = non_overlapping_ranges_work ranges in
|
||||
if changes then non_overlapping_ranges ranges
|
||||
else ranges
|
||||
|
||||
|
||||
let solve lines =
|
||||
let no_empty_lines = List.filter (fun i -> String.length i > 0) lines in
|
||||
let (ranges, items) = List.partition (fun i -> String.contains i '-') no_empty_lines in
|
||||
|
||||
let ranges = List.map get_range ranges in
|
||||
let items = List.map int_of_string items in
|
||||
|
||||
let part1 = List.fold_left (
|
||||
fun acc i -> if is_item_in_ranges ranges i then acc + 1 else acc
|
||||
) 0 items in
|
||||
|
||||
let ranges = non_overlapping_ranges ranges in
|
||||
|
||||
let part2 = List.fold_left (
|
||||
fun acc (lower, upper) -> acc + (upper - lower + 1)
|
||||
) 0 ranges 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 5";
|
||||
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/day5/day5.opam
Normal file
32
AdventOfCode2025/day5/day5.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/day5/dune-project
Normal file
26
AdventOfCode2025/day5/dune-project
Normal file
@ -0,0 +1,26 @@
|
||||
(lang dune 3.20)
|
||||
|
||||
(name day5)
|
||||
|
||||
(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 day5)
|
||||
(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
|
||||
2
AdventOfCode2025/day5/lib/dune
Normal file
2
AdventOfCode2025/day5/lib/dune
Normal file
@ -0,0 +1,2 @@
|
||||
(library
|
||||
(name day5))
|
||||
2
AdventOfCode2025/day5/test/dune
Normal file
2
AdventOfCode2025/day5/test/dune
Normal file
@ -0,0 +1,2 @@
|
||||
(test
|
||||
(name test_day5))
|
||||
0
AdventOfCode2025/day5/test/test_day5.ml
Normal file
0
AdventOfCode2025/day5/test/test_day5.ml
Normal file
34
AdventOfCode2025/day6/.gitignore
vendored
Normal file
34
AdventOfCode2025/day6/.gitignore
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
# dependencies (bun install)
|
||||
node_modules
|
||||
|
||||
# output
|
||||
out
|
||||
dist
|
||||
*.tgz
|
||||
|
||||
# code coverage
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# logs
|
||||
logs
|
||||
_.log
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# caches
|
||||
.eslintcache
|
||||
.cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
15
AdventOfCode2025/day6/README.md
Normal file
15
AdventOfCode2025/day6/README.md
Normal file
@ -0,0 +1,15 @@
|
||||
# day6
|
||||
|
||||
To install dependencies:
|
||||
|
||||
```bash
|
||||
bun install
|
||||
```
|
||||
|
||||
To run:
|
||||
|
||||
```bash
|
||||
bun run index.ts
|
||||
```
|
||||
|
||||
This project was created using `bun init` in bun v1.2.8. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
||||
25
AdventOfCode2025/day6/bun.lock
Normal file
25
AdventOfCode2025/day6/bun.lock
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"lockfileVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "day6",
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"@types/bun": ["@types/bun@1.3.3", "", { "dependencies": { "bun-types": "1.3.3" } }, "sha512-ogrKbJ2X5N0kWLLFKeytG0eHDleBYtngtlbu9cyBKFtNL3cnpDZkNdQj8flVf6WTZUX5ulI9AY1oa7ljhSrp+g=="],
|
||||
|
||||
"@types/node": ["@types/node@24.10.1", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="],
|
||||
|
||||
"bun-types": ["bun-types@1.3.3", "", { "dependencies": { "@types/node": "*" } }, "sha512-z3Xwlg7j2l9JY27x5Qn3Wlyos8YAp0kKRlrePAOjgjMGS5IG6E7Jnlx736vH9UVI4wUICwwhC9anYL++XeOgTQ=="],
|
||||
|
||||
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
||||
|
||||
"undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
|
||||
}
|
||||
}
|
||||
116
AdventOfCode2025/day6/index.ts
Normal file
116
AdventOfCode2025/day6/index.ts
Normal file
@ -0,0 +1,116 @@
|
||||
const file = Bun.file("./input.txt");
|
||||
const content = await file.text().then((c) => c.trim());
|
||||
|
||||
const filterNonEmpty = (i: string) => i.length > 0
|
||||
|
||||
type Problem = {
|
||||
operation: string
|
||||
numbers: number[]
|
||||
}
|
||||
|
||||
const problems: Problem[] = [];
|
||||
|
||||
const splitContent = content.split("\n")
|
||||
const operationsRow = splitContent.pop()!.split(" ").filter(filterNonEmpty)
|
||||
|
||||
for (const line of splitContent) {
|
||||
const numbers = line.split(" ").filter(filterNonEmpty);
|
||||
if (problems.length === 0) {
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
problems.push({
|
||||
operation: '',
|
||||
numbers: [],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
problems[i]!.numbers.push(parseInt(numbers[i]!))
|
||||
}
|
||||
}
|
||||
|
||||
for (const [index, problem] of problems.entries()) {
|
||||
problem.operation = operationsRow[index]!
|
||||
}
|
||||
|
||||
const getReducer = (op: string): (nums: number[]) => number => {
|
||||
switch (op) {
|
||||
case '*':
|
||||
return (nums) => nums.reduce((n, acc) => acc * n, 1)
|
||||
case '+':
|
||||
return (nums) => nums.reduce((n, acc) => acc + n, 0)
|
||||
default:
|
||||
throw new Error("operation not supported")
|
||||
}
|
||||
}
|
||||
|
||||
const solve = (problems: Problem[]): number => {
|
||||
let part1 = 0;
|
||||
|
||||
for (const problem of problems) {
|
||||
const reducer = getReducer(problem.operation)
|
||||
part1 += reducer(problem.numbers)
|
||||
}
|
||||
|
||||
return part1
|
||||
}
|
||||
|
||||
const transposeProblems = (str: string): Problem[] => {
|
||||
const charArr = str.split("\n").map(c => c.split(""))
|
||||
|
||||
const newCharArr: string[][] = [];
|
||||
|
||||
for (let i = 0; i < charArr.length; i++) {
|
||||
for (let j = 0; j < charArr[0]!.length; j++) {
|
||||
if (!newCharArr[j]) {
|
||||
newCharArr[j] = []
|
||||
}
|
||||
newCharArr[j]![i]! = charArr[i]![j]! ?? ' '
|
||||
}
|
||||
}
|
||||
|
||||
const problems: Problem[] = [];
|
||||
let workingProblem: Problem = {
|
||||
numbers: [],
|
||||
operation: ""
|
||||
}
|
||||
let index = 0;
|
||||
|
||||
for (const line of newCharArr) {
|
||||
if (line.every(c => c === " ")) {
|
||||
problems.push(workingProblem)
|
||||
workingProblem = {
|
||||
numbers: [],
|
||||
operation: ''
|
||||
}
|
||||
index = 0
|
||||
continue
|
||||
}
|
||||
|
||||
if (index === 0) {
|
||||
const op = line.pop()
|
||||
if (op !== " ") {
|
||||
workingProblem.operation = op!
|
||||
}
|
||||
}
|
||||
|
||||
const numbers = line.filter(c => c !== " ")
|
||||
if (numbers.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const number = parseInt(numbers.join(""))
|
||||
workingProblem.numbers.push(number)
|
||||
}
|
||||
|
||||
problems.push(workingProblem)
|
||||
|
||||
return problems
|
||||
}
|
||||
|
||||
const part1 = solve(problems)
|
||||
console.log("Part 1: ", part1)
|
||||
|
||||
const part2Problems = transposeProblems(content)
|
||||
const part2 = solve(part2Problems)
|
||||
console.log("Part 2: ", part2)
|
||||
12
AdventOfCode2025/day6/package.json
Normal file
12
AdventOfCode2025/day6/package.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "day6",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5"
|
||||
}
|
||||
}
|
||||
28
AdventOfCode2025/day6/tsconfig.json
Normal file
28
AdventOfCode2025/day6/tsconfig.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
// Environment setup & latest features
|
||||
"lib": ["ESNext"],
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleDetection": "force",
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user