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

day 16 2015 - yeah i skipped day 15

aylac.top fed6bc57 602645f9

verified
Changed files
+144
2015
+20
2015/16/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/16/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" }
+100
2015/16/gleam/src/main.gleam
···
···
+
import gleam/dict
+
import gleam/int
+
import gleam/io
+
import gleam/list
+
import gleam/result
+
import gleam/string
+
import simplifile as file
+
+
pub fn main() {
+
let assert Ok(right_sue) = file.read(from: "../sue.txt")
+
as "Sue file not found"
+
let right_sue =
+
right_sue
+
|> string.trim
+
|> string.split("\n")
+
|> list.map(fn(i) {
+
case string.split(i, " ") {
+
[item, amount] -> #(
+
string.drop_end(item, 1),
+
int.parse(amount) |> result.unwrap(0),
+
)
+
_ -> #("", 0)
+
}
+
})
+
|> dict.from_list
+
+
let assert Ok(input) = file.read(from: "../input.txt")
+
as "Input file not found"
+
let input =
+
input
+
|> string.trim
+
|> string.split("\n")
+
|> list.map(fn(i) {
+
case string.split(i, " ") {
+
[_, n, item1, amount1, item2, amount2, item3, amount3] -> #(
+
string.drop_end(n, 1) |> int.parse |> result.unwrap(0),
+
[
+
#(
+
string.drop_end(item1, 1),
+
string.drop_end(amount1, 1) |> int.parse |> result.unwrap(0),
+
),
+
#(
+
string.drop_end(item2, 1),
+
string.drop_end(amount2, 1) |> int.parse |> result.unwrap(0),
+
),
+
#(
+
string.drop_end(item3, 1),
+
amount3 |> int.parse |> result.unwrap(0),
+
),
+
],
+
)
+
_ -> #(0, [#("", 0), #("", 0), #("", 0)])
+
}
+
})
+
|> dict.from_list
+
+
// part 1
+
input
+
|> dict.fold(0, fn(right, sue, items) {
+
let total_right =
+
items
+
|> list.fold_until(0, fn(total_right, item) {
+
case item.1 == dict.get(right_sue, item.0) |> result.unwrap(-10) {
+
True -> list.Continue(total_right + 1)
+
False -> list.Stop(total_right)
+
}
+
})
+
case total_right {
+
3 -> sue
+
_ -> right
+
}
+
})
+
|> int.to_string
+
|> io.println
+
+
// part 2
+
input
+
|> dict.fold(0, fn(right, sue, items) {
+
let total_right =
+
items
+
|> list.fold_until(0, fn(total_right, item) {
+
let right_value = dict.get(right_sue, item.0) |> result.unwrap(-10)
+
let comp = case item.0 {
+
"cats" | "trees" -> item.1 > right_value
+
"pomeranians" | "goldfish" -> item.1 < right_value
+
_ -> item.1 == right_value
+
}
+
case comp {
+
True -> list.Continue(total_right + 1)
+
False -> list.Stop(total_right)
+
}
+
})
+
case total_right {
+
3 -> sue
+
_ -> right
+
}
+
})
+
|> int.to_string
+
|> io.println
+
}
+10
2015/16/sue.txt
···
···
+
children: 3
+
cats: 7
+
samoyeds: 2
+
pomeranians: 3
+
akitas: 0
+
vizslas: 0
+
goldfish: 5
+
trees: 3
+
cars: 2
+
perfumes: 1