diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-02 13:04:41 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-02 13:04:41 +0100 |
commit | 673fb853b227313ee574096591135aee713f4100 (patch) | |
tree | 12e595f4cf73739dcfcfbd137ec89e6ed8f7fcc3 /aoc-2020-gleam/src/days | |
parent | 8240608524987b3df864a34fdae9cfc0352c9477 (diff) | |
download | gleam_aoc2020-673fb853b227313ee574096591135aee713f4100.tar.gz gleam_aoc2020-673fb853b227313ee574096591135aee713f4100.zip |
Add more parsing utilities
Diffstat (limited to 'aoc-2020-gleam/src/days')
-rw-r--r-- | aoc-2020-gleam/src/days/day02.gleam | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam index 6cfb294..ba6d9f1 100644 --- a/aoc-2020-gleam/src/days/day02.gleam +++ b/aoc-2020-gleam/src/days/day02.gleam @@ -1,7 +1,6 @@ import gleam/io import gleam/list import gleam/string -import gleam/pair import gleam/bool import ext/resultx import util/input_util @@ -35,25 +34,22 @@ fn is_line_valid2(line: Line) -> Bool { ) } -fn parse_policy() -> p.Parser(Policy) { - p.int() - |> p.then_skip(p.grapheme_literal("-")) - |> p.then(p.int()) - |> p.then_skip(p.grapheme_literal(" ")) - |> p.then(p.any_grapheme()) - |> p.then_skip(p.grapheme_literal(":")) - |> p.then_skip(p.grapheme_literal(" ")) - |> p.map(with: fn(x) { - let #(#(min, max), grapheme) = x - Policy(min, max, grapheme) - }) -} - fn parse_line(string: String) -> Line { + let policy_parser = + p.int() + |> p.then_skip(p.grapheme_literal("-")) + |> p.then(p.int()) + |> p.then_skip(p.grapheme_literal(" ")) + |> p.then_third(p.any_grapheme()) + |> p.then_skip(p.string_literal(": ")) + |> p.map3(with: fn(min, max, grapheme) { Policy(min, max, grapheme) }) + + let password_parser = p.any_string() + let line_parser = - parse_policy() - |> p.then(p.any_string()) - |> p.map(fn(t) { Line(pair.first(t), pair.second(t)) }) + policy_parser + |> p.then(password_parser) + |> p.map2(fn(policy, password) { Line(policy, password) }) assert Ok(policy) = p.parse_entire(string, with: line_parser) policy |