main.pony 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. use "collections"
  2. use "format"
  3. use "itertools"
  4. primitive Password
  5. fun is_valid_pt1(pass: U64): Bool =>
  6. let digits: String box = Format.int[U64](pass)
  7. var pairwise = Array[(U8, U8)]()
  8. Iter[U8](digits.values()).zip[U8](Iter[U8](digits.values()).skip(1)).collect(pairwise)
  9. let two_adjacent = Iter[(U8, U8)](pairwise.values()).any({(tup) => (let l, let r) = tup; l == r})
  10. let increasing = Iter[(U8, U8)](pairwise.values()).all({(tup) => (let l, let r) = tup; l <= r})
  11. (digits.size() == 6) and two_adjacent and increasing
  12. fun is_valid_pt2(pass: U64, env: Env): Bool =>
  13. let digits: String box = Format.int[U64](pass)
  14. let increasing = Iter[U8](digits.values()).zip[U8](Iter[U8](digits.values()).skip(1)).all({(tup) => (let l, let r) = tup; l <= r})
  15. var last: U8 = 0
  16. var this_run: U8 = 0
  17. var runs = Array[U8]()
  18. for d in digits.values() do
  19. if d == last then
  20. this_run = this_run + 1
  21. else
  22. runs.push(this_run)
  23. this_run = 0
  24. end
  25. last = d
  26. end
  27. runs.push(this_run)
  28. let only_one_double = Iter[U8](runs.values()).filter({(n) => n == 1}).count() >= 1
  29. (digits.size() == 6) and increasing and only_one_double
  30. actor Main
  31. new create(env: Env) =>
  32. let valids = Iter[U64](Range[U64](240298, 784956)).filter({(n) => Password.is_valid_pt2(n, env)})
  33. env.out.print("Valid passwords: " + Format.int[USize](valids.count()))