aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-12-21 22:09:25 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-12-21 22:09:25 +0100
commitd8e183f02f67522d94deafa328e19b3081ca41be (patch)
tree4617cc4fec15365cdcdfeb594b02432937d6c946
parent02598d252c0e9093384ec53e46445df1a783feda (diff)
downloadgleam_aoc2020-d8e183f02f67522d94deafa328e19b3081ca41be.tar.gz
gleam_aoc2020-d8e183f02f67522d94deafa328e19b3081ca41be.zip
Update to newest Gleam version
-rw-r--r--aoc-2020-gleam/gleam.toml8
-rw-r--r--aoc-2020-gleam/manifest.toml16
-rw-r--r--aoc-2020-gleam/src/days/day01.gleam6
-rw-r--r--aoc-2020-gleam/src/days/day02.gleam6
-rw-r--r--aoc-2020-gleam/src/days/day03.gleam40
-rw-r--r--aoc-2020-gleam/src/days/day04.gleam43
-rw-r--r--aoc-2020-gleam/src/days/day05.gleam5
-rw-r--r--aoc-2020-gleam/src/days/day06.gleam8
-rw-r--r--aoc-2020-gleam/src/days/day07.gleam24
-rw-r--r--aoc-2020-gleam/src/days/day08.gleam14
-rw-r--r--aoc-2020-gleam/src/days/day09.gleam8
-rw-r--r--aoc-2020-gleam/src/days/day10.gleam8
-rw-r--r--aoc-2020-gleam/src/days/day11.gleam30
-rw-r--r--aoc-2020-gleam/src/days/day12.gleam11
-rw-r--r--aoc-2020-gleam/src/days/day13.gleam31
-rw-r--r--aoc-2020-gleam/src/days/day14.gleam39
-rw-r--r--aoc-2020-gleam/src/days/day15.gleam32
-rw-r--r--aoc-2020-gleam/src/days/day16.gleam62
-rw-r--r--aoc-2020-gleam/src/days/day17.gleam27
-rw-r--r--aoc-2020-gleam/src/days/day19.gleam20
-rw-r--r--aoc-2020-gleam/src/days/day22.gleam20
-rw-r--r--aoc-2020-gleam/src/days/day23.gleam74
-rw-r--r--aoc-2020-gleam/src/ext/dictx.gleam14
-rw-r--r--aoc-2020-gleam/src/ext/iteratorx.gleam4
-rw-r--r--aoc-2020-gleam/src/ext/listx.gleam4
-rw-r--r--aoc-2020-gleam/src/ext/mapx.gleam16
-rw-r--r--aoc-2020-gleam/src/ext/setx.gleam2
-rw-r--r--aoc-2020-gleam/src/util/cache.gleam20
-rw-r--r--aoc-2020-gleam/src/util/dir.gleam2
-rw-r--r--aoc-2020-gleam/src/util/graph.gleam15
-rw-r--r--aoc-2020-gleam/src/util/input_util.gleam4
-rw-r--r--aoc-2020-gleam/src/util/parser.gleam10
-rw-r--r--aoc-2020-gleam/src/util/pos2.gleam2
-rw-r--r--aoc-2020-gleam/src/util/pos3.gleam2
-rw-r--r--aoc-2020-gleam/src/util/pos4.gleam2
35 files changed, 303 insertions, 326 deletions
diff --git a/aoc-2020-gleam/gleam.toml b/aoc-2020-gleam/gleam.toml
index fa53656..61c0e7f 100644
--- a/aoc-2020-gleam/gleam.toml
+++ b/aoc-2020-gleam/gleam.toml
@@ -3,7 +3,7 @@ version = "0.1.0"
description = "A Gleam project"
[dependencies]
-gleam_stdlib = "~> 0.30"
-gleam_erlang = "~> 0.18"
-gleam_otp = "~> 0.5"
-gleam_bitwise = "~> 1.2"
+gleam_stdlib = "~> 0.34"
+gleam_erlang = "~> 0.23"
+gleam_otp = "~> 0.8"
+simplifile = "~> 1.0"
diff --git a/aoc-2020-gleam/manifest.toml b/aoc-2020-gleam/manifest.toml
index c92fc18..3a7849f 100644
--- a/aoc-2020-gleam/manifest.toml
+++ b/aoc-2020-gleam/manifest.toml
@@ -2,14 +2,14 @@
# You typically do not need to edit this file
packages = [
- { name = "gleam_bitwise", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "6064699EFBABB1CA392DCB193D0E8B402FB042B4B46857B01E6875E643B57F54" },
- { name = "gleam_erlang", version = "0.19.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "720D1E0A0CEBBD51C4AA88501D1D4FBFEF4AA7B3332C994691ED944767A52582" },
- { name = "gleam_otp", version = "0.5.3", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "6E705B69464237353E0380AC8143BDB29A3F0BF6168755D5F2D6E55A34A8B077" },
- { name = "gleam_stdlib", version = "0.30.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "03710B3DA047A3683117591707FCA19D32B980229DD8CE8B0603EB5B5144F6C3" },
+ { name = "gleam_erlang", version = "0.23.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "C21CFB816C114784E669FFF4BBF433535EEA9960FA2F216209B8691E87156B96" },
+ { name = "gleam_otp", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "18EF8242A5E54BA92F717C7222F03B3228AEE00D1F286D4C56C3E8C18AA2588E" },
+ { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
+ { name = "simplifile", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0BD6F0E7DA1A7E11D18B8AD48453225CAFCA4C8CFB4513D217B372D2866C501C" },
]
[requirements]
-gleam_bitwise = { version = "~> 1.2" }
-gleam_erlang = { version = "~> 0.18" }
-gleam_otp = { version = "~> 0.5" }
-gleam_stdlib = { version = "~> 0.30" }
+gleam_erlang = { version = "~> 0.23" }
+gleam_otp = { version = "~> 0.8" }
+gleam_stdlib = { version = "~> 0.34" }
+simplifile = { version = "~> 1.0" }
diff --git a/aoc-2020-gleam/src/days/day01.gleam b/aoc-2020-gleam/src/days/day01.gleam
index 1616cab..cb3fc07 100644
--- a/aoc-2020-gleam/src/days/day01.gleam
+++ b/aoc-2020-gleam/src/days/day01.gleam
@@ -21,9 +21,9 @@ fn part2(numbers: List(Int)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_numbers("test01")
- let assert 514_579 = part1(test)
- let assert 241_861_950 = part2(test)
+ let testing = input_util.read_numbers("test01")
+ let assert 514_579 = part1(testing)
+ let assert 241_861_950 = part2(testing)
let input = input_util.read_numbers("day01")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam
index 5a5a907..e3635fe 100644
--- a/aoc-2020-gleam/src/days/day02.gleam
+++ b/aoc-2020-gleam/src/days/day02.gleam
@@ -78,9 +78,9 @@ fn part2(lines: List(String)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_lines("test02")
- let assert 2 = part1(test)
- let assert 1 = part2(test)
+ let testing = input_util.read_lines("test02")
+ let assert 2 = part1(testing)
+ let assert 1 = part2(testing)
let input = input_util.read_lines("day02")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day03.gleam b/aoc-2020-gleam/src/days/day03.gleam
index 33f15eb..a3ab7b0 100644
--- a/aoc-2020-gleam/src/days/day03.gleam
+++ b/aoc-2020-gleam/src/days/day03.gleam
@@ -5,11 +5,11 @@ import gleam/result as res
import gleam/string as str
import gleam/function as fun
import gleam/iterator as iter
-import gleam/set.{Set}
+import gleam/set.{type Set}
import ext/intx
import ext/iteratorx as iterx
import util/input_util
-import util/pos2.{Pos2}
+import util/pos2.{type Pos2}
const starting_pos = #(0, 0)
@@ -25,23 +25,19 @@ fn parse_area(from text: String) -> Area {
let lines = str.split(text, on: "\n")
let trees =
- list.index_fold(
- over: lines,
- from: set.new(),
- with: fn(prev, line, y) {
- line
- |> str.to_graphemes
- |> list.index_map(with: fn(x, grapheme) {
- case grapheme {
- "#" -> Ok(#(x, y))
- _ -> Error(Nil)
- }
- })
- |> list.filter_map(with: fun.identity)
- |> set.from_list
- |> set.union(prev)
- },
- )
+ list.index_fold(over: lines, from: set.new(), with: fn(prev, line, y) {
+ line
+ |> str.to_graphemes
+ |> list.index_map(with: fn(grapheme, x) {
+ case grapheme {
+ "#" -> Ok(#(x, y))
+ _ -> Error(Nil)
+ }
+ })
+ |> list.filter_map(with: fun.identity)
+ |> set.from_list
+ |> set.union(prev)
+ })
let assert Ok(cycle) =
lines
|> list.first
@@ -81,9 +77,9 @@ fn part2(text: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test03")
- let assert 7 = part1(test)
- let assert 336 = part2(test)
+ let testing = input_util.read_text("test03")
+ let assert 7 = part1(testing)
+ let assert 336 = part2(testing)
let input = input_util.read_text("day03")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day04.gleam b/aoc-2020-gleam/src/days/day04.gleam
index dfeba0f..b562259 100644
--- a/aoc-2020-gleam/src/days/day04.gleam
+++ b/aoc-2020-gleam/src/days/day04.gleam
@@ -2,7 +2,7 @@ import gleam/io
import gleam/list
import gleam/function as fun
import gleam/result as res
-import gleam/map.{Map}
+import gleam/dict.{type Dict}
import ext/listx
import ext/intx
import ext/resultx as resx
@@ -16,7 +16,7 @@ const required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
const eye_colors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
type Passport {
- Passport(fields: Map(String, String))
+ Passport(fields: Dict(String, String))
}
fn parse_passports(from text: String) -> List(Passport) {
@@ -34,7 +34,7 @@ fn parse_passports(from text: String) -> List(Passport) {
let passport_parser =
field_parser
|> p.sep1(by: p.ws_gc())
- |> p.map(with: fun.compose(map.from_list, Passport))
+ |> p.map(with: fun.compose(dict.from_list, Passport))
|> p.labeled(with: "passport")
let input_parser =
passport_parser
@@ -49,12 +49,13 @@ fn parse_passports(from text: String) -> List(Passport) {
fn is_valid1(passport: Passport) -> Bool {
let has_only_allowed_keys =
- map.keys(passport.fields)
+ passport.fields
+ |> dict.keys
|> list.all(satisfying: list.contains(allowed_fields, _))
let has_all_required_keys =
required_fields
- |> list.all(satisfying: list.contains(map.keys(passport.fields), _))
+ |> list.all(satisfying: list.contains(dict.keys(passport.fields), _))
has_only_allowed_keys && has_all_required_keys
}
@@ -104,20 +105,18 @@ fn is_valid2(passport: Passport) -> Bool {
),
]
- is_valid1(passport) && list.all(
- validators,
- satisfying: fn(validator) {
- let #(key, parser) = validator
- passport.fields
- |> map.get(key)
- |> res.then(apply: fn(value) {
- value
- |> p.parse_entire(with: parser)
- |> res.replace_error(Nil)
- })
- |> res.is_ok
- },
- )
+ is_valid1(passport)
+ && list.all(validators, satisfying: fn(validator) {
+ let #(key, parser) = validator
+ passport.fields
+ |> dict.get(key)
+ |> res.then(apply: fn(value) {
+ value
+ |> p.parse_entire(with: parser)
+ |> res.replace_error(Nil)
+ })
+ |> res.is_ok
+ })
}
fn part1(text: String) -> Int {
@@ -133,9 +132,9 @@ fn part2(text: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test04")
- let assert 2 = part1(test)
- let assert 2 = part2(test)
+ let testing = input_util.read_text("test04")
+ let assert 2 = part1(testing)
+ let assert 2 = part2(testing)
let input = input_util.read_text("day04")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day05.gleam b/aoc-2020-gleam/src/days/day05.gleam
index 8d37736..076ae1d 100644
--- a/aoc-2020-gleam/src/days/day05.gleam
+++ b/aoc-2020-gleam/src/days/day05.gleam
@@ -14,6 +14,7 @@ fn get_seat_id(pass: String) -> Int {
case grapheme {
"F" | "L" -> "0"
"B" | "R" -> "1"
+ _ -> panic
}
})
|> str.concat
@@ -44,8 +45,8 @@ fn part2(lines: List(String)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_lines("test05")
- let assert 820 = part1(test)
+ let testing = input_util.read_lines("test05")
+ let assert 820 = part1(testing)
let input = input_util.read_lines("day05")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day06.gleam b/aoc-2020-gleam/src/days/day06.gleam
index 1e2853b..7587d42 100644
--- a/aoc-2020-gleam/src/days/day06.gleam
+++ b/aoc-2020-gleam/src/days/day06.gleam
@@ -2,7 +2,7 @@ import gleam/io
import gleam/int
import gleam/list
import gleam/string as str
-import gleam/set.{Set}
+import gleam/set.{type Set}
import ext/resultx as resx
import util/input_util
import util/parser as p
@@ -80,9 +80,9 @@ fn part2(text: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test06")
- let assert 11 = part1(test)
- let assert 6 = part2(test)
+ let testing = input_util.read_text("test06")
+ let assert 11 = part1(testing)
+ let assert 6 = part2(testing)
let input = input_util.read_text("day06")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day07.gleam b/aoc-2020-gleam/src/days/day07.gleam
index 2671d72..cfde3e6 100644
--- a/aoc-2020-gleam/src/days/day07.gleam
+++ b/aoc-2020-gleam/src/days/day07.gleam
@@ -4,8 +4,8 @@ import gleam/list
import gleam/pair
import gleam/result as res
import gleam/function as fun
-import gleam/map.{Map}
-import gleam/iterator.{Iterator} as iter
+import gleam/dict.{type Dict}
+import gleam/iterator.{type Iterator} as iter
import ext/genericx as genx
import ext/resultx as resx
import ext/iteratorx as iterx
@@ -22,7 +22,7 @@ type BagEdge =
#(BagId, Int)
type BagGraph =
- Map(BagId, List(BagEdge))
+ Dict(BagId, List(BagEdge))
type BagNeighbourFun =
fn(BagId) -> Iterator(BagId)
@@ -44,7 +44,7 @@ fn parse_graph(lines: List(String)) -> BagGraph {
|> p.skip_ws
|> p.skip(p.then(p.literal("bag"), p.opt(p.literal("s"))))
|> p.sep1(by: p.literal(", ")),
- else: p.literal("no other bags")
+ otherwise: p.literal("no other bags")
|> p.replace(with: []),
))
|> p.skip(p.literal("."))
@@ -54,13 +54,13 @@ fn parse_graph(lines: List(String)) -> BagGraph {
p.parse_entire(_, with: line_parser),
resx.assert_unwrap,
))
- |> map.from_list
+ |> dict.from_list
}
fn neighbour_fun(graph: BagGraph) -> BagNeighbourFun {
fn(bag) {
graph
- |> map.get(bag)
+ |> dict.get(bag)
|> resx.assert_unwrap
|> list.map(with: pair.first)
|> iter.from_list
@@ -70,7 +70,7 @@ fn neighbour_fun(graph: BagGraph) -> BagNeighbourFun {
fn bag_count(of bag: BagId, in graph: BagGraph) -> Int {
list.fold(
over: graph
- |> map.get(bag)
+ |> dict.get(bag)
|> res.unwrap(or: []),
from: 1,
with: fn(sum, edge) {
@@ -85,9 +85,9 @@ fn part1(lines: List(String)) -> Int {
let neighbours = neighbour_fun(graph)
graph
- |> map.keys
+ |> dict.keys
|> iter.from_list
- |> iter.filter(for: genx.different(_, than: special_bag))
+ |> iter.filter(keeping: genx.different(_, than: special_bag))
|> iterx.count(satisfying: fn(start) {
start
|> graph.dfs(with: neighbours)
@@ -102,9 +102,9 @@ fn part2(lines: List(String)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_lines("test07")
- let assert 4 = part1(test)
- let assert 32 = part2(test)
+ let testing = input_util.read_lines("test07")
+ let assert 4 = part1(testing)
+ let assert 32 = part2(testing)
let input = input_util.read_lines("day07")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day08.gleam b/aoc-2020-gleam/src/days/day08.gleam
index a4ebacd..5e7492c 100644
--- a/aoc-2020-gleam/src/days/day08.gleam
+++ b/aoc-2020-gleam/src/days/day08.gleam
@@ -3,9 +3,9 @@ import gleam/int
import gleam/list
import gleam/bool
import gleam/result as res
-import gleam/set.{Set}
-import gleam/iterator.{Iterator} as iter
-import gleam/option.{None, Option, Some} as opt
+import gleam/set.{type Set}
+import gleam/iterator.{type Iterator} as iter
+import gleam/option.{type Option, None, Some} as opt
import ext/listx
import ext/resultx as resx
import util/input_util
@@ -120,7 +120,7 @@ fn all_program_mutations(of program: Program) -> Iterator(Program) {
|> iter.from_list
|> iter.index
|> iter.flat_map(fn(elem) {
- let #(index, instr) = elem
+ let #(instr, index) = elem
case instr {
Acc(_) -> iter.empty()
_ ->
@@ -152,9 +152,9 @@ fn part2(lines: List(String)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_lines("test08")
- let assert 5 = part1(test)
- let assert 8 = part2(test)
+ let testing = input_util.read_lines("test08")
+ let assert 5 = part1(testing)
+ let assert 8 = part2(testing)
let input = input_util.read_lines("day08")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day09.gleam b/aoc-2020-gleam/src/days/day09.gleam
index 1081f64..eb7f7f9 100644
--- a/aoc-2020-gleam/src/days/day09.gleam
+++ b/aoc-2020-gleam/src/days/day09.gleam
@@ -36,7 +36,7 @@ fn part2(numbers: List(Int), preamble_length: Int) -> Int {
|> iter.from_list
|> iter.index
|> iterx.filter_map(with: fn(step) {
- let #(index, _) = step
+ let #(_, index) = step
let sublist = list.drop(from: numbers, up_to: index)
sublist
@@ -56,9 +56,9 @@ fn part2(numbers: List(Int), preamble_length: Int) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_numbers("test09")
- let assert 127 = part1(test, 5)
- let assert 62 = part2(test, 5)
+ let testing = input_util.read_numbers("test09")
+ let assert 127 = part1(testing, 5)
+ let assert 62 = part2(testing, 5)
let input = input_util.read_numbers("day09")
io.debug(part1(input, 25))
diff --git a/aoc-2020-gleam/src/days/day10.gleam b/aoc-2020-gleam/src/days/day10.gleam
index d7cadd6..12f252e 100644
--- a/aoc-2020-gleam/src/days/day10.gleam
+++ b/aoc-2020-gleam/src/days/day10.gleam
@@ -7,7 +7,7 @@ import ext/listx
import ext/pairx
import ext/genericx as genx
import util/input_util
-import util/cache.{Cache}
+import util/cache.{type Cache}
const outlet_joltage = 0
@@ -54,9 +54,9 @@ fn part2(numbers: List(Int)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_numbers("test10")
- let assert 220 = part1(test)
- let assert 19_208 = part2(test)
+ let testing = input_util.read_numbers("test10")
+ let assert 220 = part1(testing)
+ let assert 19_208 = part2(testing)
let input = input_util.read_numbers("day10")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day11.gleam b/aoc-2020-gleam/src/days/day11.gleam
index 1fd8f87..b04dc31 100644
--- a/aoc-2020-gleam/src/days/day11.gleam
+++ b/aoc-2020-gleam/src/days/day11.gleam
@@ -1,15 +1,15 @@
import gleam/io
import gleam/string as str
import gleam/iterator as iter
-import gleam/map.{Map}
-import ext/mapx
+import gleam/dict.{type Dict}
+import ext/dictx
import ext/setx
import ext/listx
import ext/resultx as resx
import ext/genericx as genx
import ext/iteratorx as iterx
import util/input_util
-import util/pos2.{Pos2}
+import util/pos2.{type Pos2}
type Seat {
Empty
@@ -17,7 +17,7 @@ type Seat {
}
type Grid {
- Grid(data: Map(Pos2, Seat))
+ Grid(data: Dict(Pos2, Seat))
}
type Settings {
@@ -31,20 +31,20 @@ fn build_grid(from input: String) -> Grid {
|> iter.map(with: str.trim)
|> iter.index
|> iter.flat_map(with: fn(line) {
- let #(row_index, row) = line
+ let #(row, row_index) = line
row
|> str.to_graphemes
|> iter.from_list
|> iter.index
|> iter.flat_map(with: fn(elem) {
- let #(col_index, grapheme) = elem
+ let #(grapheme, col_index) = elem
case grapheme == "L" {
True -> iter.single(#(#(col_index, row_index), Empty))
False -> iter.empty()
}
})
})
- |> mapx.from_iter
+ |> dictx.from_iter
|> Grid
}
@@ -52,7 +52,7 @@ fn count_near_adjacent(grid: Grid, from start: Pos2) -> Int {
start
|> pos2.neighbours8
|> setx.count(satisfying: fn(n) {
- case map.get(grid.data, n) {
+ case dict.get(grid.data, n) {
Ok(seat) -> seat == Occupied
Error(Nil) -> False
}
@@ -66,7 +66,7 @@ fn count_far_adjacent(grid: Grid, from start: Pos2) -> Int {
|> pos2.add(d)
|> iterx.unfold_infinitely(pos2.add(_, d))
|> iter.take(up_to: 1000)
- |> iterx.filter_map(with: map.get(grid.data, _))
+ |> iterx.filter_map(with: dict.get(grid.data, _))
|> iter.first
|> genx.equals(Ok(Occupied))
})
@@ -74,14 +74,14 @@ fn count_far_adjacent(grid: Grid, from start: Pos2) -> Int {
fn count_occupied(grid: Grid) -> Int {
grid.data
- |> map.values
+ |> dict.values
|> listx.count(satisfying: genx.equals(_, Occupied))
}
fn step_grid(prev: Grid, settings: Settings) -> Grid {
let Settings(threshold, adjacent_counter) = settings
prev.data
- |> map.map_values(with: fn(pos, seat) {
+ |> dict.map_values(with: fn(pos, seat) {
let adjacent = adjacent_counter(prev, pos)
case seat {
Empty if adjacent == 0 -> Occupied
@@ -95,7 +95,7 @@ fn step_grid(prev: Grid, settings: Settings) -> Grid {
fn is_stable(grid: Grid, settings: Settings) -> Bool {
let Settings(threshold, adjacent_counter) = settings
grid.data
- |> mapx.to_iter
+ |> dictx.to_iter
|> iter.all(satisfying: fn(entry) {
let #(pos, seat) = entry
let adjacent = adjacent_counter(grid, pos)
@@ -124,9 +124,9 @@ fn part2(input: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test11")
- let assert 37 = part1(test)
- let assert 26 = part2(test)
+ let testing = input_util.read_text("test11")
+ let assert 37 = part1(testing)
+ let assert 26 = part2(testing)
let input = input_util.read_text("day11")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day12.gleam b/aoc-2020-gleam/src/days/day12.gleam
index 3df7ce8..e6e7c6a 100644
--- a/aoc-2020-gleam/src/days/day12.gleam
+++ b/aoc-2020-gleam/src/days/day12.gleam
@@ -3,8 +3,8 @@ import gleam/int
import gleam/list
import gleam/string as str
import util/input_util
-import util/pos2.{Pos2}
-import util/dir.{Dir, East, North, South, West}
+import util/pos2.{type Pos2}
+import util/dir.{type Dir, East, North, South, West}
type Instr {
MoveIn(dir: Dir, by: Int)
@@ -23,6 +23,7 @@ fn parse_instr(line: String) -> Instr {
"L" -> Turn(by: dir.degree_to_turn(-value))
"R" -> Turn(by: dir.degree_to_turn(value))
"F" -> MoveForward(by: value)
+ _ -> panic
}
}
@@ -111,9 +112,9 @@ fn part2(lines: List(String)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_lines("test12")
- let assert 25 = part1(test)
- let assert 286 = part2(test)
+ let testing = input_util.read_lines("test12")
+ let assert 25 = part1(testing)
+ let assert 286 = part2(testing)
let input = input_util.read_lines("day12")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day13.gleam b/aoc-2020-gleam/src/days/day13.gleam
index c95cd42..a7307dd 100644
--- a/aoc-2020-gleam/src/days/day13.gleam
+++ b/aoc-2020-gleam/src/days/day13.gleam
@@ -71,7 +71,7 @@ fn part2(input: String) -> Int {
|> iter.from_list
|> iter.index
|> iter.flat_map(fn(entry) {
- let #(i, b) = entry
+ let #(b, i) = entry
case b {
Line(line) -> iter.single(#(line, i))
Any -> iter.empty()
@@ -82,28 +82,25 @@ fn part2(input: String) -> Int {
let assert [#(timestamp, _), ..] = buses
buses
- |> list.fold(
- from: #(timestamp, timestamp),
- with: fn(prev, entry) {
- let #(timestamp, period) = prev
- let #(id, i) = entry
+ |> list.fold(from: #(timestamp, timestamp), with: fn(prev, entry) {
+ let #(timestamp, period) = prev
+ let #(id, i) = entry
- let assert Ok(timestamp) =
- timestamp
- |> iterx.unfold_infinitely(with: int.add(_, period))
- |> iter.find(one_that: fn(t) { { t + i } % id == 0 })
- let period = intx.lcm(period, id)
+ let assert Ok(timestamp) =
+ timestamp
+ |> iterx.unfold_infinitely(with: int.add(_, period))
+ |> iter.find(one_that: fn(t) { { t + i } % id == 0 })
+ let period = intx.lcm(period, id)
- #(timestamp, period)
- },
- )
+ #(timestamp, period)
+ })
|> pair.first
}
pub fn main() -> Nil {
- let test = input_util.read_text("test13")
- let assert 295 = part1(test)
- let assert 1_068_781 = part2(test)
+ let testing = input_util.read_text("test13")
+ let assert 295 = part1(testing)
+ let assert 1_068_781 = part2(testing)
let input = input_util.read_text("day13")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day14.gleam b/aoc-2020-gleam/src/days/day14.gleam
index 38c909a..cc38fae 100644
--- a/aoc-2020-gleam/src/days/day14.gleam
+++ b/aoc-2020-gleam/src/days/day14.gleam
@@ -1,11 +1,10 @@
import gleam/io
import gleam/int
-import gleam/map.{Map}
+import gleam/dict.{type Dict}
import gleam/list
import gleam/pair
import gleam/string as str
import gleam/iterator as iter
-import gleam/bitwise as bit
import ext/resultx as resx
import util/input_util
import util/graph
@@ -22,7 +21,7 @@ type Program =
List(Instr)
type Memory =
- Map(String, Int)
+ Dict(String, Int)
type State =
#(Memory, String)
@@ -67,8 +66,8 @@ fn apply_mask(value: Int, mask: String) -> Int {
|> resx.assert_unwrap
value
- |> bit.or(or_mask)
- |> bit.and(and_mask)
+ |> int.bitwise_or(or_mask)
+ |> int.bitwise_and(and_mask)
}
fn execute_program(
@@ -77,11 +76,11 @@ fn execute_program(
) -> Int {
list.fold(
over: program,
- from: #(map.new(), "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"),
+ from: #(dict.new(), "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"),
with: interpreter,
)
|> pair.first
- |> map.values
+ |> dict.values
|> int.sum
}
@@ -97,6 +96,7 @@ fn memory_locations(from address: String, with mask: String) -> List(String) {
"0" -> a
"1" -> "1"
"X" -> "X"
+ _ -> panic
}
})
|> str.concat
@@ -110,11 +110,12 @@ fn memory_locations(from address: String, with mask: String) -> List(String) {
case head {
"0" | "1" -> iter.single(#(done <> head, rest))
"X" -> iter.from_list([#(done <> "0", rest), #(done <> "1", rest)])
+ _ -> panic
}
}
})
|> iter.map(with: pair.first)
- |> iter.filter(for: fn(res) { str.length(res) == bits })
+ |> iter.filter(keeping: fn(res) { str.length(res) == bits })
|> iter.to_list
}
@@ -126,11 +127,8 @@ fn part1(input: List(String)) -> Int {
case instr {
SetMem(address, value) -> {
#(
- map.insert(
- into: memory,
- for: address,
- insert: apply_mask(value, mask),
- ),
+ memory
+ |> dict.insert(for: address, insert: apply_mask(value, mask)),
mask,
)
}
@@ -148,12 +146,9 @@ fn part2(input: List(String)) -> Int {
SetMem(address, value) -> {
#(
memory_locations(from: address, with: mask)
- |> list.fold(
- from: memory,
- with: fn(memory, address) {
- map.insert(into: memory, for: address, insert: value)
- },
- ),
+ |> list.fold(from: memory, with: fn(memory, address) {
+ dict.insert(into: memory, for: address, insert: value)
+ }),
mask,
)
}
@@ -163,9 +158,9 @@ fn part2(input: List(String)) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_lines("test14")
- let assert 51 = part1(test)
- let assert 208 = part2(test)
+ let testing = input_util.read_lines("test14")
+ let assert 51 = part1(testing)
+ let assert 208 = part2(testing)
let input = input_util.read_lines("day14")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day15.gleam b/aoc-2020-gleam/src/days/day15.gleam
index 55ef0b4..83f2bb8 100644
--- a/aoc-2020-gleam/src/days/day15.gleam
+++ b/aoc-2020-gleam/src/days/day15.gleam
@@ -1,7 +1,7 @@
import gleam/io
import gleam/int
import gleam/list
-import gleam/map
+import gleam/dict
import gleam/string as str
import gleam/function as fun
import gleam/iterator as iter
@@ -16,25 +16,23 @@ fn solve(input: String, nth: Int) -> Int {
let history =
starting
- |> list.index_map(fn(index, number) { #(number, index + 1) })
- |> map.from_list
+ |> list.index_map(fn(number, index) { #(number, index + 1) })
+ |> dict.from_list
let turn = list.length(starting)
let assert Ok(last) = list.last(starting)
- iterx.unfold_infinitely(
- from: #(history, turn, last),
- with: fn(state) {
- let #(history, turn, last) = state
- #(
- map.insert(into: history, for: last, insert: turn),
- turn + 1,
- case map.get(history, last) {
- Ok(previous) -> turn - previous
- Error(Nil) -> 0
- },
- )
- },
- )
+ iterx.unfold_infinitely(from: #(history, turn, last), with: fn(state) {
+ let #(history, turn, last) = state
+ #(
+ dict.insert(into: history, for: last, insert: turn),
+ turn
+ + 1,
+ case dict.get(history, last) {
+ Ok(previous) -> turn - previous
+ Error(Nil) -> 0
+ },
+ )
+ })
|> iter.filter(fn(state) { state.1 == nth })
|> iter.map(fn(state) { state.2 })
|> iter.first
diff --git a/aoc-2020-gleam/src/days/day16.gleam b/aoc-2020-gleam/src/days/day16.gleam
index d978ac7..d56aea2 100644
--- a/aoc-2020-gleam/src/days/day16.gleam
+++ b/aoc-2020-gleam/src/days/day16.gleam
@@ -4,7 +4,7 @@ import gleam/list
import gleam/bool
import gleam/pair
import gleam/string as str
-import gleam/map.{Map}
+import gleam/dict.{type Dict}
import ext/resultx as resx
import ext/genericx as genx
import util/input_util
@@ -22,7 +22,7 @@ type Ticket =
type Notes {
Notes(
- fields: Map(String, Rule),
+ fields: Dict(String, Rule),
your_ticket: Ticket,
nearby_tickets: List(Ticket),
)
@@ -55,7 +55,7 @@ fn parse_notes(input: String) -> Notes {
let notes_parser =
field_parser
|> p.sep1(by: p.nl())
- |> p.map(with: map.from_list)
+ |> p.map(with: dict.from_list)
|> p.skip(p.nlnl())
|> p.skip(p.literal("your ticket:"))
|> p.skip(p.nl())
@@ -88,15 +88,12 @@ type Column {
fn collapse_columns(columns: List(Column)) -> List(String) {
case
- list.find_map(
- in: columns,
- with: fn(column) {
- case column {
- Pending([name]) -> Ok(name)
- _ -> Error(Nil)
- }
- },
- )
+ list.find_map(in: columns, with: fn(column) {
+ case column {
+ Pending([name]) -> Ok(name)
+ _ -> Error(Nil)
+ }
+ })
{
Ok(target) ->
columns
@@ -106,19 +103,17 @@ fn collapse_columns(columns: List(Column)) -> List(String) {
Pending([_]) -> Collapsed(target)
Pending(names) ->
names
- |> list.filter(for: genx.different(_, than: target))
+ |> list.filter(keeping: genx.different(_, than: target))
|> Pending
}
})
|> collapse_columns
Error(Nil) ->
- list.map(
- columns,
- with: fn(column) {
- let assert Collapsed(name) = column
- name
- },
- )
+ columns
+ |> list.map(with: fn(column) {
+ let assert Collapsed(name) = column
+ name
+ })
}
}
@@ -127,9 +122,9 @@ fn part1(input: String) -> Int {
notes.nearby_tickets
|> list.flatten
- |> list.filter(for: fn(value) {
+ |> list.filter(keeping: fn(value) {
notes.fields
- |> map.values
+ |> dict.values
|> list.any(satisfying: satisfies_rule(value, _))
|> bool.negate
})
@@ -140,20 +135,17 @@ fn part2(input: String) -> Int {
let notes = parse_notes(input)
[notes.your_ticket, ..notes.nearby_tickets]
- |> list.filter(for: fn(ticket) {
- list.all(
- in: ticket,
- satisfying: fn(value) {
- notes.fields
- |> map.values
- |> list.any(satisfying: satisfies_rule(value, _))
- },
- )
+ |> list.filter(keeping: fn(ticket) {
+ list.all(in: ticket, satisfying: fn(value) {
+ notes.fields
+ |> dict.values
+ |> list.any(satisfying: satisfies_rule(value, _))
+ })
})
|> list.transpose
|> list.map(with: fn(column) {
notes.fields
- |> map.to_list
+ |> dict.to_list
|> list.filter_map(with: fn(entry) {
let #(name, rule) = entry
case list.all(in: column, satisfying: satisfies_rule(_, rule)) {
@@ -175,9 +167,9 @@ fn part2(input: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test16")
- let assert 71 = part1(test)
- let assert 1 = part2(test)
+ let testing = input_util.read_text("test16")
+ let assert 71 = part1(testing)
+ let assert 1 = part2(testing)
let input = input_util.read_text("day16")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day17.gleam b/aoc-2020-gleam/src/days/day17.gleam
index 78603e0..e17439c 100644
--- a/aoc-2020-gleam/src/days/day17.gleam
+++ b/aoc-2020-gleam/src/days/day17.gleam
@@ -2,7 +2,7 @@ import gleam/io
import gleam/list
import gleam/bool
import gleam/string as str
-import gleam/set.{Set}
+import gleam/set.{type Set}
import util/input_util
import util/pos3
import util/pos4
@@ -10,10 +10,10 @@ import util/pos4
fn parse_grid(input: String, with constructor: fn(Int, Int) -> a) -> Set(a) {
input
|> str.split(on: "\n")
- |> list.index_map(with: fn(y, line) {
+ |> list.index_map(with: fn(line, y) {
line
|> str.to_graphemes
- |> list.index_map(with: fn(x, grapheme) {
+ |> list.index_map(with: fn(grapheme, x) {
case grapheme {
"#" -> [constructor(x, y)]
"." -> []
@@ -34,15 +34,12 @@ fn cycle(
use <- bool.guard(when: times == 0, return: grid)
grid
- |> set.fold(
- from: set.new(),
- with: fn(acc, pos) {
- acc
- |> set.insert(pos)
- |> set.union(neighbours(pos))
- },
- )
- |> set.filter(for: fn(pos) {
+ |> set.fold(from: set.new(), with: fn(acc, pos) {
+ acc
+ |> set.insert(pos)
+ |> set.union(neighbours(pos))
+ })
+ |> set.filter(keeping: fn(pos) {
let active = set.contains(in: grid, this: pos)
let count =
pos
@@ -76,9 +73,9 @@ fn part2(input: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test17")
- let assert 112 = part1(test)
- let assert 848 = part2(test)
+ let testing = input_util.read_text("test17")
+ let assert 112 = part1(testing)
+ let assert 848 = part2(testing)
let input = input_util.read_text("day17")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day19.gleam b/aoc-2020-gleam/src/days/day19.gleam
index bfee1e8..5838d4a 100644
--- a/aoc-2020-gleam/src/days/day19.gleam
+++ b/aoc-2020-gleam/src/days/day19.gleam
@@ -2,7 +2,7 @@ import gleam/io
import gleam/list
import gleam/bool
import gleam/string as str
-import gleam/map.{Map}
+import gleam/dict.{type Dict}
import ext/listx
import ext/resultx as resx
import util/input_util
@@ -14,7 +14,7 @@ type Rule {
}
type Ruleset =
- Map(Int, Rule)
+ Dict(Int, Rule)
fn parse_input(input: String) -> #(Ruleset, List(String)) {
let rule_parser =
@@ -26,7 +26,7 @@ fn parse_input(input: String) -> #(Ruleset, List(String)) {
|> p.skip(p.literal("\""))
|> p.map(with: Literal)
|> p.or(
- else: p.int()
+ otherwise: p.int()
|> p.sep1(by: p.literal(" "))
|> p.sep1(by: p.literal(" | "))
|> p.map(with: Reference),
@@ -37,7 +37,7 @@ fn parse_input(input: String) -> #(Ruleset, List(String)) {
let input_parser =
rule_parser
|> p.sep1(by: p.nl())
- |> p.map(with: map.from_list)
+ |> p.map(with: dict.from_list)
|> p.skip(p.nlnl())
|> p.then(
p.str1_until_ws()
@@ -57,7 +57,7 @@ fn matches(
) -> Bool {
case #(gc_queue, rule_queue) {
#([gc, ..rest], [rule_id, ..rule_queue]) -> {
- let assert Ok(rule) = map.get(ruleset, rule_id)
+ let assert Ok(rule) = dict.get(ruleset, rule_id)
case rule {
Literal(expected) -> {
use <- bool.guard(when: gc != expected, return: False)
@@ -92,16 +92,16 @@ fn part2(input: String) -> Int {
let #(ruleset, messages) = parse_input(input)
let ruleset =
ruleset
- |> map.insert(for: 8, insert: Reference([[42], [42, 8]]))
- |> map.insert(for: 11, insert: Reference([[42, 31], [42, 11, 31]]))
+ |> dict.insert(for: 8, insert: Reference([[42], [42, 8]]))
+ |> dict.insert(for: 11, insert: Reference([[42, 31], [42, 11, 31]]))
solve(for: messages, under: ruleset)
}
pub fn main() -> Nil {
- let test = input_util.read_text("test19")
- let assert 3 = part1(test)
- let assert 12 = part2(test)
+ let testing = input_util.read_text("test19")
+ let assert 3 = part1(testing)
+ let assert 12 = part2(testing)
let input = input_util.read_text("day19")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day22.gleam b/aoc-2020-gleam/src/days/day22.gleam
index b925473..1f02f9f 100644
--- a/aoc-2020-gleam/src/days/day22.gleam
+++ b/aoc-2020-gleam/src/days/day22.gleam
@@ -4,9 +4,9 @@ import gleam/list
import gleam/bool
import gleam/string as str
import gleam/function as fun
-import gleam/set.{Set}
+import gleam/set.{type Set}
import gleam/order.{Eq, Gt, Lt}
-import gleam/option.{None, Option, Some}
+import gleam/option.{type Option, None, Some}
import ext/boolx
import ext/resultx as resx
import util/input_util
@@ -39,10 +39,9 @@ fn parse_game(input: String) -> Game {
fn score(deck: List(Int)) -> Int {
deck
|> list.reverse
- |> list.index_fold(
- from: 0,
- with: fn(sum, card, index) { sum + card * { index + 1 } },
- )
+ |> list.index_fold(from: 0, with: fn(sum, card, index) {
+ sum + card * { index + 1 }
+ })
}
fn outcome(game: Game) -> Option(Int) {
@@ -87,7 +86,8 @@ fn play_recursive_combat(game: Game, seen: Set(Game)) -> Int {
let winner = {
use <- boolx.guard_lazy(
- when: list.length(rest1) >= top1 && list.length(rest2) >= top2,
+ when: list.length(rest1) >= top1
+ && list.length(rest2) >= top2,
return: fn() {
Game(list.take(rest1, top1), list.take(rest2, top2))
|> play_recursive_combat(set.new())
@@ -119,9 +119,9 @@ fn part2(input: String) -> Int {
}
pub fn main() -> Nil {
- let test = input_util.read_text("test22")
- let assert 306 = part1(test)
- let assert 291 = part2(test)
+ let testing = input_util.read_text("test22")
+ let assert 306 = part1(testing)
+ let assert 291 = part2(testing)
let input = input_util.read_text("day22")
io.debug(part1(input))
diff --git a/aoc-2020-gleam/src/days/day23.gleam b/aoc-2020-gleam/src/days/day23.gleam
index 8b58fa7..72e6c4d 100644
--- a/aoc-2020-gleam/src/days/day23.gleam
+++ b/aoc-2020-gleam/src/days/day23.gleam
@@ -4,7 +4,7 @@ import gleam/bool
import gleam/string as str
import gleam/function as fun
import gleam/iterator as iter
-import gleam/map.{Map}
+import gleam/dict.{type Dict}
import gleam/list.{Continue, Stop}
import ext/resultx as resx
import ext/iteratorx as iterx
@@ -15,7 +15,7 @@ fn parse_input(input: String) -> List(Int) {
|> list.map(with: fun.compose(int.parse, resx.assert_unwrap))
}
-fn play(input: String, bound: Int, moves: Int) -> Map(Int, Int) {
+fn play(input: String, bound: Int, moves: Int) -> Dict(Int, Int) {
let assert [first, ..tail] = parse_input(input)
let assert Ok(second) = list.first(tail)
@@ -24,24 +24,24 @@ fn play(input: String, bound: Int, moves: Int) -> Map(Int, Int) {
True -> list.range(from: 10, to: bound)
False -> []
})
- |> build_map(map.from_list([#(first, second)]), first)
+ |> build_dict(dict.from_list([#(first, second)]), first)
|> move(first, moves, bound)
}
fn move(
- cups: Map(Int, Int),
+ cups: Dict(Int, Int),
current: Int,
round: Int,
bound: Int,
-) -> Map(Int, Int) {
+) -> Dict(Int, Int) {
use <- bool.guard(when: round == 0, return: cups)
// Remove nodes from source
- let assert Ok(first) = map.get(cups, current)
- let assert Ok(second) = map.get(cups, first)
- let assert Ok(third) = map.get(cups, second)
- let assert Ok(rest) = map.get(cups, third)
- let cups = map.insert(into: cups, for: current, insert: rest)
+ let assert Ok(first) = dict.get(cups, current)
+ let assert Ok(second) = dict.get(cups, first)
+ let assert Ok(third) = dict.get(cups, second)
+ let assert Ok(rest) = dict.get(cups, third)
+ let cups = dict.insert(into: cups, for: current, insert: rest)
// Insert nodes at destination
let assert Ok(before) =
@@ -50,46 +50,48 @@ fn move(
|> iter.find(one_that: fn(key) {
!list.contains([first, second, third], key)
})
- let assert Ok(after) = map.get(cups, before)
+ let assert Ok(after) = dict.get(cups, before)
let cups =
cups
- |> map.insert(for: before, insert: first)
- |> map.insert(for: third, insert: after)
+ |> dict.insert(for: before, insert: first)
+ |> dict.insert(for: third, insert: after)
cups
|> move(
cups
- |> map.get(current)
+ |> dict.get(current)
|> resx.assert_unwrap,
- round - 1,
+ round
+ - 1,
bound,
)
}
-fn build_map(list: List(Int), cups: Map(Int, Int), first: Int) -> Map(Int, Int) {
+fn build_dict(
+ list: List(Int),
+ cups: Dict(Int, Int),
+ first: Int,
+) -> Dict(Int, Int) {
case list {
- [] -> map.new()
- [head] -> map.insert(into: cups, for: head, insert: first)
+ [] -> dict.new()
+ [head] -> dict.insert(into: cups, for: head, insert: first)
[head, ..tail] -> {
let assert Ok(second) = list.first(tail)
- build_map(tail, map.insert(into: cups, for: head, insert: second), first)
+ build_dict(tail, dict.insert(into: cups, for: head, insert: second), first,
+ )
}
}
}
-fn to_result_string(cups: Map(Int, Int)) -> String {
- iterx.unfold_infinitely(
- from: 1,
- with: fn(key) { resx.assert_unwrap(map.get(cups, key)) },
- )
+fn to_result_string(cups: Dict(Int, Int)) -> String {
+ iterx.unfold_infinitely(from: 1, with: fn(key) {
+ resx.assert_unwrap(dict.get(cups, key))
+ })
|> iter.drop(1)
- |> iter.fold_until(
- from: "",
- with: fn(acc, key) {
- use <- bool.guard(when: key == 1, return: Stop(acc))
- Continue(acc <> int.to_string(key))
- },
- )
+ |> iter.fold_until(from: "", with: fn(acc, key) {
+ use <- bool.guard(when: key == 1, return: Stop(acc))
+ Continue(acc <> int.to_string(key))
+ })
}
fn part1(input: String) -> String {
@@ -100,15 +102,15 @@ fn part1(input: String) -> String {
fn part2(input: String) -> Int {
let finished = play(input, 1_000_000, 10_000_000)
- let assert Ok(first) = map.get(finished, 1)
- let assert Ok(second) = map.get(finished, first)
+ let assert Ok(first) = dict.get(finished, 1)
+ let assert Ok(second) = dict.get(finished, first)
first * second
}
pub fn main() -> Nil {
- let test = "389125467"
- let assert "67384529" = part1(test)
- let assert 149_245_887_792 = part2(test)
+ let testing = "389125467"
+ let assert "67384529" = part1(testing)
+ let assert 149_245_887_792 = part2(testing)
let input = "925176834"
io.println(part1(input))
diff --git a/aoc-2020-gleam/src/ext/dictx.gleam b/aoc-2020-gleam/src/ext/dictx.gleam
new file mode 100644
index 0000000..97b57b8
--- /dev/null
+++ b/aoc-2020-gleam/src/ext/dictx.gleam
@@ -0,0 +1,14 @@
+import gleam/dict.{type Dict}
+import gleam/iterator.{type Iterator} as iter
+
+pub fn from_iter(iterator: Iterator(#(k, v))) -> Dict(k, v) {
+ iter.fold(over: iterator, from: dict.new(), with: fn(acc, cur) {
+ dict.insert(acc, cur.0, cur.1)
+ })
+}
+
+pub fn to_iter(map: Dict(k, v)) -> Iterator(#(k, v)) {
+ map
+ |> dict.to_list
+ |> iter.from_list
+}
diff --git a/aoc-2020-gleam/src/ext/iteratorx.gleam b/aoc-2020-gleam/src/ext/iteratorx.gleam
index 4e184bd..0e59f17 100644
--- a/aoc-2020-gleam/src/ext/iteratorx.gleam
+++ b/aoc-2020-gleam/src/ext/iteratorx.gleam
@@ -1,4 +1,4 @@
-import gleam/iterator.{Iterator, Next} as iter
+import gleam/iterator.{type Iterator, Next} as iter
pub fn length(iterator: Iterator(a)) -> Int {
iterator
@@ -7,7 +7,7 @@ pub fn length(iterator: Iterator(a)) -> Int {
pub fn count(iterator: Iterator(a), satisfying predicate: fn(a) -> Bool) -> Int {
iterator
- |> iter.filter(for: predicate)
+ |> iter.filter(keeping: predicate)
|> length
}
diff --git a/aoc-2020-gleam/src/ext/listx.gleam b/aoc-2020-gleam/src/ext/listx.gleam
index 8d20bd6..2ca8648 100644
--- a/aoc-2020-gleam/src/ext/listx.gleam
+++ b/aoc-2020-gleam/src/ext/listx.gleam
@@ -25,6 +25,6 @@ pub fn index_of(list: List(a), value: a) -> Result(Int, Nil) {
list
|> iter.from_list
|> iter.index
- |> iter.find(one_that: fn(elem) { pair.second(elem) == value })
- |> res.map(with: pair.first)
+ |> iter.find(one_that: fn(elem) { pair.first(elem) == value })
+ |> res.map(with: pair.second)
}
diff --git a/aoc-2020-gleam/src/ext/mapx.gleam b/aoc-2020-gleam/src/ext/mapx.gleam
deleted file mode 100644
index 244c1ac..0000000
--- a/aoc-2020-gleam/src/ext/mapx.gleam
+++ /dev/null
@@ -1,16 +0,0 @@
-import gleam/map.{Map}
-import gleam/iterator.{Iterator} as iter
-
-pub fn from_iter(iterator: Iterator(#(k, v))) -> Map(k, v) {
- iter.fold(
- over: iterator,
- from: map.new(),
- with: fn(acc, cur) { map.insert(acc, cur.0, cur.1) },
- )
-}
-
-pub fn to_iter(map: Map(k, v)) -> Iterator(#(k, v)) {
- map
- |> map.to_list
- |> iter.from_list
-}
diff --git a/aoc-2020-gleam/src/ext/setx.gleam b/aoc-2020-gleam/src/ext/setx.gleam
index 57e368e..33ebbc3 100644
--- a/aoc-2020-gleam/src/ext/setx.gleam
+++ b/aoc-2020-gleam/src/ext/setx.gleam
@@ -1,4 +1,4 @@
-import gleam/set.{Set}
+import gleam/set.{type Set}
import gleam/iterator as iter
import ext/iteratorx as iterx
diff --git a/aoc-2020-gleam/src/util/cache.gleam b/aoc-2020-gleam/src/util/cache.gleam
index e1eefd2..c007d35 100644
--- a/aoc-2020-gleam/src/util/cache.gleam
+++ b/aoc-2020-gleam/src/util/cache.gleam
@@ -1,6 +1,6 @@
-import gleam/map.{Map}
-import gleam/otp/actor.{Continue, Next, Stop}
-import gleam/erlang/process.{Normal, Subject}
+import gleam/dict.{type Dict}
+import gleam/otp/actor.{type Next, Stop}
+import gleam/erlang/process.{type Subject, Normal}
const timeout = 1000
@@ -13,14 +13,18 @@ type Message(k, v) {
type Server(k, v) =
Subject(Message(k, v))
-fn handle_message(message: Message(k, v), dict: Map(k, v)) -> Next(Map(k, v)) {
+fn handle_message(
+ message: Message(k, v),
+ map: Dict(k, v),
+) -> Next(Message(k, v), Dict(k, v)) {
case message {
Shutdown -> Stop(Normal)
Get(key, client) -> {
- process.send(client, map.get(dict, key))
- Continue(dict)
+ process.send(client, dict.get(map, key))
+
+ actor.continue(map)
}
- Set(key, value) -> Continue(map.insert(dict, key, value))
+ Set(key, value) -> actor.continue(dict.insert(map, key, value))
}
}
@@ -29,7 +33,7 @@ pub opaque type Cache(k, v) {
}
pub fn create(apply fun: fn(Cache(k, v)) -> t) -> t {
- let assert Ok(server) = actor.start(map.new(), handle_message)
+ let assert Ok(server) = actor.start(dict.new(), handle_message)
let result = fun(Cache(server))
process.send(server, Shutdown)
result
diff --git a/aoc-2020-gleam/src/util/dir.gleam b/aoc-2020-gleam/src/util/dir.gleam
index 194d712..d445d86 100644
--- a/aoc-2020-gleam/src/util/dir.gleam
+++ b/aoc-2020-gleam/src/util/dir.gleam
@@ -2,7 +2,7 @@ import gleam/int
import gleam/iterator as iter
import ext/resultx as resx
import ext/iteratorx as iterx
-import util/pos2.{Pos2}
+import util/pos2.{type Pos2}
pub type Dir {
North
diff --git a/aoc-2020-gleam/src/util/graph.gleam b/aoc-2020-gleam/src/util/graph.gleam
index d9aa5aa..5d02fff 100644
--- a/aoc-2020-gleam/src/util/graph.gleam
+++ b/aoc-2020-gleam/src/util/graph.gleam
@@ -1,6 +1,6 @@
import gleam/list
-import gleam/iterator.{Iterator} as iter
-import gleam/set.{Set}
+import gleam/iterator.{type Iterator} as iter
+import gleam/set.{type Set}
fn dfs_helper(
neighbours: fn(a) -> Iterator(a),
@@ -14,7 +14,7 @@ fn dfs_helper(
neighbours,
stack: node
|> neighbours
- |> iter.filter(for: fn(n) { !set.contains(visited, n) })
+ |> iter.filter(keeping: fn(n) { !set.contains(visited, n) })
|> iter.to_list
|> list.append(stack),
visited: visited
@@ -26,10 +26,7 @@ fn dfs_helper(
}
pub fn dfs(from start: a, with neighbours: fn(a) -> Iterator(a)) -> Iterator(a) {
- iter.from_list(dfs_helper(
- neighbours,
- stack: [start],
- visited: set.new(),
- acc: [],
- ))
+ iter.from_list(
+ dfs_helper(neighbours, stack: [start], visited: set.new(), acc: []),
+ )
}
diff --git a/aoc-2020-gleam/src/util/input_util.gleam b/aoc-2020-gleam/src/util/input_util.gleam
index 27f3009..f4f6812 100644
--- a/aoc-2020-gleam/src/util/input_util.gleam
+++ b/aoc-2020-gleam/src/util/input_util.gleam
@@ -3,7 +3,7 @@ import gleam/list
import gleam/bool
import gleam/string as str
import gleam/function as fun
-import gleam/erlang/file
+import simplifile as file
import ext/resultx as resx
pub fn read_text(filename: String) -> String {
@@ -17,7 +17,7 @@ pub fn read_lines(filename: String) -> List(String) {
|> read_text
|> str.split(on: "\n")
|> list.map(with: str.trim)
- |> list.filter(for: fun.compose(str.is_empty, bool.negate))
+ |> list.filter(keeping: fun.compose(str.is_empty, bool.negate))
}
pub fn read_numbers(filename: String) -> List(Int) {
diff --git a/aoc-2020-gleam/src/util/parser.gleam b/aoc-2020-gleam/src/util/parser.gleam
index 1d4bba9..3b2e20e 100644
--- a/aoc-2020-gleam/src/util/parser.gleam
+++ b/aoc-2020-gleam/src/util/parser.gleam
@@ -5,7 +5,7 @@ import gleam/bool
import gleam/string as str
import gleam/result as res
import gleam/function as fun
-import gleam/option.{None, Option, Some} as opt
+import gleam/option.{None, type Option, Some}
// Heavily inspired by https://fsharpforfunandprofit.com/posts/understanding-parser-combinators/
@@ -188,19 +188,19 @@ pub fn then_3rd(two: Parser(#(a, b)), third: Parser(c)) -> Parser(#(a, b, c)) {
})
}
-pub fn or(first: Parser(a), else second: Parser(a)) -> Parser(a) {
+pub fn or(first: Parser(a), otherwise second: Parser(a)) -> Parser(a) {
create(fn(input) {
first
|> run(on: input)
|> res.or(run(second, on: input))
})
- |> labeled(with: first.label <> " |> or(else: " <> second.label <> ")")
+ |> labeled(with: first.label <> " |> or(otherwise: " <> second.label <> ")")
}
pub fn opt(parser: Parser(a)) -> Parser(Option(a)) {
parser
|> map(with: Some)
- |> or(else: succeeding(with: None))
+ |> or(otherwise: succeeding(with: None))
|> labeled(with: "opt(" <> parser.label <> ")")
}
@@ -340,7 +340,7 @@ pub fn sep1(parser: Parser(a), by separator: Parser(b)) -> Parser(List(a)) {
pub fn sep0(parser: Parser(a), by separator: Parser(b)) -> Parser(List(a)) {
parser
|> sep1(by: separator)
- |> or(else: succeeding(with: []))
+ |> or(otherwise: succeeding(with: []))
|> labeled(
with: "sep0(" <> parser.label <> ", by: " <> separator.label <> ")",
)
diff --git a/aoc-2020-gleam/src/util/pos2.gleam b/aoc-2020-gleam/src/util/pos2.gleam
index 4de2ab9..0b6256c 100644
--- a/aoc-2020-gleam/src/util/pos2.gleam
+++ b/aoc-2020-gleam/src/util/pos2.gleam
@@ -1,7 +1,7 @@
import gleam/int
import gleam/bool
import gleam/list
-import gleam/set.{Set}
+import gleam/set.{type Set}
pub type Pos2 =
#(Int, Int)
diff --git a/aoc-2020-gleam/src/util/pos3.gleam b/aoc-2020-gleam/src/util/pos3.gleam
index 5525607..3787c19 100644
--- a/aoc-2020-gleam/src/util/pos3.gleam
+++ b/aoc-2020-gleam/src/util/pos3.gleam
@@ -1,6 +1,6 @@
import gleam/list
import gleam/bool
-import gleam/set.{Set}
+import gleam/set.{type Set}
pub type Pos3 =
#(Int, Int, Int)
diff --git a/aoc-2020-gleam/src/util/pos4.gleam b/aoc-2020-gleam/src/util/pos4.gleam
index 3eda4c5..a96bcca 100644
--- a/aoc-2020-gleam/src/util/pos4.gleam
+++ b/aoc-2020-gleam/src/util/pos4.gleam
@@ -1,6 +1,6 @@
import gleam/list
import gleam/bool
-import gleam/set.{Set}
+import gleam/set.{type Set}
pub type Pos4 =
#(Int, Int, Int, Int)