From fa1ab8cc861718c55b837cbe5acb354f2c71cfcb Mon Sep 17 00:00:00 2001 From: Annwan Date: Fri, 1 Dec 2023 22:23:47 +0100 Subject: [PATCH] Day 1 --- .gitignore | 1 + day1.moon | 34 ++++++++++++++++++++++++++++++++++ utils.moon | 10 ++++++++++ 3 files changed, 45 insertions(+) create mode 100644 .gitignore create mode 100644 day1.moon create mode 100644 utils.moon diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6436383 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +input/ diff --git a/day1.moon b/day1.moon new file mode 100644 index 0000000..37a56ce --- /dev/null +++ b/day1.moon @@ -0,0 +1,34 @@ +import aoc from require"utils" +rex = require "rex_pcre2" + +aoc 1, 1, => + sum = 0 + for k in @gmatch "(%w+)" + digits = {} + for i = 1, #k + if string.match (k\sub i, i), "%d" + digits[#digits + 1] = k\sub i, i + sum += tonumber (digits[1] .. digits[#digits]) + sum + +aoc 1, 2, => + regex = "([0-9]|on(?=e)|tw(?=o)|thre(?=e)|four|fiv(?=e)|six|seve(?=n)|eigh(?=t)|nin(?=e)).*?" + rewrite = { + on: "1", + tw: "2", + thre: "3", + four: "4", + fiv: "5", + six: "6", + seve: "7", + eigh: "8", + nin: "9" + } + sum = 0 + for k in @gmatch "(%w+)" + digits = {} + for d in rex.gmatch k, regex + digits[#digits + 1] = rewrite[d] or d + sum += tonumber (digits[1] .. digits[#digits]) + sum + diff --git a/utils.moon b/utils.moon new file mode 100644 index 0000000..6bf5dba --- /dev/null +++ b/utils.moon @@ -0,0 +1,10 @@ +import p from require "moon" +aoc = (day, problem, fun) -> + with io.open "input/#{day}.txt" + data = \read "*a" + print "============ #{day}.#{problem} ================" + out = fun data + print out + \close! + +{:aoc, :p}