-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.jl
50 lines (38 loc) · 1.05 KB
/
08.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Day8
using Test
function getInput()
data = readline("08.txt")
layers = [data[i:i+149] for i in 1:150:14999]
end
numberOfChars(layer, c = '0') = count(x -> x == c, collect(layer))
@test numberOfChars("154852365962") == 0
@test numberOfChars("104802360960") == 4
@test numberOfChars("00000000") == 8
function part1()
layers = getInput()
minLayer = argmin(map(numberOfChars, layers))
numberOfChars(layers[minLayer], '2') * numberOfChars(layers[minLayer], '1')
end
function getPixel(layers, pos, layerIndex = 1)
if layers[layerIndex][pos] == '2'
getPixel(layers, pos, layerIndex + 1)
else
layers[layerIndex][pos]
end
end
@test getPixel(["002012", "221001"], 1) == '0'
@test getPixel(["002012", "221001"], 3) == '1'
@test getPixel(["002012", "221001"], 4, 2) == '0'
function part2()
layers = getInput()
s = collect(getPixel(layers, i) for i in 1:150)
for i in 1:150
print(s[i] == '1' ? 'X' : ' ')
if i % 25 == 0
println()
end
end
end
println(part1())
part2()
end