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
|
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
)
func main() {
if len(os.Args) < 2 {
panic("runtime error: missing operand")
}
file, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
image := sliceify(file, 25, 6, len(file)/(25*6))
checksum(image)
decode(image)
}
func sliceify(file []byte, width int, height int, depth int) [][][]int {
var image [][][]int
pointer := 0
for i := 0; i < depth; i++ {
var b [][]int
for j := 0; j < height; j++ {
var a []int
for k := 0; k < width; k++ {
element, err := strconv.Atoi(string(file[pointer]))
if err != nil {
panic(err)
}
a = append(a, element)
pointer++
}
b = append(b, a)
}
image = append(image, b)
}
return image
}
func count(layer [][]int, val int) int {
total := 0
for i := 0; i < len(layer); i++ {
for j := 0; j < len(layer[i]); j++ {
if layer[i][j] == val {
total++
}
}
}
return total
}
func checksum(image [][][]int) {
min := count(image[0], 0)
layer := -1
for i := 0; i < len(image); i++ {
total := count(image[i], 0)
if total < min {
min = total
layer = i
}
}
fmt.Println(count(image[layer], 1) * count(image[layer], 2))
}
func decode(image [][][]int) {
for i := 0; i < len(image[0]); i++ {
for j := 0; j < len(image[0][0]); j++ {
for k := 0; k < len(image); k++ {
if image[k][i][j] == 0 {
fmt.Print(" ")
break
} else if image[k][i][j] == 1 {
fmt.Print("X")
break
}
}
}
fmt.Println()
}
}
|