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

1 year ago
  1. import aoc from require"utils"
  2. rex = require "rex_pcre2"
  3. aoc 1, 1, =>
  4. sum = 0
  5. for k in @gmatch "(%w+)"
  6. digits = {}
  7. for i = 1, #k
  8. if string.match (k\sub i, i), "%d"
  9. digits[#digits + 1] = k\sub i, i
  10. sum += tonumber (digits[1] .. digits[#digits])
  11. sum
  12. aoc 1, 2, =>
  13. regex = "([0-9]|on(?=e)|tw(?=o)|thre(?=e)|four|fiv(?=e)|six|seve(?=n)|eigh(?=t)|nin(?=e)).*?"
  14. rewrite = {
  15. on: "1",
  16. tw: "2",
  17. thre: "3",
  18. four: "4",
  19. fiv: "5",
  20. six: "6",
  21. seve: "7",
  22. eigh: "8",
  23. nin: "9"
  24. }
  25. sum = 0
  26. for k in @gmatch "(%w+)"
  27. digits = {}
  28. for d in rex.gmatch k, regex
  29. digits[#digits + 1] = rewrite[d] or d
  30. sum += tonumber (digits[1] .. digits[#digits])
  31. sum