my solutions to advent of code
aoc advent-of-code

this is all so ugly...

aylac.top fd8c61e2 ecf8623a

verified
+20
2015/7/gleam/gleam.toml
···
+
name = "main"
+
version = "1.0.0"
+
+
# Fill out these fields if you intend to generate HTML documentation or publish
+
# your project to the Hex package manager.
+
#
+
# description = ""
+
# licences = ["Apache-2.0"]
+
# repository = { type = "github", user = "", repo = "" }
+
# links = [{ title = "Website", href = "" }]
+
#
+
# For a full reference of all the available options, you can have a look at
+
# https://gleam.run/writing-gleam/gleam-toml/.
+
+
[dependencies]
+
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
+
simplifile = ">= 2.3.0 and < 3.0.0"
+
+
[dev-dependencies]
+
gleeunit = ">= 1.0.0 and < 2.0.0"
+14
2015/7/gleam/manifest.toml
···
+
# This file was generated by Gleam
+
# You typically do not need to edit this file
+
+
packages = [
+
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
+
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
+
{ name = "gleeunit", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "CD701726CBCE5588B375D157B4391CFD0F2F134CD12D9B6998A395484DE05C58" },
+
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
+
]
+
+
[requirements]
+
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
+
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
+
simplifile = { version = ">= 2.3.0 and < 3.0.0" }
+117
2015/7/gleam/src/main.gleam
···
+
import gleam/dict.{type Dict}
+
import gleam/int.{to_string}
+
import gleam/io.{println}
+
import gleam/list.{fold}
+
import gleam/result.{unwrap}
+
import gleam/string.{split, trim}
+
import simplifile.{read}
+
+
pub type Circuit =
+
Dict(String, Operation)
+
+
pub type CircuitCache =
+
Dict(String, Int)
+
+
pub type Operation {
+
Not(var: String)
+
And(var1: String, var2: String)
+
Or(var1: String, var2: String)
+
Rshift(var: String, n: Int)
+
Lshift(var: String, n: Int)
+
Set(n: String)
+
OpError
+
}
+
+
pub type GetCircRes {
+
GetCircRes(value: Int, cache: CircuitCache)
+
}
+
+
pub fn get_circuit_var(
+
circuit: Circuit,
+
name: String,
+
cache: CircuitCache,
+
) -> GetCircRes {
+
let get = fn(name, cache) { get_circuit_var(circuit, name, cache) }
+
+
let parsed_name = int.base_parse(name, 10)
+
let GetCircRes(value, cache) = case result.is_ok(parsed_name) {
+
True -> GetCircRes(parsed_name |> unwrap(0), cache)
+
False -> {
+
case dict.get(cache, name) {
+
Ok(value) -> GetCircRes(value, cache)
+
Error(_) -> {
+
case dict.get(circuit, name) |> unwrap(OpError) {
+
Not(var) -> {
+
let GetCircRes(value, cache) = get(var, cache)
+
GetCircRes(int.bitwise_not(value), cache)
+
}
+
And(var1, var2) -> {
+
let GetCircRes(value1, cache) = get(var1, cache)
+
let GetCircRes(value2, cache) = get(var2, cache)
+
GetCircRes(int.bitwise_and(value1, value2), cache)
+
}
+
Or(var1, var2) -> {
+
let GetCircRes(value1, cache) = get(var1, cache)
+
let GetCircRes(value2, cache) = get(var2, cache)
+
GetCircRes(int.bitwise_or(value1, value2), cache)
+
}
+
Rshift(var, n) -> {
+
let GetCircRes(value, cache) = get(var, cache)
+
GetCircRes(int.bitwise_shift_right(value, n), cache)
+
}
+
Lshift(var, n) -> {
+
let GetCircRes(value, cache) = get(var, cache)
+
GetCircRes(int.bitwise_shift_left(value, n), cache)
+
}
+
Set(var) -> get(var, cache)
+
OpError -> GetCircRes(0, cache)
+
}
+
}
+
}
+
}
+
}
+
GetCircRes(value, dict.insert(cache, name, value))
+
}
+
+
pub fn main() {
+
let input: Circuit =
+
read(from: "../input.txt")
+
|> unwrap("")
+
|> trim()
+
|> split("\n")
+
|> fold(dict.new(), fn(circ, v) {
+
case split(v, " ") {
+
["NOT", var, "->", target] -> dict.insert(circ, target, Not(var))
+
[var1, "AND", var2, "->", target] ->
+
dict.insert(circ, target, And(var1, var2))
+
[var1, "OR", var2, "->", target] ->
+
dict.insert(circ, target, Or(var1, var2))
+
[var, "RSHIFT", n, "->", target] ->
+
dict.insert(
+
circ,
+
target,
+
Rshift(var, int.base_parse(n, 10) |> unwrap(0)),
+
)
+
[var, "LSHIFT", n, "->", target] ->
+
dict.insert(
+
circ,
+
target,
+
Lshift(var, int.base_parse(n, 10) |> unwrap(0)),
+
)
+
[v, "->", target] -> dict.insert(circ, target, Set(v))
+
+
_ -> circ
+
}
+
})
+
+
let result_part_1 = get_circuit_var(input, "a", dict.new()).value
+
println(result_part_1 |> to_string)
+
+
let result_part_2 =
+
get_circuit_var(
+
dict.insert(input, "b", Set(int.to_string(result_part_1))),
+
"a",
+
dict.new(),
+
).value
+
println(result_part_2 |> to_string)
+
}
+29
2015/7/ts/bun.lock
···
+
{
+
"lockfileVersion": 1,
+
"workspaces": {
+
"": {
+
"name": "ts",
+
"devDependencies": {
+
"@types/bun": "latest",
+
},
+
"peerDependencies": {
+
"typescript": "^5",
+
},
+
},
+
},
+
"packages": {
+
"@types/bun": ["@types/bun@1.3.1", "", { "dependencies": { "bun-types": "1.3.1" } }, "sha512-4jNMk2/K9YJtfqwoAa28c8wK+T7nvJFOjxI4h/7sORWcypRNxBpr+TPNaCfVWq70tLCJsqoFwcf0oI0JU/fvMQ=="],
+
+
"@types/node": ["@types/node@24.9.2", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-uWN8YqxXxqFMX2RqGOrumsKeti4LlmIMIyV0lgut4jx7KQBcBiW6vkDtIBvHnHIquwNfJhk8v2OtmO8zXWHfPA=="],
+
+
"@types/react": ["@types/react@19.2.2", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-6mDvHUFSjyT2B2yeNx2nUgMxh9LtOWvkhIU3uePn2I2oyNymUAX1NIsdgviM4CH+JSrp2D2hsMvJOkxY+0wNRA=="],
+
+
"bun-types": ["bun-types@1.3.1", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-NMrcy7smratanWJ2mMXdpatalovtxVggkj11bScuWuiOoXTiKIu2eVS1/7qbyI/4yHedtsn175n4Sm4JcdHLXw=="],
+
+
"csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
+
+
"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=="],
+
}
+
}
+1
2015/7/ts/index.ts
···
+
const input = (await Bun.file("../input.txt").text()).trim();
+15
2015/7/ts/package.json
···
+
{
+
"name": "ts",
+
"module": "index.ts",
+
"type": "module",
+
"scripts": {
+
"start": "bun index.ts"
+
},
+
"private": true,
+
"devDependencies": {
+
"@types/bun": "latest"
+
},
+
"peerDependencies": {
+
"typescript": "^5"
+
}
+
}
+29
2015/7/ts/tsconfig.json
···
+
{
+
"compilerOptions": {
+
// Environment setup & latest features
+
"lib": ["ESNext"],
+
"target": "ESNext",
+
"module": "Preserve",
+
"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,
+
"noImplicitOverride": true,
+
+
// Some stricter flags (disabled by default)
+
"noUnusedLocals": false,
+
"noUnusedParameters": false,
+
"noPropertyAccessFromIndexSignature": false
+
}
+
}