day-16.jl (1151B)
1 function fft!(signal) 2 pattern = [0, 1, 0, -1] 3 for i in 1:length(signal) 4 curr_signal = 0 5 for j in i:length(signal) 6 curr_signal += pattern[j ÷ i % length(pattern) + 1] * signal[j] 7 end 8 signal[i] = abs(curr_signal) % 10 9 end 10 end 11 12 function digits_to_num(digits) 13 num = 0 14 for i in 1:length(digits) 15 num = num * 10 + digits[i] 16 end 17 num 18 end 19 20 function part_1(input) 21 signal = copy(input) 22 for _ in 1:100 23 fft!(signal) 24 end 25 digits_to_num(signal[1:8]) 26 end 27 28 function part_2(input) 29 offset = digits_to_num(input[1:7]) 30 # We only care about signal beyond the offset. 31 signal = repeat(input, 10000)[offset:end] 32 # Since this will be in latter half of the signal, the only pattern we see 33 # is i 0s and (n - i) 1s. 34 for _ in 1:100 35 reverse!(signal) 36 cumsum!(signal, signal, dims = 1) 37 reverse!(signal) 38 signal .%= 10 39 end 40 digits_to_num(signal[2:9]) 41 end 42 43 input = map(x -> parse(Int, x), collect(readlines(open("input.txt"))[1])) 44 45 println("Julia:") 46 println("Part 1: ", part_1(input)) 47 println("Part 2: ", part_2(input))