advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

day-08.jl (1339B)

    1 function part_1(input)
    2     width = 25
    3     height = 6
    4     num_layer = length(input) ÷ (width * height)
    5     num_0 = zeros(num_layer)
    6     num_1 = zeros(num_layer)
    7     num_2 = zeros(num_layer)
    8     for i in 1:length(input)
    9         layer = (i - 1) ÷ (width * height) + 1
   10         if input[i] == 0
   11             num_0[layer] += 1
   12         elseif input[i] == 1
   13             num_1[layer] += 1
   14         else
   15             num_2[layer] += 1
   16         end
   17     end
   18     min_0_layer = argmin(num_0)
   19     Int(num_1[min_0_layer] * num_2[min_0_layer])
   20 end
   21 
   22 function part_2(input)
   23     width = 25
   24     height = 6
   25     num_layer = length(input) ÷ (width * height)
   26     image = repeat([2], width * height)
   27     for i in 1:length(image)
   28         # Find first non-transparent cell.
   29         layer = 1
   30         while image[i] == 2 && layer <= num_layer
   31             image[i] = input[(layer - 1) * width * height + i]
   32             layer += 1
   33         end
   34     end
   35     for h in 1:height
   36         for w in 1:width
   37             pixel = image[(h - 1) * width + w]
   38             if pixel == 1
   39                 print("██")
   40             else
   41                 print("  ")
   42             end
   43         end
   44         println()
   45     end
   46 end
   47 
   48 input = map(x -> parse(Int32, x), collect(readlines(open("input.txt"))[1]))
   49 
   50 println("Julia:")
   51 println("Part 1: ", part_1(input))
   52 println("Part 2: ")
   53 part_2(input)