advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 888daaf75fd78afcf924dd87ec556f2a569f8dc3
parent 1a4dc10972eae693fdce4c9464eeea1e52f2b876
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sun,  8 Dec 2019 09:45:21 -0500

Add Julia solution for day 08

Diffstat:
Aday-08/day-08.jl | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+), 0 deletions(-)
diff --git a/day-08/day-08.jl b/day-08/day-08.jl
@@ -0,0 +1,53 @@
+function part_1(input)
+    width = 25
+    height = 6
+    num_layer = length(input) ÷ (width * height)
+    num_0 = zeros(num_layer)
+    num_1 = zeros(num_layer)
+    num_2 = zeros(num_layer)
+    for i in 1:length(input)
+        layer = (i - 1) ÷ (width * height) + 1
+        if input[i] == 0
+            num_0[layer] += 1
+        elseif input[i] == 1
+            num_1[layer] += 1
+        else
+            num_2[layer] += 1
+        end
+    end
+    min_0_layer = argmin(num_0)
+    Int(num_1[min_0_layer] * num_2[min_0_layer])
+end
+
+function part_2(input)
+    width = 25
+    height = 6
+    num_layer = length(input) ÷ (width * height)
+    image = repeat([2], width * height)
+    for i in 1:length(image)
+        # Find first non-transparent cell.
+        layer = 1
+        while image[i] == 2 && layer <= num_layer
+            image[i] = input[(layer - 1) * width * height + i]
+            layer += 1
+        end
+    end
+    for h in 1:height
+        for w in 1:width
+            pixel = image[(h - 1) * width + w]
+            if pixel == 1
+                print("██")
+            else
+                print("  ")
+            end
+        end
+        println()
+    end
+end
+
+input = map(x -> parse(Int32, x), collect(readlines(open("input.txt"))[1]))
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ")
+part_2(input)