|
@@ -0,0 +1,51 @@
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "fmt"
|
|
5
|
+ "github.com/csmith/aoc-2019/common"
|
|
6
|
+ "math"
|
|
7
|
+)
|
|
8
|
+
|
|
9
|
+const width = 25
|
|
10
|
+const height = 6
|
|
11
|
+
|
|
12
|
+func checkChecksum(layer string, fewestZeros *int, checksum *int) {
|
|
13
|
+ counts := []int{0, 0, 0}
|
|
14
|
+ for _, r := range layer {
|
|
15
|
+ counts[r-'0']++
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ if counts[0] < *fewestZeros {
|
|
19
|
+ *fewestZeros = counts[0]
|
|
20
|
+ *checksum = counts[1] * counts[2]
|
|
21
|
+ }
|
|
22
|
+}
|
|
23
|
+
|
|
24
|
+func main() {
|
|
25
|
+ var (
|
|
26
|
+ input = common.ReadFileAsStrings("08/input.txt")[0]
|
|
27
|
+ fewestZeros = math.MaxInt64
|
|
28
|
+ checksum = 0
|
|
29
|
+ pixels [width * height]rune
|
|
30
|
+ )
|
|
31
|
+
|
|
32
|
+ for i := len(input) - width*height; i >= 0; i -= width * height {
|
|
33
|
+ layer := input[i : i+width*height]
|
|
34
|
+ checkChecksum(layer, &fewestZeros, &checksum)
|
|
35
|
+ for i, r := range layer {
|
|
36
|
+ if r == '0' {
|
|
37
|
+ pixels[i] = ' '
|
|
38
|
+ } else if r == '1' {
|
|
39
|
+ pixels[i] = '█'
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ fmt.Printf("%d\n", checksum)
|
|
45
|
+ for i, p := range pixels {
|
|
46
|
+ fmt.Printf("%c", p)
|
|
47
|
+ if i%width == width-1 {
|
|
48
|
+ fmt.Printf("\n")
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+}
|