commit cbd3c63584bd7d7bb68db69deabd59368afc2700
parent c3fc2b38859fe28f21c7c8c196340262c39ed935
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Mon, 16 Dec 2019 09:40:18 -0500
Add Julia solution for day 16
Diffstat:
3 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/day-16/Makefile b/day-16/Makefile
@@ -0,0 +1,9 @@
+CC := g++
+CCFLAGS := --std=c++17 -Wall
+
+all: day-16-rust day-16.jl
+ julia day-16.jl
+ ./day-16-rust
+
+day-16-rust: day-16.rs
+ rustc $^ -o $@
diff --git a/day-16/day-16.jl b/day-16/day-16.jl
@@ -0,0 +1,47 @@
+function fft!(signal)
+ pattern = [0, 1, 0, -1]
+ for i in 1:length(signal)
+ curr_signal = 0
+ for j in i:length(signal)
+ curr_signal += pattern[j ÷ i % length(pattern) + 1] * signal[j]
+ end
+ signal[i] = abs(curr_signal) % 10
+ end
+end
+
+function digits_to_num(digits)
+ num = 0
+ for i in 1:length(digits)
+ num = num * 10 + digits[i]
+ end
+ num
+end
+
+function part_1(input)
+ signal = copy(input)
+ for _ in 1:100
+ fft!(signal)
+ end
+ digits_to_num(signal[1:8])
+end
+
+function part_2(input)
+ offset = digits_to_num(input[1:7])
+ # We only care about signal beyond the offset.
+ signal = repeat(input, 10000)[offset:end]
+ # Since this will be in latter half of the signal, the only pattern we see
+ # is i 0s and (n - i) 1s.
+ for _ in 1:100
+ reverse!(signal)
+ cumsum!(signal, signal, dims = 1)
+ reverse!(signal)
+ signal .%= 10
+ end
+ digits_to_num(signal[2:9])
+end
+
+input = map(x -> parse(Int, x), collect(readlines(open("input.txt"))[1]))
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))
diff --git a/day-16/input.txt b/day-16/input.txt
@@ -0,0 +1 @@
+59772698208671263608240764571860866740121164692713197043172876418614411671204569068438371694198033241854293277505547521082227127768000396875825588514931816469636073669086528579846568167984238468847424310692809356588283194938312247006770713872391449523616600709476337381408155057994717671310487116607321731472193054148383351831456193884046899113727301389297433553956552888308567897333657138353770191097676986516493304731239036959591922009371079393026332649558536888902303554797360691183681625604439250088062481052510016157472847289467410561025668637527408406615316940050060474260802000437356279910335624476330375485351373298491579364732029523664108987