From 0e8d6bbf3e55e7506e1e92b5292833b1f3907d06 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 30 Nov 2023 23:31:34 -0800 Subject: update scripts etc; we're adventing --- 2023/nim/day01.nim | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2023/nim/day01.nim (limited to '2023/nim') 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) -- cgit v1.2.3-70-g09d2