Browse Source

Unoptimised day 8

master
Chris Smith 4 years ago
parent
commit
5b549b2d76
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
2 changed files with 52 additions and 0 deletions
  1. 1
    0
      08/input.txt
  2. 51
    0
      08/main.go

+ 1
- 0
08/input.txt
File diff suppressed because it is too large
View File


+ 51
- 0
08/main.go View File

@@ -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
+}

Loading…
Cancel
Save