aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2023-12-02 00:10:57 -0500
committerHJ <thechairman@thechairman.info>2023-12-02 00:10:57 -0500
commite06c1b7ee377fbb09066bc05472757759a3b6435 (patch)
tree4c7cee6535b7c23f5c8191ac37567b2ea614f975 /aoc2023/src
parenta82877a093f43c11b298f2a59374b53595b40f53 (diff)
parent826d7698c84b42dadf6b1684c93828e3e2f5ee3a (diff)
downloadgleam_aoc-e06c1b7ee377fbb09066bc05472757759a3b6435.tar.gz
gleam_aoc-e06c1b7ee377fbb09066bc05472757759a3b6435.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023/src')
-rw-r--r--aoc2023/src/day1/solve.gleam30
1 files changed, 12 insertions, 18 deletions
diff --git a/aoc2023/src/day1/solve.gleam b/aoc2023/src/day1/solve.gleam
index fbc160f..ed14bde 100644
--- a/aoc2023/src/day1/solve.gleam
+++ b/aoc2023/src/day1/solve.gleam
@@ -3,29 +3,24 @@ import gleam/io
import gleam/list
import gleam/string
import gleam/regex.{type Match, Match}
-import gleam/result
import gleam/int
-fn parse_digits(input: String) {
- let assert Ok(re) = regex.from_string("[0-9]")
+pub fn part1(input: String) {
+ let assert Ok(re) = regex.from_string("[1-9]")
input
|> string.split("\n")
- |> list.map(fn(s) {
- let matches = regex.scan(s, with: re)
-
- let assert Ok(Match(content: first, ..)) = list.first(matches)
- let assert Ok(Match(content: last, ..)) = list.last(matches)
-
- int.parse(first <> last)
- })
-}
+ |> list.fold(
+ 0,
+ fn(acc, s) {
+ let matches = regex.scan(s, with: re)
-pub fn part1(input: String) {
- input
- |> parse_digits
- |> result.values
- |> int.sum
+ let assert Ok(Match(content: first, ..)) = list.first(matches)
+ let assert Ok(Match(content: last, ..)) = list.last(matches)
+ let assert Ok(i) = int.parse(first <> last)
+ acc + i
+ },
+ )
|> string.inspect
}
@@ -39,7 +34,6 @@ const substitutions = [
#("seven", "7n"),
#("eight", "e8t"),
#("nine", "n9e"),
- #("zero", "0o"),
]
pub fn part2(input: String) {