This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.go
127 lines (115 loc) · 3.05 KB
/
22.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"strconv"
"strings"
_ "embed"
)
//go:embed 22.txt
var fileContent string
func main() {
inputRows := strings.Split(fileContent, "\r\n")
type Instruction struct {
xMin int
xMax int
yMin int
yMax int
zMin int
zMax int
turnOn bool
}
// instructions := make([]Instruction, len(inputRows))
var instructions []Instruction
var minX int = 1 << 62
var maxX int
var minY int = 1 << 62
var maxY int
var minZ int = 1 << 62
var maxZ int
for _, inputRow := range inputRows {
var instruction Instruction
instructionSplit := strings.Split(inputRow, " ")
instruction.turnOn = instructionSplit[0] == "on"
coordinatesSplit := strings.Split(instructionSplit[1], ",")
for _, split := range coordinatesSplit {
targetSplit := strings.Split(split, "=")
valuesSplit := strings.Split(targetSplit[1], "..")
min, err := strconv.Atoi(valuesSplit[0])
if err != nil {
panic(err)
}
max, err := strconv.Atoi(valuesSplit[1])
if err != nil {
panic(err)
}
switch targetSplit[0] {
case "x":
instruction.xMin = min
instruction.xMax = max
case "y":
instruction.yMin = min
instruction.yMax = max
case "z":
instruction.zMin = min
instruction.zMax = max
}
}
if instruction.xMin >= -50 && instruction.xMax <= 50 &&
instruction.yMin >= -50 && instruction.yMax <= 50 &&
instruction.zMin >= -50 && instruction.zMax <= 50 {
// instructions[i] = instruction
instructions = append(instructions, instruction)
if instruction.xMin < minX {
minX = instruction.xMin
}
if instruction.xMax > maxX {
maxX = instruction.xMax
}
if instruction.yMin < minY {
minY = instruction.yMin
}
if instruction.yMax > maxY {
maxY = instruction.yMax
}
if instruction.zMin < minZ {
minZ = instruction.zMin
}
if instruction.zMax > maxZ {
maxZ = instruction.zMax
}
}
}
gridWidth := maxX - minX + 1
gridHeight := maxY - minY + 1
gridDepth := maxZ - minZ + 1
fmt.Println(instructions)
fmt.Println(gridWidth,"*",gridHeight,"*",gridDepth,"\tx:",minX,"=>",maxX," y:", minY,"=>",maxY,
" z:",minZ,"=>",maxZ)
grid := make([]bool, gridWidth*gridHeight*gridDepth)
turnedOnCubesCount := 0
for _, instruction := range instructions {
// fmt.Println(i+1,":",instruction)
for z := 0; z <= instruction.zMax-instruction.zMin; z++ {
for y := 0; y <= instruction.yMax-instruction.yMin; y++ {
for x := 0; x <= instruction.xMax-instruction.xMin; x++ {
coord := (z+instruction.zMin-minZ)*gridHeight*gridWidth +
(y+instruction.yMin-minY)*gridWidth +
(x + instruction.xMin - minX)
// fmt.Println("\t", x +instruction.xMin, y + instruction.yMin,
// z + instruction.zMin,"\t",x,y,z ,"=>",coord,)
if grid[coord] != instruction.turnOn {
if instruction.turnOn {
turnedOnCubesCount++
// fmt.Println("\t\t+1")
} else {
turnedOnCubesCount--
// fmt.Println("\t\t-1")
}
}
grid[coord] = instruction.turnOn
}
}
}
}
fmt.Println(turnedOnCubesCount, "turned on cubes")
}