bench-rejected-impls/01_report_repair.rb
# Advent of Code inputs only had 200 elements,
# and iterating those should be faster than 2021 elements...
# But in practice, this one's faster.
# It takes advantage of the fact that we're scanning the numbers in order,
# and therefore we can stop when the first two numbers are too big.
# (Note that stopping is still such that we find all solutions)
# This implies we can improve on the above solutions by sorting.
Advent of Code inputs only had 200 elements,
and iterating those should be faster than 2021 elements
But in practice, this one's faster.
It takes advantage of the fact that we're scanning the numbers in order,
and therefore we can stop when the first two numbers are too big.
(Note that stopping is still such that we find all solutions)
This implies we can improve on the above solutions by sorting.
Advent of Code inputs only had 200 elements and iterating those should be faster than 2021 elements But in practice this one ' s faster . It takes advantage of the fact that we ' re scanning the numbers in order and therefore we can stop when the first two numbers are too big . ( Note that stopping is still such that we find all solutions ) This implies we can improve on the above solutions by sorting .
def find_over_input_range(input, input_tally)
  ans = [p1 = [], p2 = []]
  min, max = input.minmax
  (min..max).each { |a|
    next unless input_tally[a] &.>= 1
    b = 2020 - a
    break if b < a
    required_count = a == b ? 2 : 1
    p1 << a * b if input_tally[b] &.>= required_count
    (a..max).each { |b|
      required_count = a == b ? 2 : 1
      next unless input_tally[b] &.>= required_count
      c = 2020 - a - b
      break if c < b
      required_count = (a == c ? 1 : 0) + (b == c ? 1 : 0) + 1
      p2 << a * b * c if input_tally[c] &.>= required_count
    }
  }
  ans
end
def find_over_input_range ( input , input_tally ) ans = [ p1 = [ ] , p2 = [ ] ] min , max = input . minmax ( min .. max ) . each { | a | next unless input_tally [ a ] &. >= 1 b = 2020 - a break if b < a required_count = a == b ? 2 : 1 p1 << a * b if input_tally [ b ] &. >= required_count ( a .. max ) . each { | b | required_count = a == b ? 2 : 1 next unless input_tally [ b ] &. >= required_count c = 2020 - a - b break if c < b required_count = ( a == c ? 1 : 0 ) + ( b == c ? 1 : 0 ) + 1 p2 << a * b * c if input_tally [ c ] &. >= required_count } } ans end
Advent of Code inputs only had 200 elements,
and iterating those should be faster than 2021 elements...
Advent of Code inputs only had 200 elements and iterating those should be faster than 2021 elements ...
# This break will not miss a part 2 solution. # Same reasoning as in current.
input
<not_specific>
input_tally
<not_specific>