Annwan 1 year ago
commit
fa1ab8cc86
  1. 1
      .gitignore
  2. 34
      day1.moon
  3. 10
      utils.moon

1
.gitignore

@ -0,0 +1 @@
input/

34
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

10
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}
Loading…
Cancel
Save