bench-rejected-impls/01_report_repair.rb
# Use the fact that the numbers tend to be large (otherwise there'd be many solutions),
# sort them and end loops at earliest opportunity.
# (Note that stopping is still such that we find all solutions)
# Theoretically not an asymptotic improvement:
# askalski thinks the worst input is 1-673, 675, 2019,
# for which the early `break` have almost no effect.
# However, given the sorts of inputs we actually see, a pretty good improvement.
Use the fact that the numbers tend to be large (otherwise there'd be many solutions),
sort them and end loops at earliest opportunity.
(Note that stopping is still such that we find all solutions)
Theoretically not an asymptotic improvement:
askalski thinks the worst input is 1-673, 675, 2019,
for which the early `break` have almost no effect.
However, given the sorts of inputs we actually see, a pretty good improvement.
Use the fact that the numbers tend to be large ( otherwise there ' d be many solutions ) sort them and end loops at earliest opportunity . ( Note that stopping is still such that we find all solutions ) Theoretically not an asymptotic improvement : askalski thinks the worst input is 1 - 673 675 2019 for which the early ` break ` have almost no effect . However given the sorts of inputs we actually see a pretty good improvement .
def current(input, input_tally)
  sorted = input.sort
  ans = [p1 = [], p2 = []]
  sorted.each_with_index { |x, i|
    needed = 2020 - x
    break if needed < x
    required_count = x == needed ? 2 : 1
    if input_tally[needed] &.>= required_count
      p [x, needed] if VERBOSE
      p1 << x * needed
    end
    ((i + 1)...sorted.size).each { |j|
      y = sorted[j]
      needed = 2020 - x - y
      break if needed < y
      required_count = (x == needed ? 1 : 0) + (y == needed ? 1 : 0) + 1
      if input_tally[needed] &.>= required_count
        p [x, y, needed] if VERBOSE
        p2 << x * y * needed
      end
    }
  }
  ans
end
def current ( input , input_tally ) sorted = input . sort ans = [ p1 = [ ] , p2 = [ ] ] sorted . each_with_index { | x , i | needed = 2020 - x break if needed < x required_count = x == needed ? 2 : 1 if input_tally [ needed ] &. >= required_count p [ x , needed ] if VERBOSE p1 << x * needed end ( ( i + 1 ) ... sorted . size ) . each { | j | y = sorted [ j ] needed = 2020 - x - y break if needed < y required_count = ( x == needed ? 1 : 0 ) + ( y == needed ? 1 : 0 ) + 1 if input_tally [ needed ] &. >= required_count p [ x , y , needed ] if VERBOSE p2 << x * y * needed end } } ans end
Use the fact that the numbers tend to be large (otherwise there'd be many solutions),
sort them and end loops at earliest opportunity.
Use the fact that the numbers tend to be large ( otherwise there ' d be many solutions ) sort them and end loops at earliest opportunity .
# This break will not miss a part 2 solution. # Same reasoning as in find_over_input_range.
input
<not_specific>
input_tally
<not_specific>