1
0
Fork 0

Day twenty-two

This commit is contained in:
Andrew Tomaka 2024-12-22 21:06:40 -05:00
parent 6dd5d78331
commit 2167efbb3a
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
2 changed files with 2385 additions and 0 deletions

2332
22/input Normal file

File diff suppressed because it is too large Load diff

53
22/main.rb Executable file
View file

@ -0,0 +1,53 @@
#!/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