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

day 12 2015, not very pretty

aylac.top e4e5a9f8 28a1be15

verified
Changed files
+143
2015
+21
2015/12/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"
+
gleam_json = ">= 3.0.2 and < 4.0.0"
+
+
[dev-dependencies]
+
gleeunit = ">= 1.0.0 and < 2.0.0"
+16
2015/12/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_json", version = "3.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "874FA3C3BB6E22DD2BB111966BD40B3759E9094E05257899A7C08F5DE77EC049" },
+
{ 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_json = { version = ">= 3.0.2 and < 4.0.0" }
+
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" }
+106
2015/12/gleam/src/main.gleam
···
+
import gleam/dict.{type Dict}
+
import gleam/dynamic
+
import gleam/dynamic/decode
+
import gleam/int.{to_string}
+
import gleam/io.{println}
+
import gleam/json
+
import gleam/list
+
import gleam/result.{unwrap}
+
import gleam/string.{trim}
+
import simplifile.{read}
+
+
type Nested {
+
NestedList(List(Nested))
+
NestedDict(Dict(String, Nested))
+
IntValue(Int)
+
StringValue(String)
+
}
+
+
fn int_anywhere_decoder() -> decode.Decoder(Nested) {
+
use <- decode.recursive
+
decode.one_of(decode.int |> decode.map(IntValue), or: [
+
decode.list(int_anywhere_decoder()) |> decode.map(NestedList),
+
decode.dict(decode.string, int_anywhere_decoder()) |> decode.map(NestedDict),
+
decode.string |> decode.map(StringValue),
+
])
+
}
+
+
fn get_total_number_dict(data: Dict(String, Nested)) {
+
data
+
|> dict.fold(0, fn(acc, _, data) {
+
let add = case data {
+
NestedList(items) -> get_total_number_list(items)
+
NestedDict(items) -> get_total_number_dict(items)
+
IntValue(v) -> v
+
_ -> 0
+
}
+
acc + add
+
})
+
}
+
+
fn get_total_number_list(data: List(Nested)) {
+
data
+
|> list.fold(0, fn(acc, data) {
+
let add = case data {
+
NestedList(items) -> get_total_number_list(items)
+
NestedDict(items) -> get_total_number_dict(items)
+
IntValue(v) -> v
+
_ -> 0
+
}
+
acc + add
+
})
+
}
+
+
fn get_total_number_dict_no_red(data: Dict(String, Nested)) {
+
let has_red =
+
data
+
|> dict.fold(False, fn(state, _, data) {
+
case data {
+
StringValue("red") -> True
+
_ -> state
+
}
+
})
+
case has_red {
+
True -> 0
+
False ->
+
data
+
|> dict.fold(0, fn(acc, _, data) {
+
let add = case data {
+
NestedList(items) -> get_total_number_list_no_red(items)
+
NestedDict(items) -> get_total_number_dict_no_red(items)
+
IntValue(v) -> v
+
_ -> 0
+
}
+
acc + add
+
})
+
}
+
}
+
+
fn get_total_number_list_no_red(data: List(Nested)) {
+
data
+
|> list.fold(0, fn(acc, data) {
+
let add = case data {
+
NestedList(items) -> get_total_number_list_no_red(items)
+
NestedDict(items) -> get_total_number_dict_no_red(items)
+
IntValue(v) -> v
+
_ -> 0
+
}
+
acc + add
+
})
+
}
+
+
pub fn main() {
+
let assert Ok(input) =
+
read(from: "../input.json")
+
|> unwrap("")
+
|> json.parse(int_anywhere_decoder())
+
+
println(
+
get_total_number_list([input])
+
|> to_string,
+
)
+
println(
+
get_total_number_list_no_red([input])
+
|> to_string,
+
)
+
}