|
| 1 | +INITIAL = [3, 7].freeze |
| 2 | + |
| 3 | +two_only = ARGV.delete('-2') |
| 4 | + |
| 5 | +input = !ARGV.empty? && ARGV.first.match?(/^\d+$/) ? ARGV.first : ARGF.read |
| 6 | + |
| 7 | +unless two_only |
| 8 | + first = 0 |
| 9 | + second = 1 |
| 10 | + scores = INITIAL.dup |
| 11 | + target = Integer(input) |
| 12 | + until scores.size >= target + 10 |
| 13 | + scores.concat((scores[first] + scores[second]).digits.reverse) |
| 14 | + first = (first + 1 + scores[first]) % scores.size |
| 15 | + second = (second + 1 + scores[second]) % scores.size |
| 16 | + end |
| 17 | + puts scores[target, 10].join |
| 18 | +end |
| 19 | + |
| 20 | +# This isn't as formally verified as Knuth-Morris-Pratt, |
| 21 | +# but I think it should be fine. |
| 22 | +# I'm assuming the search pattern is small compared to the digit stream, |
| 23 | +# so it's fine if this is not the most efficient. |
| 24 | +def state_transitions(digits) |
| 25 | + next_state = Array.new(digits.size) { [0] * 10 } |
| 26 | + digits.each_with_index { |d, i| |
| 27 | + next_state[i][d] = i + 1 |
| 28 | + (0..9).each { |wrong_digit| |
| 29 | + next if wrong_digit == d |
| 30 | + prefix = digits.first(i) << wrong_digit |
| 31 | + until prefix.empty? |
| 32 | + if digits[0, prefix.size] == prefix |
| 33 | + next_state[i][wrong_digit] = prefix.size |
| 34 | + break |
| 35 | + end |
| 36 | + prefix.shift |
| 37 | + end |
| 38 | + } |
| 39 | + } |
| 40 | + next_state.freeze |
| 41 | +end |
| 42 | + |
| 43 | +# This code does some bad things solely for the purpose of being fast. |
| 44 | +def find(digits) |
| 45 | + first = 0 |
| 46 | + second = 1 |
| 47 | + scores = INITIAL.dup |
| 48 | + |
| 49 | + state_table = state_transitions(digits) |
| 50 | + good_digits = 0 |
| 51 | + |
| 52 | + score1 = scores[first] |
| 53 | + score2 = scores[second] |
| 54 | + |
| 55 | + # while true is faster than loop |
| 56 | + # https://github.com/JuanitoFatas/fast-ruby#loop-vs-while-true-code |
| 57 | + while true |
| 58 | + new_score = score1 + score2 |
| 59 | + |
| 60 | + # Normally, you'd write new_scores = new_score >= 10 ? new_score.divmod(10) : [new_score] |
| 61 | + # and then iterate over new_scores. |
| 62 | + # Instead, here we manually unroll that loop. |
| 63 | + # Unfortunately, the fastest way was code duplication. |
| 64 | + |
| 65 | + if new_score >= 10 |
| 66 | + new_score -= 10 |
| 67 | + good_digits = state_table[good_digits][1] |
| 68 | + return scores.size + 1 - digits.size if good_digits == digits.size |
| 69 | + scores << 1 |
| 70 | + end |
| 71 | + |
| 72 | + good_digits = state_table[good_digits][new_score] |
| 73 | + return scores.size + 1 - digits.size if good_digits == digits.size |
| 74 | + scores << new_score |
| 75 | + |
| 76 | + unless (score1 = scores[first += 1 + score1]) |
| 77 | + first %= scores.size |
| 78 | + score1 = scores[first] |
| 79 | + end |
| 80 | + unless (score2 = scores[second += 1 + score2]) |
| 81 | + second %= scores.size |
| 82 | + score2 = scores[second] |
| 83 | + end |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +{ |
| 88 | + [5, 1, 5, 8, 9] => 9, |
| 89 | + [0, 1, 2, 4, 5] => 5, |
| 90 | + [9, 2, 5, 1, 0] => 18, |
| 91 | + [5, 9, 4, 1, 4] => 2018, |
| 92 | +}.each { |k, want| |
| 93 | + got = find(k) |
| 94 | + puts "#{k.join}: want #{want}, got #{got}" if want != got |
| 95 | +} |
| 96 | + |
| 97 | +puts find(input.chars.map(&method(:Integer))) |
0 commit comments