aboutsummaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
Diffstat (limited to '2023')
-rw-r--r--2023/nim/day01.nim38
1 files changed, 38 insertions, 0 deletions
diff --git a/2023/nim/day01.nim b/2023/nim/day01.nim
new file mode 100644
index 0000000..2dd1f70
--- /dev/null
+++ b/2023/nim/day01.nim
@@ -0,0 +1,38 @@
+# Day One: Trebuchet?!
+import std/[os, strutils, sequtils, sugar]
+
+let input = paramStr(1).readFile().strip().split("\n")
+
+var values: seq[int]
+for line in input:
+ var buf = ""
+ for c in line:
+ if c in '0'..'9':
+ buf.add(c)
+ buf = if buf.len == 1: buf & buf else: buf[0] & buf[^1]
+ values.add(buf.parseInt())
+echo values.foldl(a + b, 0)
+
+var parsed = input
+for _ in 0 ..< 100: # :-(
+ parsed = parsed.map(x => x.multiReplace([
+ ("one", "o1e"), # one1one does NOT work
+ ("two", "t2o"),
+ ("three", "t3e"),
+ ("four", "f4fr"),
+ ("five", "f5e"),
+ ("six", "s6x"),
+ ("seven", "s7n"),
+ ("eight", "e8t"),
+ ("nine", "n9e"),
+ ]))
+
+values = @[]
+for line in parsed:
+ var buf = ""
+ for c in line:
+ if c in '0'..'9':
+ buf.add(c)
+ buf = if buf.len == 1: buf & buf else: buf[0] & buf[^1]
+ values.add(buf.parseInt())
+echo values.foldl(a + b, 0)