17_conway_cubes.rb
# Precomputing this is the part that takes most of the time.
# Speed this up by considering counts of 0..6,
# https://www.reddit.com/r/adventofcode/comments/kfb6zx/day_17_getting_to_t6_at_for_higher_spoilerss/ggsx9e9
Precomputing this is the part that takes most of the time.
Precomputing this is the part that takes most of the time .
def neigh_weights(dimensions, rounds, wzbits)
  if dimensions <= rounds
    ds = [0, -1, 1].repeated_permutation(dimensions - 2).to_a
    ds.shift
  end
  return Hash.new([].freeze).freeze if dimensions <= 2
  weights = Hash.new { |h, k| h[k] = Hash.new(0) }
  prefix = Array.new(dimensions - 2)
  build_if_representative = ->n {
    if n == dimensions - 2
      if dimensions <= rounds
        neigh_weights_by_ds(prefix, ds, rounds, wzbits, weights)
      else
        neigh_weights_by_count(prefix, rounds, wzbits, weights)
      end
    else
      (prefix[n - 1]..rounds).each { |x|
        prefix[n] = x
        build_if_representative[n + 1]
      }
    end
  }
  (0..rounds).each { |x|
    prefix[0] = x
    build_if_representative[1]
  }
  weights.transform_values { |h| h.to_a.map(&:freeze).freeze }.freeze
end
def neigh_weights ( dimensions , rounds , wzbits ) if dimensions <= rounds ds = [ 0 , - 1 , 1 ] . repeated_permutation ( dimensions - 2 ) . to_a ds . shift end return Hash . new ( [ ] . freeze ) . freeze if dimensions <= 2 weights = Hash . new { | h , k | h [ k ] = Hash . new ( 0 ) } prefix = Array . new ( dimensions - 2 ) build_if_representative = -> n { if n == dimensions - 2 if dimensions <= rounds neigh_weights_by_ds ( prefix , ds , rounds , wzbits , weights ) else neigh_weights_by_count ( prefix , rounds , wzbits , weights ) end else ( prefix [ n - 1 ] .. rounds ) . each { | x | prefix [ n ] = x build_if_representative [ n + 1 ] } end } ( 0 .. rounds ) . each { | x | prefix [ 0 ] = x build_if_representative [ 1 ] } weights . transform_values { | h | h . to_a . map ( & :freeze ) . freeze } . freeze end
Precomputing this is the part that takes most of the time.
Precomputing this is the part that takes most of the time .
# rule: May only mutate at index n and above.
dimensions
<not_specific>
rounds
<not_specific>
wzbits
<not_specific>