123456789101112131415161718192021222324252627282930313233343536 |
- use "collections"
- use "format"
- use "itertools"
- primitive Password
- fun is_valid_pt1(pass: U64): Bool =>
- let digits: String box = Format.int[U64](pass)
- var pairwise = Array[(U8, U8)]()
- Iter[U8](digits.values()).zip[U8](Iter[U8](digits.values()).skip(1)).collect(pairwise)
- let two_adjacent = Iter[(U8, U8)](pairwise.values()).any({(tup) => (let l, let r) = tup; l == r})
- let increasing = Iter[(U8, U8)](pairwise.values()).all({(tup) => (let l, let r) = tup; l <= r})
- (digits.size() == 6) and two_adjacent and increasing
- fun is_valid_pt2(pass: U64, env: Env): Bool =>
- let digits: String box = Format.int[U64](pass)
- let increasing = Iter[U8](digits.values()).zip[U8](Iter[U8](digits.values()).skip(1)).all({(tup) => (let l, let r) = tup; l <= r})
- var last: U8 = 0
- var this_run: U8 = 0
- var runs = Array[U8]()
- for d in digits.values() do
- if d == last then
- this_run = this_run + 1
- else
- runs.push(this_run)
- this_run = 0
- end
- last = d
- end
- runs.push(this_run)
- let only_one_double = Iter[U8](runs.values()).filter({(n) => n == 1}).count() >= 1
- (digits.size() == 6) and increasing and only_one_double
- actor Main
- new create(env: Env) =>
- let valids = Iter[U64](Range[U64](240298, 784956)).filter({(n) => Password.is_valid_pt2(n, env)})
- env.out.print("Valid passwords: " + Format.int[USize](valids.count()))
|