diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-22 14:01:12 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-22 14:01:12 +0100 |
commit | efde7d6c018e360efbf590a67d128de41e6da7af (patch) | |
tree | 1ae6d7d3eba15c48b1219e4e9e1d956809f76064 /aoc-2020-gleam/src | |
parent | 5746dbca0ddaef455b6f26fba3945f3533d0b2c1 (diff) | |
download | gleam_aoc2020-efde7d6c018e360efbf590a67d128de41e6da7af.tar.gz gleam_aoc2020-efde7d6c018e360efbf590a67d128de41e6da7af.zip |
Refactor previous days
Diffstat (limited to 'aoc-2020-gleam/src')
-rw-r--r-- | aoc-2020-gleam/src/days/day01.gleam | 9 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day02.gleam | 14 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day04.gleam | 6 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day06.gleam | 2 | ||||
-rw-r--r-- | aoc-2020-gleam/src/days/day07.gleam | 10 | ||||
-rw-r--r-- | aoc-2020-gleam/src/util/parser.gleam | 11 |
6 files changed, 28 insertions, 24 deletions
diff --git a/aoc-2020-gleam/src/days/day01.gleam b/aoc-2020-gleam/src/days/day01.gleam index 0d2a994..9e332d2 100644 --- a/aoc-2020-gleam/src/days/day01.gleam +++ b/aoc-2020-gleam/src/days/day01.gleam @@ -1,24 +1,23 @@ import gleam/io import gleam/int import gleam/list -import gleam/result as res import ext/resultx as resx import util/input_util -fn solve(numbers: List(Int), n: Int) -> Int { +fn solve(numbers: List(Int), n n: Int) -> Int { numbers |> list.combinations(by: n) |> list.find(one_that: fn(p) { int.sum(p) == 2020 }) - |> res.map(with: int.product) |> resx.assert_unwrap + |> int.product } fn part1(numbers: List(Int)) -> Int { - solve(numbers, 2) + solve(numbers, n: 2) } fn part2(numbers: List(Int)) -> Int { - solve(numbers, 3) + solve(numbers, n: 3) } pub fn run() -> Nil { diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam index 6d2e743..3099d23 100644 --- a/aoc-2020-gleam/src/days/day02.gleam +++ b/aoc-2020-gleam/src/days/day02.gleam @@ -18,12 +18,12 @@ type Line { fn parse_line(string: String) -> Line { let policy_parser = p.int() - |> p.then_skip(p.literal("-")) + |> p.skip(p.literal("-")) |> p.then(p.int()) - |> p.then_skip(p.literal(" ")) + |> p.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.skip(p.literal(": ")) + |> p.map3(with: Policy) |> p.labeled(with: "policy") let password_parser = p.labeled(p.any_str_greedy(), with: "password") @@ -31,7 +31,7 @@ fn parse_line(string: String) -> Line { let line_parser = policy_parser |> p.then(password_parser) - |> p.map2(fn(policy, password) { Line(policy, password) }) + |> p.map2(with: Line) |> p.labeled(with: "line") assert Ok(policy) = p.parse_entire(string, with: line_parser) @@ -60,10 +60,10 @@ fn part2(lines: List(String)) -> Int { solve( lines, fn(line) { - let grapheme_matches = fn(idx) { + let grapheme_matches = fn(index) { line.password |> str.to_graphemes - |> list.at(idx - 1) + |> list.at(index - 1) |> resx.assert_unwrap == line.policy.grapheme } bool.exclusive_or( diff --git a/aoc-2020-gleam/src/days/day04.gleam b/aoc-2020-gleam/src/days/day04.gleam index 6087e12..51722ed 100644 --- a/aoc-2020-gleam/src/days/day04.gleam +++ b/aoc-2020-gleam/src/days/day04.gleam @@ -27,7 +27,7 @@ fn parse_passports(from text: String) -> List(Passport) { |> p.labeled(with: "value") let field_parser = key_parser - |> p.then_skip(p.literal(":")) + |> p.skip(p.literal(":")) |> p.then(value_parser) |> p.labeled(with: "field") let passport_parser = @@ -38,7 +38,7 @@ fn parse_passports(from text: String) -> List(Passport) { let input_parser = passport_parser |> p.sep1(by: p.literal("\n\n")) - |> p.then_skip(p.opt(p.ws_gc())) + |> p.skip_ws |> 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.satisfying(rule: fn(number) { min <= number && number <= max }) + |> p.satisfying(rule: fn(num) { min <= num && num <= max }) |> p.ignore } diff --git a/aoc-2020-gleam/src/days/day06.gleam b/aoc-2020-gleam/src/days/day06.gleam index d655d0e..824f984 100644 --- a/aoc-2020-gleam/src/days/day06.gleam +++ b/aoc-2020-gleam/src/days/day06.gleam @@ -38,7 +38,7 @@ fn parse_input(text: String) -> Input { let input_parser = group_parser |> p.sep1(by: p.literal("\n\n")) - |> p.then_skip(p.opt(p.ws_gc())) + |> p.skip_ws |> p.labeled(with: "input") text diff --git a/aoc-2020-gleam/src/days/day07.gleam b/aoc-2020-gleam/src/days/day07.gleam index b99b36f..8569956 100644 --- a/aoc-2020-gleam/src/days/day07.gleam +++ b/aoc-2020-gleam/src/days/day07.gleam @@ -33,19 +33,19 @@ fn parse_graph(lines: List(String)) -> BagGraph { let line_parser = bag_type_parser - |> p.then_skip(p.literal(" bags contain ")) + |> p.skip(p.literal(" bags contain ")) |> p.then(p.or( p.int() - |> p.then_skip(p.ws_gc()) + |> p.skip_ws |> p.then(bag_type_parser) |> p.map(with: pair.swap) - |> p.then_skip(p.ws_gc()) - |> p.then_skip(p.then(p.literal("bag"), p.opt(p.literal("s")))) + |> 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") |> p.map(fun.constant([])), )) - |> p.then_skip(p.literal(".")) + |> p.skip(p.literal(".")) lines |> list.map(with: fun.compose( diff --git a/aoc-2020-gleam/src/util/parser.gleam b/aoc-2020-gleam/src/util/parser.gleam index 9220679..d5fa429 100644 --- a/aoc-2020-gleam/src/util/parser.gleam +++ b/aoc-2020-gleam/src/util/parser.gleam @@ -135,6 +135,11 @@ pub fn str1_until_ws() -> Parser(String) { str_of_many1(of: non_ws_gc()) } +pub fn skip_ws(after parser: Parser(a)) -> Parser(a) { + parser + |> skip(ws0()) +} + pub fn ignore(parser: Parser(a)) -> Parser(Nil) { parser |> map(fun.constant(Nil)) @@ -151,13 +156,13 @@ pub fn then(first: Parser(a), second: Parser(b)) -> Parser(#(a, b)) { |> labeled(with: first.label <> " |> then(" <> second.label <> ")") } -pub fn then_skip(first: Parser(a), second: Parser(b)) -> Parser(a) { +pub fn skip(first: Parser(a), second: Parser(b)) -> Parser(a) { first |> then(second) |> map(with: pair.first) } -pub fn drop_then(first: Parser(a), second: Parser(b)) -> Parser(b) { +pub fn proceed(first: Parser(a), second: Parser(b)) -> Parser(b) { first |> then(second) |> map(with: pair.second) @@ -316,7 +321,7 @@ pub fn str_of_many1(of parser: Parser(String)) -> Parser(String) { pub fn sep1(parser: Parser(a), by separator: Parser(b)) -> Parser(List(a)) { parser - |> then(many0(of: drop_then(separator, parser))) + |> then(many0(of: proceed(separator, parser))) |> map2(with: fn(p, ps) { [p, ..ps] }) |> labeled( with: "sep1(" <> parser.label <> ", by: " <> separator.label <> ")", |