-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
99 lines (83 loc) · 1.88 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
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"io"
"math"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, maxRow, maxCol int) int {
s := lib.ReaderToString(input)
var layers [][][]int
grid := newGrid(maxRow, maxCol)
for i := 0; i < len(s); i++ {
if i%(maxRow*maxCol) == 0 && i != 0 {
layers = append(layers, grid)
grid = newGrid(maxRow, maxCol)
}
row := (i % (maxRow * maxCol)) / maxCol
col := (i % (maxRow * maxCol)) % maxCol
grid[row][col] = lib.RuneToInt(rune(s[i]))
}
layers = append(layers, grid)
fewestZero := math.MaxInt
res := 0
for _, layer := range layers {
occurences := make(map[int]int)
for row := 0; row < maxRow; row++ {
for col := 0; col < maxCol; col++ {
occurences[layer[row][col]]++
}
}
if occurences[0] < fewestZero {
res = occurences[1] * occurences[2]
fewestZero = occurences[0]
}
}
return res
}
func newGrid(maxRow, maxCol int) [][]int {
grid := make([][]int, maxRow)
for i := 0; i < maxRow; i++ {
grid[i] = make([]int, maxCol)
}
return grid
}
func fs2(input io.Reader, maxRow, maxCol int) {
s := lib.ReaderToString(input)
var layers [][][]int
grid := newGrid(maxRow, maxCol)
for i := 0; i < len(s); i++ {
if i%(maxRow*maxCol) == 0 && i != 0 {
layers = append(layers, grid)
grid = newGrid(maxRow, maxCol)
}
row := (i % (maxRow * maxCol)) / maxCol
col := (i % (maxRow * maxCol)) % maxCol
grid[row][col] = lib.RuneToInt(rune(s[i]))
}
layers = append(layers, grid)
grid = newGrid(maxRow, maxCol)
for row := 0; row < maxRow; row++ {
for col := 0; col < maxCol; col++ {
for _, layer := range layers {
v := layer[row][col]
if v == 2 {
continue
}
grid[row][col] = v
break
}
}
}
for row := 0; row < maxRow; row++ {
for col := 0; col < maxCol; col++ {
switch grid[row][col] {
case 1:
fmt.Print(".")
case 0:
fmt.Print(" ")
}
}
fmt.Println()
}
}