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

day 6 2015 so fucking slow

aylac.top 7e73817e b61f33f5

verified
+20
2015/6/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/6/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" }
+120
2015/6/gleam/src/main.gleam
···
+
import gleam/dict.{type Dict}
+
import gleam/int.{to_string}
+
import gleam/io.{println}
+
import gleam/list.{fold, map}
+
import gleam/result.{unwrap}
+
import gleam/string.{split, trim}
+
import simplifile.{read}
+
+
pub type Location {
+
Location(x: Int, y: Int)
+
}
+
+
pub type ActionType {
+
Toggle
+
TurnOn
+
TurnOff
+
}
+
+
pub type Action {
+
Action(atype: ActionType, loc1: Location, loc2: Location)
+
}
+
+
pub type Grid =
+
Dict(Location, Bool)
+
+
pub type GridInt =
+
Dict(Location, Int)
+
+
pub fn str_to_loc(str) -> Location {
+
let extract =
+
str
+
|> split(",")
+
|> map(fn(v) { int.base_parse(v, 10) |> unwrap(0) })
+
case extract {
+
[x, y] -> Location(x, y)
+
_ -> Location(0, 0)
+
}
+
}
+
+
pub fn do_action(grid: Grid, action: Action) {
+
list.range(action.loc1.y, action.loc2.y)
+
|> fold(grid, fn(grid, y) {
+
list.range(action.loc1.x, action.loc2.x)
+
|> fold(grid, fn(grid, x) {
+
let loc = Location(x, y)
+
let value = case action.atype {
+
Toggle -> !{ grid |> dict.get(loc) |> unwrap(False) }
+
TurnOff -> False
+
TurnOn -> True
+
}
+
grid |> dict.insert(loc, value)
+
})
+
})
+
}
+
+
pub fn get_grid_true_count(grid: Grid) {
+
grid
+
|> dict.fold(0, fn(cur, _, v) {
+
case v {
+
True -> cur + 1
+
False -> cur
+
}
+
})
+
}
+
+
pub fn do_action_int(grid: GridInt, action: Action) {
+
list.range(action.loc1.y, action.loc2.y)
+
|> fold(grid, fn(grid, y) {
+
list.range(action.loc1.x, action.loc2.x)
+
|> fold(grid, fn(grid, x) {
+
let loc = Location(x, y)
+
let cur = grid |> dict.get(loc) |> unwrap(0)
+
let value = case action.atype {
+
Toggle -> cur + 2
+
TurnOff -> int.max(0, cur - 1)
+
TurnOn -> int.max(0, cur + 1)
+
}
+
grid |> dict.insert(loc, value)
+
})
+
})
+
}
+
+
pub fn get_grid_total_brightness(grid: GridInt) {
+
grid
+
|> dict.fold(0, fn(cur, _, v) { cur + v })
+
}
+
+
pub fn main() {
+
let input =
+
read(from: "../input.txt")
+
|> unwrap("")
+
|> trim()
+
|> split("\n")
+
|> list.map(fn(v) {
+
let split_v = split(v, " ")
+
case split_v {
+
["toggle", loc1, _, loc2] ->
+
Action(Toggle, str_to_loc(loc1), str_to_loc(loc2))
+
["turn", "off", loc1, _, loc2] ->
+
Action(TurnOff, str_to_loc(loc1), str_to_loc(loc2))
+
["turn", "on", loc1, _, loc2] ->
+
Action(TurnOn, str_to_loc(loc1), str_to_loc(loc2))
+
_ -> Action(Toggle, Location(0, 0), Location(0, 0))
+
}
+
})
+
+
println(
+
input
+
|> list.fold(dict.new(), fn(grid, a) { grid |> do_action(a) })
+
|> get_grid_true_count
+
|> to_string,
+
)
+
+
println(
+
input
+
|> list.fold(dict.new(), fn(grid, a) { grid |> do_action_int(a) })
+
|> get_grid_total_brightness
+
|> to_string,
+
)
+
}
+29
2015/6/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/6/ts/index.ts
···
+
const input = (await Bun.file("../input.txt").text()).trim();
+15
2015/6/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/6/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
+
}
+
}