1
0
Fork 0
advent-of-code-2024/22/main.rb

54 lines
923 B
Ruby
Raw Permalink Normal View History

2024-12-22 21:06:40 -05:00
#!/usr/bin/env ruby
require "debug"
input = (ARGV.first.nil? ? DATA : ARGF)
.readlines(chomp: true)
.map(&:to_i)
def evolve(secret)
a = ((secret * 64) ^ secret) % 16777216
b = ((a / 32) ^ a) % 16777216
c = ((b * 2048) ^ b) % 16777216
end
part_1 = input.map do |secret|
original = secret
2000.times { secret = evolve(secret) }
secret
end.sum
p part_1
sequences = input.map do |secret|
secrets = 2000.times
.map { secret = evolve(secret) }
prices = secrets.map { _1.digits.first }
changes = prices
.map.with_index { |p, i| i == 0 ? 0 : p - prices[i - 1] }
sequences = Hash.new
changes[1..].each_cons(4).with_index.each do |sequence, i|
sequences[sequence] ||= prices[i + 4]
end
sequences
end
unique_sequences = sequences.flat_map(&:keys).uniq
part_2 = unique_sequences.map do |sequence|
sequences.sum { _1.fetch(sequence, 0) }
end.max
p part_2
__END__
1
10
100
2024