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.

main.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "github.com/csmith/aoc-2019/common"
  4. "strings"
  5. )
  6. func matchesRules(input int) (hasAnyRun bool, hasTwoRun bool, next int) {
  7. last := 10
  8. run := 0
  9. for input > 0 {
  10. digit := input % 10
  11. if digit > last {
  12. // Simple optimisation: if we hit a digit that's larger than the previous (running right-to-left) then
  13. // we can predict the next possible number that might match. e.g. 1234111 => 1234444.
  14. for input < 100000 {
  15. input = 10*input + digit
  16. }
  17. return false, false, input
  18. } else if digit == last {
  19. run++
  20. } else {
  21. hasTwoRun = hasTwoRun || run == 1
  22. hasAnyRun = hasAnyRun || run > 0
  23. last = digit
  24. run = 0
  25. }
  26. input = input / 10
  27. }
  28. hasTwoRun = hasTwoRun || run == 1
  29. hasAnyRun = hasAnyRun || run > 0
  30. return
  31. }
  32. func main() {
  33. var (
  34. input = strings.Split(common.ReadFileAsStrings("04/input.txt")[0], "-")
  35. from = common.MustAtoi(input[0])
  36. to = common.MustAtoi(input[1])
  37. part1 = 0
  38. part2 = 0
  39. )
  40. for i := from; i <= to; i++ {
  41. p1, p2, n := matchesRules(i)
  42. if p1 {
  43. part1++
  44. }
  45. if p2 {
  46. part2++
  47. }
  48. if n > 0 {
  49. i = n - 1
  50. }
  51. }
  52. println(part1)
  53. println(part2)
  54. }