You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

34 lines
822 B

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