2019: Day 16 Part 1
This commit is contained in:
parent
c4d7d6cd48
commit
c8452594c8
1 changed files with 24 additions and 0 deletions
24
2019/day16/part1.rb
Normal file
24
2019/day16/part1.rb
Normal 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
|
||||||
Loading…
Reference in a new issue