From 826d7698c84b42dadf6b1684c93828e3e2f5ee3a Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 18:00:48 -0500 Subject: day 1 improvements --- aoc2023/src/day1/solve.gleam | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'aoc2023') diff --git a/aoc2023/src/day1/solve.gleam b/aoc2023/src/day1/solve.gleam index 29322ce..ed14bde 100644 --- a/aoc2023/src/day1/solve.gleam +++ b/aoc2023/src/day1/solve.gleam @@ -5,24 +5,22 @@ import gleam/string import gleam/regex.{type Match, Match} import gleam/int -fn parse_digits(input: String) { +pub fn part1(input: String) { let assert Ok(re) = regex.from_string("[1-9]") input |> string.split("\n") - |> list.fold(0, fn(acc, 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) - let assert Ok(i) = int.parse(first <> last) - acc + i - }) -} + |> list.fold( + 0, + fn(acc, s) { + let matches = regex.scan(s, with: re) -pub fn part1(input: String) { - input - |> parse_digits + 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 } -- cgit v1.2.3