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.

53 lines
1.4 KiB

1 year ago
1 year ago
1 year ago
1 year ago
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
  32. aoc 1, "2-alt", (s) ->
  33. s = s\gsub "one", "o1e"
  34. s = s\gsub "two", "t2o"
  35. s = s\gsub "three", "t3ee"
  36. s = s\gsub "four", "f4ur"
  37. s = s\gsub "five", "f5ve"
  38. s = s\gsub "six", "s6x"
  39. s = s\gsub "seven", "s7ven"
  40. s = s\gsub "eight", "e8ght"
  41. s = s\gsub "nine", "n9ne"
  42. sum = 0
  43. for k in s\gmatch "(%w+)"
  44. digits = {}
  45. for i = 1, #k
  46. if string.match (k\sub i, i), "%d"
  47. digits[#digits + 1] = k\sub i, i
  48. sum += tonumber (digits[1] .. digits[#digits])
  49. sum