diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-02 14:46:32 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-02-02 14:46:32 +0100 |
commit | c74bb3862190e866a8764bf0781f60b248b64876 (patch) | |
tree | d746453322d6234a0234c44ebc6f70e5f27af916 /aoc-2020-gleam | |
parent | a5b3eff4b14e3d84a9f5f5f73dc6df4be34cd27d (diff) | |
download | gleam_aoc2020-c74bb3862190e866a8764bf0781f60b248b64876.tar.gz gleam_aoc2020-c74bb3862190e866a8764bf0781f60b248b64876.zip |
Refactor day 2
Diffstat (limited to 'aoc-2020-gleam')
-rw-r--r-- | aoc-2020-gleam/src/days/day02.gleam | 56 | ||||
-rw-r--r-- | aoc-2020-gleam/src/ext/listx.gleam | 7 |
2 files changed, 36 insertions, 27 deletions
diff --git a/aoc-2020-gleam/src/days/day02.gleam b/aoc-2020-gleam/src/days/day02.gleam index c4dd751..bc5f7bd 100644 --- a/aoc-2020-gleam/src/days/day02.gleam +++ b/aoc-2020-gleam/src/days/day02.gleam @@ -3,6 +3,7 @@ import gleam/list import gleam/string import gleam/bool import ext/resultx +import ext/listx import util/input_util import util/parser as p @@ -14,26 +15,6 @@ type Line { Line(policy: Policy, password: String) } -fn is_line_valid1(line: Line) -> Bool { - line.password - |> string.to_graphemes - |> list.filter(for: fn(g) { g == line.policy.grapheme }) - |> list.length - |> fn(l) { line.policy.min <= l && l <= line.policy.max } -} - -fn is_line_valid2(line: Line) -> Bool { - 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 - } - bool.exclusive_or( - grapheme_matches(line.policy.min), - grapheme_matches(line.policy.max), - ) -} - fn parse_line(string: String) -> Line { let policy_parser = p.int() @@ -57,18 +38,39 @@ fn parse_line(string: String) -> Line { policy } -fn part1(lines: List(String)) -> Int { +fn solve(lines: List(String), predicate: fn(Line) -> Bool) -> Int { lines |> list.map(with: parse_line) - |> list.filter(for: is_line_valid1) - |> list.length + |> listx.count(satisfying: predicate) +} + +fn part1(lines: List(String)) -> Int { + solve( + lines, + fn(line) { + line.password + |> string.to_graphemes + |> listx.count(satisfying: fn(g) { g == line.policy.grapheme }) + |> fn(l) { line.policy.min <= l && l <= line.policy.max } + }, + ) } fn part2(lines: List(String)) -> Int { - lines - |> list.map(with: parse_line) - |> list.filter(for: is_line_valid2) - |> list.length + 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 + } + bool.exclusive_or( + grapheme_matches(line.policy.min), + grapheme_matches(line.policy.max), + ) + }, + ) } pub fn run() -> Nil { diff --git a/aoc-2020-gleam/src/ext/listx.gleam b/aoc-2020-gleam/src/ext/listx.gleam new file mode 100644 index 0000000..d962515 --- /dev/null +++ b/aoc-2020-gleam/src/ext/listx.gleam @@ -0,0 +1,7 @@ +import gleam/list + +pub fn count(list: List(a), satisfying predicate: fn(a) -> Bool) -> Int { + list + |> list.filter(for: predicate) + |> list.length +} |