diff options
author | JJ | 2023-12-01 07:31:34 +0000 |
---|---|---|
committer | JJ | 2023-12-01 07:31:34 +0000 |
commit | 0e8d6bbf3e55e7506e1e92b5292833b1f3907d06 (patch) | |
tree | aa48059a011d58a1efdd46f5b41427851ba7218d /2023/nim | |
parent | dad35e2ee87ede7f19490b5abaffbf3aaf2f12e6 (diff) |
Diffstat (limited to '2023/nim')
-rw-r--r-- | 2023/nim/day01.nim | 38 |
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) |