aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-02-22 13:34:58 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-02-22 13:34:58 +0100
commit5746dbca0ddaef455b6f26fba3945f3533d0b2c1 (patch)
tree855e48c9d25a0136351947dfe17140d40568e8fd /aoc-2020-gleam
parented0763c9dce58a53715415f31146b6f670519d76 (diff)
downloadgleam_aoc2020-5746dbca0ddaef455b6f26fba3945f3533d0b2c1.tar.gz
gleam_aoc2020-5746dbca0ddaef455b6f26fba3945f3533d0b2c1.zip
Add aliases to long imports
Diffstat (limited to 'aoc-2020-gleam')
-rw-r--r--aoc-2020-gleam/src/days/day01.gleam8
-rw-r--r--aoc-2020-gleam/src/days/day02.gleam13
-rw-r--r--aoc-2020-gleam/src/days/day03.gleam18
-rw-r--r--aoc-2020-gleam/src/days/day04.gleam18
-rw-r--r--aoc-2020-gleam/src/days/day05.gleam14
-rw-r--r--aoc-2020-gleam/src/days/day06.gleam12
-rw-r--r--aoc-2020-gleam/src/days/day07.gleam8
-rw-r--r--aoc-2020-gleam/src/ext/resultx.gleam2
-rw-r--r--aoc-2020-gleam/src/util/input_util.gleam16
-rw-r--r--aoc-2020-gleam/src/util/parser.gleam54
-rw-r--r--aoc-2020-gleam/src/util/runner.gleam16
11 files changed, 90 insertions, 89 deletions
diff --git a/aoc-2020-gleam/src/days/day01.gleam b/aoc-2020-gleam/src/days/day01.gleam
index ac46581..0d2a994 100644
--- a/aoc-2020-gleam/src/days/day01.gleam
+++ b/aoc-2020-gleam/src/days/day01.gleam
@@ -1,16 +1,16 @@
import gleam/io
import gleam/int
import gleam/list
-import gleam/result
-import ext/resultx
+import gleam/result as res
+import ext/resultx as resx
import util/input_util
fn solve(numbers: List(Int), n: Int) -> Int {
numbers
|> list.combinations(by: n)
|> list.find(one_that: fn(p) { int.sum(p) == 2020 })
- |> result.map(with: int.product)
- |> resultx.force_unwrap
+ |> res.map(with: int.product)
+ |> resx.assert_unwrap
}
fn part1(numbers: List(Int)) -> Int {
diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam
index 3f77384..6d2e743 100644
--- a/aoc-2020-gleam/src/days/day02.gleam
+++ b/aoc-2020-gleam/src/days/day02.gleam
@@ -1,9 +1,9 @@
import gleam/io
import gleam/list
-import gleam/string
import gleam/bool
-import ext/resultx
+import gleam/string as str
import ext/listx
+import ext/resultx as resx
import util/input_util
import util/parser as p
@@ -49,7 +49,7 @@ fn part1(lines: List(String)) -> Int {
lines,
fn(line) {
line.password
- |> string.to_graphemes
+ |> str.to_graphemes
|> listx.count(satisfying: fn(g) { g == line.policy.grapheme })
|> fn(l) { line.policy.min <= l && l <= line.policy.max }
},
@@ -60,10 +60,11 @@ fn part2(lines: List(String)) -> Int {
solve(
lines,
fn(line) {
- let graphemes = string.to_graphemes(line.password)
let grapheme_matches = fn(idx) {
- list.at(in: graphemes, get: idx - 1)
- |> resultx.force_unwrap == line.policy.grapheme
+ line.password
+ |> str.to_graphemes
+ |> list.at(idx - 1)
+ |> resx.assert_unwrap == line.policy.grapheme
}
bool.exclusive_or(
grapheme_matches(line.policy.min),
diff --git a/aoc-2020-gleam/src/days/day03.gleam b/aoc-2020-gleam/src/days/day03.gleam
index da67658..6f256bf 100644
--- a/aoc-2020-gleam/src/days/day03.gleam
+++ b/aoc-2020-gleam/src/days/day03.gleam
@@ -1,11 +1,11 @@
-import gleam/string
import gleam/list
-import gleam/function
import gleam/io
-import gleam/iterator as iter
import gleam/int
+import gleam/string as str
+import gleam/function as fun
+import gleam/iterator as iter
import gleam/set.{Set}
-import ext/resultx
+import ext/resultx as resx
import ext/iteratorx as iterx
import util/input_util
@@ -27,7 +27,7 @@ type Area {
}
fn parse_area(from text: String) -> Area {
- let lines = string.split(text, on: "\n")
+ let lines = str.split(text, on: "\n")
let trees =
list.index_fold(
@@ -35,14 +35,14 @@ fn parse_area(from text: String) -> Area {
from: set.new(),
with: fn(prev, line, y) {
line
- |> string.to_graphemes
+ |> str.to_graphemes
|> list.index_map(with: fn(x, grapheme) {
case grapheme {
"#" -> Ok(#(x, y))
_ -> Error(Nil)
}
})
- |> list.filter_map(with: function.identity)
+ |> list.filter_map(with: fun.identity)
|> set.from_list
|> set.union(prev)
},
@@ -50,8 +50,8 @@ fn parse_area(from text: String) -> Area {
let cycle =
lines
|> list.first
- |> resultx.force_unwrap
- |> string.length
+ |> resx.assert_unwrap
+ |> str.length
let height = list.length(lines)
Area(trees, cycle, height)
diff --git a/aoc-2020-gleam/src/days/day04.gleam b/aoc-2020-gleam/src/days/day04.gleam
index fa165ae..6087e12 100644
--- a/aoc-2020-gleam/src/days/day04.gleam
+++ b/aoc-2020-gleam/src/days/day04.gleam
@@ -1,12 +1,12 @@
import gleam/io
-import gleam/function
import gleam/list
-import gleam/result
+import gleam/function as fun
+import gleam/result as res
import gleam/map.{Map}
-import ext/resultx
import ext/listx
-import util/parser as p
+import ext/resultx as resx
import util/input_util
+import util/parser as p
const allowed_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid"]
@@ -33,7 +33,7 @@ fn parse_passports(from text: String) -> List(Passport) {
let passport_parser =
field_parser
|> p.sep1(by: p.ws_gc())
- |> p.map(with: function.compose(map.from_list, Passport))
+ |> p.map(with: fun.compose(map.from_list, Passport))
|> p.labeled(with: "passport")
let input_parser =
passport_parser
@@ -43,7 +43,7 @@ fn parse_passports(from text: String) -> List(Passport) {
text
|> p.parse_entire(with: input_parser)
- |> resultx.force_unwrap
+ |> resx.assert_unwrap
}
fn is_valid1(passport: Passport) -> Bool {
@@ -109,12 +109,12 @@ fn is_valid2(passport: Passport) -> Bool {
let #(key, parser) = validator
passport.fields
|> map.get(key)
- |> result.then(apply: fn(value) {
+ |> res.then(apply: fn(value) {
value
|> p.parse_entire(with: parser)
- |> result.replace_error(Nil)
+ |> res.replace_error(Nil)
})
- |> result.is_ok
+ |> res.is_ok
},
)
}
diff --git a/aoc-2020-gleam/src/days/day05.gleam b/aoc-2020-gleam/src/days/day05.gleam
index f655be9..b1471f9 100644
--- a/aoc-2020-gleam/src/days/day05.gleam
+++ b/aoc-2020-gleam/src/days/day05.gleam
@@ -1,31 +1,31 @@
import gleam/io
import gleam/int
-import gleam/string
import gleam/list
import gleam/set
+import gleam/string as str
import gleam/iterator as iter
-import ext/resultx
+import ext/resultx as resx
import util/input_util
fn get_seat_id(pass: String) -> Int {
pass
- |> string.to_graphemes
+ |> str.to_graphemes
|> list.map(with: fn(grapheme) {
case grapheme {
"F" | "L" -> "0"
"B" | "R" -> "1"
}
})
- |> string.concat
+ |> str.concat
|> int.base_parse(2)
- |> resultx.force_unwrap
+ |> resx.assert_unwrap
}
fn part1(lines: List(String)) -> Int {
lines
|> list.map(with: get_seat_id)
|> list.reduce(with: int.max)
- |> resultx.force_unwrap
+ |> resx.assert_unwrap
}
fn part2(lines: List(String)) -> Int {
@@ -40,7 +40,7 @@ fn part2(lines: List(String)) -> Int {
in: iter.range(from: 1, to: 1023),
one_that: fn(id) { occupied(id - 1) && !occupied(id) && occupied(id + 1) },
)
- |> resultx.force_unwrap
+ |> resx.assert_unwrap
}
pub fn run() -> Nil {
diff --git a/aoc-2020-gleam/src/days/day06.gleam b/aoc-2020-gleam/src/days/day06.gleam
index 6c3481c..d655d0e 100644
--- a/aoc-2020-gleam/src/days/day06.gleam
+++ b/aoc-2020-gleam/src/days/day06.gleam
@@ -1,11 +1,11 @@
import gleam/io
import gleam/int
-import gleam/string
import gleam/list
+import gleam/string as str
import gleam/set.{Set}
-import ext/resultx
-import util/parser as p
+import ext/resultx as resx
import util/input_util
+import util/parser as p
type Answers =
Set(String)
@@ -17,7 +17,7 @@ type Input =
List(Group)
fn alphabet() -> Set(String) {
- set.from_list(string.to_graphemes("abcdefghijklmnopqrstuvwxyz"))
+ set.from_list(str.to_graphemes("abcdefghijklmnopqrstuvwxyz"))
}
fn parse_input(text: String) -> Input {
@@ -25,7 +25,7 @@ fn parse_input(text: String) -> Input {
p.str1_until_ws()
|> p.map(fn(answer_string) {
answer_string
- |> string.to_graphemes
+ |> str.to_graphemes
|> set.from_list
})
|> p.labeled(with: "answers")
@@ -43,7 +43,7 @@ fn parse_input(text: String) -> Input {
text
|> p.parse_entire(with: input_parser)
- |> resultx.force_unwrap
+ |> resx.assert_unwrap
}
fn fold_group(
diff --git a/aoc-2020-gleam/src/days/day07.gleam b/aoc-2020-gleam/src/days/day07.gleam
index c7c26dc..b99b36f 100644
--- a/aoc-2020-gleam/src/days/day07.gleam
+++ b/aoc-2020-gleam/src/days/day07.gleam
@@ -1,10 +1,10 @@
import gleam/io
import gleam/list
-import gleam/function as fun
import gleam/pair
import gleam/result as res
-import gleam/iterator.{Iterator} as iter
+import gleam/function as fun
import gleam/map.{Map}
+import gleam/iterator.{Iterator} as iter
import ext/resultx as resx
import ext/iteratorx as iterx
import util/graph
@@ -50,7 +50,7 @@ fn parse_graph(lines: List(String)) -> BagGraph {
lines
|> list.map(with: fun.compose(
p.parse_entire(_, with: line_parser),
- resx.force_unwrap,
+ resx.assert_unwrap,
))
|> map.from_list
}
@@ -59,7 +59,7 @@ fn neighbour_fun(graph: BagGraph) -> BagNeighbourFun {
fn(bag) {
graph
|> map.get(bag)
- |> resx.force_unwrap
+ |> resx.assert_unwrap
|> list.map(with: pair.first)
|> iter.from_list
}
diff --git a/aoc-2020-gleam/src/ext/resultx.gleam b/aoc-2020-gleam/src/ext/resultx.gleam
index 1748d77..8898841 100644
--- a/aoc-2020-gleam/src/ext/resultx.gleam
+++ b/aoc-2020-gleam/src/ext/resultx.gleam
@@ -1,4 +1,4 @@
-pub fn force_unwrap(result: Result(t, _)) -> t {
+pub fn assert_unwrap(result: Result(t, _)) -> t {
assert Ok(value) = result
value
}
diff --git a/aoc-2020-gleam/src/util/input_util.gleam b/aoc-2020-gleam/src/util/input_util.gleam
index fbe2d17..3ed2ce8 100644
--- a/aoc-2020-gleam/src/util/input_util.gleam
+++ b/aoc-2020-gleam/src/util/input_util.gleam
@@ -1,27 +1,27 @@
import gleam/int
import gleam/list
-import gleam/string
-import gleam/function
import gleam/bool
+import gleam/string as str
+import gleam/function as fun
import gleam/erlang/file
-import ext/resultx
+import ext/resultx as resx
pub fn read_text(filename: String) -> String {
"data/" <> filename <> ".txt"
|> file.read
- |> resultx.force_unwrap
+ |> resx.assert_unwrap
}
pub fn read_lines(filename: String) -> List(String) {
filename
|> read_text
- |> string.split(on: "\n")
- |> list.map(with: string.trim)
- |> list.filter(for: function.compose(string.is_empty, bool.negate))
+ |> str.split(on: "\n")
+ |> list.map(with: str.trim)
+ |> list.filter(for: fun.compose(str.is_empty, bool.negate))
}
pub fn read_numbers(filename: String) -> List(Int) {
filename
|> read_lines
- |> list.map(with: function.compose(int.parse, resultx.force_unwrap))
+ |> list.map(with: fun.compose(int.parse, resx.assert_unwrap))
}
diff --git a/aoc-2020-gleam/src/util/parser.gleam b/aoc-2020-gleam/src/util/parser.gleam
index 66c20e3..9220679 100644
--- a/aoc-2020-gleam/src/util/parser.gleam
+++ b/aoc-2020-gleam/src/util/parser.gleam
@@ -1,11 +1,11 @@
-import gleam/string
import gleam/list
-import gleam/function
import gleam/pair
-import gleam/result
import gleam/int
import gleam/bool
-import gleam/option.{None, Option, Some}
+import gleam/string as str
+import gleam/result as res
+import gleam/function as fun
+import gleam/option.{None, Option, Some} as opt
// Heavily inspired by https://fsharpforfunandprofit.com/posts/understanding-parser-combinators/
@@ -52,7 +52,7 @@ pub fn labeled(parser: Parser(a), with label: String) -> Parser(a) {
Parser(
fn(input) {
run(parser, on: input)
- |> result.map_error(with: fn(error) {
+ |> res.map_error(with: fn(error) {
case error {
InvalidInput(_, found) -> InvalidInput(label, found)
other -> other
@@ -83,7 +83,7 @@ pub fn parse_entire(
fn gc_satisfying(rule predicate: fn(String) -> Bool) -> Parser(String) {
create(fn(input) {
- case string.pop_grapheme(input) {
+ case str.pop_grapheme(input) {
Ok(#(value, remaining)) ->
case predicate(value) {
True -> Ok(#(value, remaining))
@@ -95,17 +95,17 @@ fn gc_satisfying(rule predicate: fn(String) -> Bool) -> Parser(String) {
}
pub fn any_gc() -> Parser(String) {
- gc_satisfying(rule: function.constant(True))
+ gc_satisfying(rule: fun.constant(True))
|> labeled(with: "any_gc")
}
pub fn gc_in(range allowed: String) -> Parser(String) {
- gc_satisfying(rule: string.contains(allowed, _))
+ gc_satisfying(rule: str.contains(allowed, _))
|> labeled(with: "gc_in(range: " <> q_d(allowed) <> ")")
}
pub fn gc_not_in(range denied: String) -> Parser(String) {
- gc_satisfying(rule: function.compose(string.contains(denied, _), bool.negate))
+ gc_satisfying(rule: fun.compose(str.contains(denied, _), bool.negate))
|> labeled(with: "gc_not_in(range: " <> q_d(denied) <> ")")
}
@@ -137,14 +137,14 @@ pub fn str1_until_ws() -> Parser(String) {
pub fn ignore(parser: Parser(a)) -> Parser(Nil) {
parser
- |> map(function.constant(Nil))
+ |> map(fun.constant(Nil))
}
pub fn then(first: Parser(a), second: Parser(b)) -> Parser(#(a, b)) {
create(fn(input) {
- use parsed1 <- result.then(run(first, on: input))
+ use parsed1 <- res.then(run(first, on: input))
let #(value1, remaining1) = parsed1
- use parsed2 <- result.then(run(second, on: remaining1))
+ use parsed2 <- res.then(run(second, on: remaining1))
let #(value2, remaining2) = parsed2
Ok(#(#(value1, value2), remaining2))
})
@@ -176,7 +176,7 @@ pub fn or(first: Parser(a), else second: Parser(a)) -> Parser(a) {
create(fn(input) {
first
|> run(on: input)
- |> result.or(run(second, on: input))
+ |> res.or(run(second, on: input))
})
|> labeled(with: first.label <> " |> or(else: " <> second.label <> ")")
}
@@ -191,12 +191,12 @@ pub fn opt(parser: Parser(a)) -> Parser(Option(a)) {
pub fn any(of parsers: List(Parser(a))) -> Parser(a) {
parsers
|> list.reduce(with: or)
- |> result.unwrap(or: failing(with: InvalidParser))
+ |> res.unwrap(or: failing(with: InvalidParser))
|> labeled(
"any(of: [" <> {
parsers
|> list.map(with: fn(p) { p.label })
- |> string.join(with: ", ")
+ |> str.join(with: ", ")
} <> "])",
)
}
@@ -211,11 +211,11 @@ fn flat_map(
with mapper: fn(a) -> Result(b, ParseError),
) -> Parser(b) {
create(fn(input) {
- use parsed <- result.then(run(parser, on: input))
+ use parsed <- res.then(run(parser, on: input))
let #(value, remaining) = parsed
value
|> mapper
- |> result.map(with: fn(new_value) { #(new_value, remaining) })
+ |> res.map(with: fn(new_value) { #(new_value, remaining) })
})
|> labeled(with: parser.label)
}
@@ -266,7 +266,7 @@ pub fn seq(of parsers: List(Parser(a))) -> Parser(List(a)) {
with: "seq(of: [" <> {
parsers
|> list.map(with: fn(p) { p.label })
- |> string.join(", ")
+ |> str.join(", ")
} <> "])",
)
}
@@ -274,7 +274,7 @@ pub fn seq(of parsers: List(Parser(a))) -> Parser(List(a)) {
pub fn str_of_seq(of parsers: List(Parser(String))) -> Parser(String) {
parsers
|> seq
- |> map(with: string.concat)
+ |> map(with: str.concat)
}
fn do_zero_or_more(input: String, with parser: Parser(a)) -> #(List(a), String) {
@@ -295,12 +295,12 @@ pub fn many0(of parser: Parser(a)) -> Parser(List(a)) {
pub fn str_of_many0(of parser: Parser(String)) -> Parser(String) {
parser
|> many0
- |> map(with: string.concat)
+ |> map(with: str.concat)
}
pub fn many1(of parser: Parser(a)) -> Parser(List(a)) {
create(fn(input) {
- use parsed <- result.then(run(parser, on: input))
+ use parsed <- res.then(run(parser, on: input))
let #(value, rest) = parsed
let #(previous, rest) = do_zero_or_more(rest, with: parser)
Ok(#([value, ..previous], rest))
@@ -311,7 +311,7 @@ pub fn many1(of parser: Parser(a)) -> Parser(List(a)) {
pub fn str_of_many1(of parser: Parser(String)) -> Parser(String) {
parser
|> many1
- |> map(with: string.concat)
+ |> map(with: str.concat)
}
pub fn sep1(parser: Parser(a), by separator: Parser(b)) -> Parser(List(a)) {
@@ -338,7 +338,7 @@ pub fn int() -> Parser(Int) {
|> flat_map(with: fn(int_string) {
int_string
|> int.parse
- |> result.replace_error(InvalidOperation(ran: "int.parse", with: int_string))
+ |> res.replace_error(InvalidOperation(ran: "int.parse", with: int_string))
})
|> labeled(with: "int")
}
@@ -351,7 +351,7 @@ pub fn any_str_greedy() -> Parser(String) {
pub fn literal(expected: String) -> Parser(String) {
expected
- |> string.to_graphemes
+ |> str.to_graphemes
|> list.map(with: fn(eg) { gc_satisfying(fn(g) { g == eg }) })
|> str_of_seq
|> labeled(with: q_d(expected))
@@ -381,14 +381,14 @@ pub fn repeat(parser: Parser(a), times times: Int) -> Parser(List(a)) {
pub fn satisfying(parser: Parser(a), rule predicate: fn(a) -> Bool) -> Parser(a) {
create(fn(input) {
- use parsed <- result.then(run(parser, on: input))
+ use parsed <- res.then(run(parser, on: input))
let #(value, _) = parsed
case predicate(value) {
True -> Ok(parsed)
False ->
Error(InvalidOperation(
- ran: string.inspect(predicate),
- with: string.inspect(value),
+ ran: str.inspect(predicate),
+ with: str.inspect(value),
))
}
})
diff --git a/aoc-2020-gleam/src/util/runner.gleam b/aoc-2020-gleam/src/util/runner.gleam
index 0ff0a41..938f5b3 100644
--- a/aoc-2020-gleam/src/util/runner.gleam
+++ b/aoc-2020-gleam/src/util/runner.gleam
@@ -1,22 +1,22 @@
import gleam/list
import gleam/int
import gleam/io
-import gleam/result
+import gleam/result as res
import gleam/erlang.{start_arguments}
fn get_day(handler: fn(Int) -> Nil) -> Result(Nil, String) {
let args = start_arguments()
- use first <- result.then(
+ use first <- res.then(
args
|> list.first()
- |> result.replace_error("Pass the day as first argument!"),
+ |> res.replace_error("Pass the day as first argument!"),
)
- use day <- result.then(
+ use day <- res.then(
first
|> int.parse()
- |> result.replace_error("The day argument must be a number!"),
+ |> res.replace_error("The day argument must be a number!"),
)
handler(day)
@@ -26,7 +26,7 @@ fn get_day(handler: fn(Int) -> Nil) -> Result(Nil, String) {
pub fn with_day(handler: fn(Int) -> Nil) -> Nil {
handler
- |> get_day()
- |> result.map_error(io.println)
- |> result.unwrap(Nil)
+ |> get_day
+ |> res.map_error(io.println)
+ |> res.unwrap(or: Nil)
}