Watch
1
0
Fork
You've already forked advent-of-code
0

2019: Day 16 Part 1

This commit is contained in:
Jeremy Kaplan 2019-12-15 21:52:41 -08:00
commit c8452594c8

24
2019/day16/part1.rb Normal file
View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
def fft(input)
base = [0, 1, 0, -1]
input.each_with_index.map do |_n, i|
pattern = base.flat_map { |e| [e] * (i + 1) }
pattern *= (input.length.fdiv pattern.length).ceil + 1
pattern.shift
cross(input, pattern[0...input.length])
end
end
def cross(input, pattern)
total = input.zip(pattern).sum { |a, b| a * b }
total.abs % 10
end
input = File.read(File.join(__dir__, 'input'))
numbers = input.strip.split('').map(&:to_i)
100.times do |_i|
numbers = fft(numbers)
end
puts numbers[0, 8].join