Skip to content

Instantly share code, notes, and snippets.

@cjavdev
Created December 20, 2022 15:18
Show Gist options
  • Save cjavdev/acdcf1d9dad509928ddd0305e8d878f7 to your computer and use it in GitHub Desktop.
Save cjavdev/acdcf1d9dad509928ddd0305e8d878f7 to your computer and use it in GitHub Desktop.
if ARGV.empty?
data = [1, 2, -3, 3, -2, 0, 4]
else
data = File
.readlines(ARGV[0], chomp: true)
.map(&:to_i)
end
def mix(data)
# iterate over all the elements and
# shuffle them to the right positions
data.length.times do |i|
data.rotate! until data.first.last == i
head, *tail = data
data = [
head,
*tail.rotate(head.first)
].rotate(-head.first)
end
data.rotate! until data.first.last == 0
data
end
data = data.each_with_index.to_a
# part 1
# numbers = mix(data).map(&:first)
# zero = numbers.find_index(0)
# numbers = numbers.cycle.take(zero + 3001)
# p numbers[zero + 1000] + numbers[zero + 2000] +
# numbers[zero + 3000]
# part 2
data = data.map {|n, i| [n * 811589153, i] }
10.times do |i|
puts "Round #{i}"
data = mix(data)
end
numbers = data.map(&:first)
zero = numbers.find_index(0)
numbers = numbers.cycle.take(zero + 3001)
p numbers[zero + 1000] + numbers[zero + 2000] +
numbers[zero + 3000]
@cjavdev
Copy link
Author

cjavdev commented Dec 20, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment