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

day 13 2015 - did i do too much

aylac.top 762cd8ea 859daf6d

verified
Changed files
+222 -4
2015
template
gleam
+20
2015/13/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/13/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" }
+183
2015/13/gleam/src/main.gleam
···
+
import gleam/dict.{type Dict}
+
import gleam/int
+
import gleam/io
+
import gleam/list
+
import gleam/set.{type Set}
+
import gleam/string
+
import simplifile.{read}
+
+
pub type Relation {
+
Relation(person: String, neighbor: String)
+
}
+
+
pub type SingularHappiness =
+
Int
+
+
pub type SingularHappinessDict =
+
Dict(Relation, SingularHappiness)
+
+
pub type Happiness =
+
Int
+
+
pub type HappinessDict =
+
Dict(Relation, Happiness)
+
+
pub type People =
+
Set(String)
+
+
pub type Sign {
+
Gain
+
Lose
+
}
+
+
fn do_find_happiest_table(
+
happiness_dict,
+
people,
+
happiest_table,
+
first_person first_person,
+
last_person last_person,
+
table table,
+
) {
+
case set.size(people) == 0 {
+
True -> {
+
let assert Ok(relation) =
+
happiness_dict |> dict.get(Relation(last_person, first_person))
+
as "There should be more than one person"
+
let table = table + relation
+
case table > happiest_table {
+
True -> table
+
False -> happiest_table
+
}
+
}
+
False -> {
+
people
+
|> set.fold(0, fn(happiest_table, person) {
+
let assert Ok(relation) =
+
happiness_dict |> dict.get(Relation(last_person, person))
+
as {
+
"Relation between "
+
<> { last_person <> " and " <> person }
+
<> " was not found"
+
}
+
let table =
+
table
+
+ relation
+
+ do_find_happiest_table(
+
happiness_dict,
+
people |> set.delete(person),
+
happiest_table,
+
first_person,
+
last_person: person,
+
table: table,
+
)
+
case table > happiest_table {
+
True -> table
+
False -> happiest_table
+
}
+
})
+
}
+
}
+
}
+
+
pub fn find_happiest_table(happiness_dict: HappinessDict, people: People) {
+
let assert Ok(person) = people |> set.to_list |> list.first
+
as "People list is empty"
+
do_find_happiest_table(
+
happiness_dict,
+
people |> set.delete(person),
+
0,
+
first_person: person,
+
last_person: person,
+
table: 0,
+
)
+
}
+
+
pub fn parse_sign(sign: String) {
+
case sign {
+
"gain" -> Ok(Gain)
+
"lose" -> Ok(Lose)
+
_ -> Error("Some sign input was neither gain or lose: " <> sign)
+
}
+
}
+
+
pub fn main() {
+
let assert Ok(input) = read(from: "../input.txt") as "Input file not found"
+
+
let singular_happiness_dict: SingularHappinessDict =
+
input
+
|> string.trim
+
|> string.split("\n")
+
|> list.map(fn(value) {
+
case value |> string.split(" ") {
+
// (person) would (gain/lose) (amount) happiness units by sitting next to (neighbor.)
+
[person, _, sign, happiness, _, _, _, _, _, _, neighbor] -> {
+
let assert Ok(happiness) = happiness |> int.base_parse(10)
+
as { "Happiness values should be integers: " <> happiness }
+
let sign = case parse_sign(sign) {
+
Ok(sign) -> sign
+
Error(message) -> panic as message
+
}
+
let happiness = case sign {
+
Gain -> happiness
+
Lose -> happiness |> int.negate
+
}
+
+
let neighbor = case neighbor |> string.last {
+
Ok(".") -> neighbor |> string.drop_end(1)
+
_ -> panic as { "Neighbor didn't end in a dot: " <> neighbor }
+
}
+
+
#(Relation(person, neighbor), happiness)
+
}
+
_ -> panic as { "Value doesn't match constrainsts" <> value }
+
}
+
})
+
|> dict.from_list
+
+
let happiness_dict: HappinessDict =
+
singular_happiness_dict
+
|> dict.fold(dict.new(), fn(hd, relation, singular_happiness) {
+
let Relation(person1, person2) = relation
+
+
case hd |> dict.has_key(relation) {
+
False -> {
+
let other_relation = Relation(person2, person1)
+
let assert Ok(other_singular_happiness) =
+
singular_happiness_dict |> dict.get(other_relation)
+
as "Malformed singular_happiness_dict"
+
let total_happiness = singular_happiness + other_singular_happiness
+
+
hd
+
|> dict.insert(relation, total_happiness)
+
|> dict.insert(other_relation, total_happiness)
+
}
+
True -> hd
+
}
+
})
+
+
let people: People =
+
singular_happiness_dict
+
|> dict.fold(set.new(), fn(people, relation, _) {
+
people |> set.insert(relation.person)
+
})
+
+
"Part 1" |> io.println
+
let happiest_table = find_happiest_table(happiness_dict, people)
+
happiest_table |> int.to_string |> io.println
+
+
"Part 2" |> io.println
+
let relations_with_me: SingularHappinessDict =
+
people
+
|> set.to_list
+
|> list.map(fn(person) {
+
[#(Relation(person, "Me"), 0), #(Relation("Me", person), 0)]
+
})
+
|> list.flatten
+
|> dict.from_list
+
let happiness_dict = happiness_dict |> dict.merge(relations_with_me)
+
let people = people |> set.insert("Me")
+
+
let happiest_table_with_me = find_happiest_table(happiness_dict, people)
+
"With me: " |> io.print
+
happiest_table_with_me |> int.to_string |> io.println
+
}
+5 -4
template/gleam/src/main.gleam
···
import gleam/int.{to_string}
import gleam/io.{println}
-
import gleam/result.{unwrap}
-
import gleam/string.{trim}
-
import simplifile.{read}
+
import gleam/string
+
import simplifile.{read as read_file}
pub fn main() {
-
let input = read(from: "../input.txt") |> unwrap("") |> trim()
+
let assert Ok(input) = read_file(from: "../input.txt")
+
as "Input file not found"
+
let input = input |> string.trim
println(
5