|
|
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
aoc 1, "2-alt", (s) -> s = s\gsub "one", "o1e" s = s\gsub "two", "t2o" s = s\gsub "three", "t3ee" s = s\gsub "four", "f4ur" s = s\gsub "five", "f5ve" s = s\gsub "six", "s6x" s = s\gsub "seven", "s7ven" s = s\gsub "eight", "e8ght" s = s\gsub "nine", "n9ne" sum = 0 for k in s\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
|