87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package day08
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"tools"
|
|
)
|
|
|
|
func shiftRight(display *tools.GridToggle, row, maxCol int) {
|
|
buf := display.State(maxCol, row)
|
|
for x := maxCol; x > 0; x-- {
|
|
display.Set(x, row, display.State(x-1, row))
|
|
}
|
|
display.Set(0, row, buf)
|
|
}
|
|
|
|
func shiftDown(display *tools.GridToggle, column, maxRow int) {
|
|
buf := display.State(column, maxRow)
|
|
for y := maxRow; y > 0; y-- {
|
|
display.Set(column, y, display.State(column, y-1))
|
|
}
|
|
display.Set(column, 0, buf)
|
|
}
|
|
|
|
func printDisplay(display tools.GridToggle, dimX, dimY int) {
|
|
for y := 0; y <= dimY; y++ {
|
|
for x := 0; x <= dimX; x++ {
|
|
if display.State(x, y) {
|
|
fmt.Print("#")
|
|
} else {
|
|
fmt.Print(" ")
|
|
}
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func buildDisplayFromInput(input []string) tools.GridToggle {
|
|
dims := strings.Split(input[0], ",")
|
|
dimX, _ := strconv.Atoi(dims[0])
|
|
dimY, _ := strconv.Atoi(dims[1])
|
|
dimX--
|
|
dimY--
|
|
|
|
display := tools.NewGridToggle()
|
|
for _, instruction := range input[1:] {
|
|
parts := strings.Split(instruction, " ")
|
|
switch parts[0] {
|
|
case "rect":
|
|
rectxy := strings.Split(parts[1], "x")
|
|
rectx, _ := strconv.Atoi(rectxy[0])
|
|
recty, _ := strconv.Atoi(rectxy[1])
|
|
for x := 0; x < rectx; x++ {
|
|
for y := 0; y < recty; y++ {
|
|
display.Set(x, y, tools.On)
|
|
}
|
|
}
|
|
case "rotate":
|
|
which, _ := strconv.Atoi(parts[2][2:])
|
|
amount, _ := strconv.Atoi(parts[4])
|
|
switch parts[1] {
|
|
case "row":
|
|
for i := 0; i < amount; i++ {
|
|
shiftRight(&display, which, dimX)
|
|
}
|
|
case "column":
|
|
for i := 0; i < amount; i++ {
|
|
shiftDown(&display, which, dimY)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return display
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
return buildDisplayFromInput(puzzle.GetInputArray()).GetOnCount()
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
display := buildDisplayFromInput(puzzle.GetInputArray())
|
|
printDisplay(display, 49, 5)
|
|
return 0
|
|
}
|