aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-02-21 19:46:12 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-02-21 19:46:12 +0100
commit16509ecd4f31ef421a464a98c8e0ea73bb5cb111 (patch)
tree7ac2145ed18cf868abe0b877fecb5988d11fd2ac /aoc-2020-gleam/src/days
parent9c94df2676e8b857eac6c94e827086bf0e8cb850 (diff)
downloadgleam_aoc2020-16509ecd4f31ef421a464a98c8e0ea73bb5cb111.tar.gz
gleam_aoc2020-16509ecd4f31ef421a464a98c8e0ea73bb5cb111.zip
Refactor previous days
Diffstat (limited to 'aoc-2020-gleam/src/days')
-rw-r--r--aoc-2020-gleam/src/days/day02.gleam10
-rw-r--r--aoc-2020-gleam/src/days/day04.gleam30
-rw-r--r--aoc-2020-gleam/src/days/day06.gleam8
3 files changed, 24 insertions, 24 deletions
diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam
index bc8df7c..3f77384 100644
--- a/aoc-2020-gleam/src/days/day02.gleam
+++ b/aoc-2020-gleam/src/days/day02.gleam
@@ -18,15 +18,15 @@ type Line {
fn parse_line(string: String) -> Line {
let policy_parser =
p.int()
- |> p.then_skip(p.grapheme_literal("-"))
+ |> p.then_skip(p.literal("-"))
|> p.then(p.int())
- |> p.then_skip(p.grapheme_literal(" "))
- |> p.then_third(p.any_grapheme())
- |> p.then_skip(p.string_literal(": "))
+ |> p.then_skip(p.literal(" "))
+ |> p.then_3rd(p.any_gc())
+ |> p.then_skip(p.literal(": "))
|> p.map3(with: fn(min, max, grapheme) { Policy(min, max, grapheme) })
|> p.labeled(with: "policy")
- let password_parser = p.labeled(p.any_string_greedy(), with: "password")
+ let password_parser = p.labeled(p.any_str_greedy(), with: "password")
let line_parser =
policy_parser
diff --git a/aoc-2020-gleam/src/days/day04.gleam b/aoc-2020-gleam/src/days/day04.gleam
index 322d5c9..53abb0c 100644
--- a/aoc-2020-gleam/src/days/day04.gleam
+++ b/aoc-2020-gleam/src/days/day04.gleam
@@ -20,25 +20,25 @@ type Passport {
fn parse_passports(from text: String) -> List(Passport) {
let key_parser =
- p.any_string_of_exactly(length: 3)
+ p.any_str_of_len(3)
|> p.labeled(with: "key")
let value_parser =
- p.string1_until_whitespace()
+ p.str1_until_ws()
|> p.labeled(with: "value")
let field_parser =
key_parser
- |> p.then_skip(p.grapheme_literal(":"))
+ |> p.then_skip(p.literal(":"))
|> p.then(value_parser)
|> p.labeled(with: "field")
let passport_parser =
field_parser
- |> p.separated1(by: p.whitespace_grapheme())
+ |> p.sep1(by: p.ws_gc())
|> p.map(with: function.compose(map.from_list, Passport))
|> p.labeled(with: "passport")
let input_parser =
passport_parser
- |> p.separated1(by: p.string_literal("\n\n"))
- |> p.then_skip(p.optional(p.whitespace_grapheme()))
+ |> p.sep1(by: p.literal("\n\n"))
+ |> p.then_skip(p.opt(p.ws_gc()))
|> p.labeled(with: "input")
text
@@ -61,7 +61,7 @@ fn is_valid1(passport: Passport) -> Bool {
fn is_valid2(passport: Passport) -> Bool {
let int_between = fn(min, max) {
p.int()
- |> p.matching(rule: fn(number) { min <= number && number <= max })
+ |> p.satisfying(rule: fn(number) { min <= number && number <= max })
|> p.ignore
}
@@ -73,32 +73,32 @@ fn is_valid2(passport: Passport) -> Bool {
"hgt",
p.or(
int_between(150, 193)
- |> p.then(p.string_literal("cm")),
+ |> p.then(p.literal("cm")),
int_between(59, 76)
- |> p.then(p.string_literal("in")),
+ |> p.then(p.literal("in")),
)
|> p.ignore,
),
#(
"hcl",
- p.then(
- p.grapheme_literal("#"),
- p.grapheme_in(range: "0123456789abcdef")
- |> p.repeated(times: 6),
+ p.literal("#")
+ |> p.then(
+ p.gc_in(range: "0123456789abcdef")
+ |> p.repeat(times: 6),
)
|> p.ignore,
),
#(
"ecl",
eye_colors
- |> list.map(with: p.string_literal)
+ |> list.map(with: p.literal)
|> p.any
|> p.ignore,
),
#(
"pid",
p.digit()
- |> p.string_of_exactly(length: 9)
+ |> p.str_of_len(9)
|> p.ignore,
),
]
diff --git a/aoc-2020-gleam/src/days/day06.gleam b/aoc-2020-gleam/src/days/day06.gleam
index 9308166..6c3481c 100644
--- a/aoc-2020-gleam/src/days/day06.gleam
+++ b/aoc-2020-gleam/src/days/day06.gleam
@@ -22,7 +22,7 @@ fn alphabet() -> Set(String) {
fn parse_input(text: String) -> Input {
let answers_parser =
- p.string1_until_whitespace()
+ p.str1_until_ws()
|> p.map(fn(answer_string) {
answer_string
|> string.to_graphemes
@@ -32,13 +32,13 @@ fn parse_input(text: String) -> Input {
let group_parser =
answers_parser
- |> p.separated1(by: p.whitespace_grapheme())
+ |> p.sep1(by: p.ws_gc())
|> p.labeled(with: "group")
let input_parser =
group_parser
- |> p.separated1(by: p.string_literal("\n\n"))
- |> p.then_skip(p.optional(p.whitespace_grapheme()))
+ |> p.sep1(by: p.literal("\n\n"))
+ |> p.then_skip(p.opt(p.ws_gc()))
|> p.labeled(with: "input")
text