aboutsummaryrefslogtreecommitdiff
path: root/aoc2023-gleam/src/day1/solve.gleam
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2024-02-02 17:05:12 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2024-02-02 17:05:12 -0500
commit48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9 (patch)
treef59a13e0b5e80ab925220b4488c6e36b1bec660a /aoc2023-gleam/src/day1/solve.gleam
parent87e9ab25ff70e215b537939a4bc23ab101f41dbe (diff)
downloadgleam_aoc-48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9.tar.gz
gleam_aoc-48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9.zip
renaming
Diffstat (limited to 'aoc2023-gleam/src/day1/solve.gleam')
-rw-r--r--aoc2023-gleam/src/day1/solve.gleam64
1 files changed, 64 insertions, 0 deletions
diff --git a/aoc2023-gleam/src/day1/solve.gleam b/aoc2023-gleam/src/day1/solve.gleam
new file mode 100644
index 0000000..ed14bde
--- /dev/null
+++ b/aoc2023-gleam/src/day1/solve.gleam
@@ -0,0 +1,64 @@
+import adglent.{First, Second}
+import gleam/io
+import gleam/list
+import gleam/string
+import gleam/regex.{type Match, Match}
+import gleam/int
+
+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
+ },
+ )
+ |> string.inspect
+}
+
+const substitutions = [
+ #("one", "o1e"),
+ #("two", "t2o"),
+ #("three", "t3e"),
+ #("four", "4"),
+ #("five", "5e"),
+ #("six", "6"),
+ #("seven", "7n"),
+ #("eight", "e8t"),
+ #("nine", "n9e"),
+]
+
+pub fn part2(input: String) {
+ list.fold(
+ over: substitutions,
+ from: input,
+ with: fn(acc, sub) {
+ let #(from, to) = sub
+ string.replace(in: acc, each: from, with: to)
+ },
+ )
+ |> part1
+}
+
+pub fn main() {
+ let assert Ok(part) = adglent.get_part()
+ let assert Ok(input) = adglent.get_input("1")
+ case part {
+ First ->
+ part1(input)
+ |> adglent.inspect
+ |> io.println
+ Second ->
+ part2(input)
+ |> adglent.inspect
+ |> io.println
+ }
+}