-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (77 loc) · 1.29 KB
/
main.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"math"
"strconv"
"github.com/sanderploegsma/advent-of-code/2019/go/utils"
)
const w, h = 25, 6
func main() {
input, _ := utils.ReadFile("input.txt")
layers := ParseLayers(input)
fmt.Println(CheckLayers(layers))
image := OverlayLayers(layers)
for i := 0; i < w*h; i++ {
if i%w == 0 {
fmt.Print("\n")
}
if image[i] == 1 {
fmt.Print("█")
} else {
fmt.Print(" ")
}
}
}
func CheckLayers(layers [][]int) (out int) {
minZeroes := math.MaxInt32
for _, l := range layers {
zeroes, ones, twos := 0, 0, 0
for _, d := range l {
switch d {
case 0:
zeroes++
case 1:
ones++
case 2:
twos++
}
}
if zeroes < minZeroes {
minZeroes = zeroes
out = ones * twos
}
}
return out
}
func OverlayLayers(layers [][]int) []int {
res := make([]int, w*h)
for i := range res {
res[i] = 2
}
for _, l := range layers {
for i, d := range l {
if res[i] == 2 {
res[i] = d
}
}
}
return res
}
func ParseLayers(input string) [][]int {
layers := make([][]int, len(input)/(w*h))
cur := make([]int, w*h)
x := 0
y := 0
for i := 0; i < len(input); i++ {
if x >= w*h {
layers[y] = cur
cur = make([]int, w*h)
x = 0
y++
}
cur[x], _ = strconv.Atoi(string(input[i]))
x++
}
layers[y] = cur
return layers
}