mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
2015-day06: grid toggling/incrementing & decrementing
This commit is contained in:
@@ -0,0 +1,119 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var part int
|
||||||
|
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||||
|
flag.Parse()
|
||||||
|
fmt.Println("Running part", part)
|
||||||
|
|
||||||
|
var ans int
|
||||||
|
if part == 1 {
|
||||||
|
ans = part1(util.ReadFile("./input.txt"))
|
||||||
|
} else {
|
||||||
|
ans = part2(util.ReadFile("./input.txt"))
|
||||||
|
}
|
||||||
|
fmt.Println("Output:", ans)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(input string) int {
|
||||||
|
// 1000x1000 grid
|
||||||
|
grid := make([][]bool, 1000)
|
||||||
|
for i := range grid {
|
||||||
|
grid[i] = make([]bool, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(input, "\n") {
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(line, "toggle"):
|
||||||
|
var row1, col1, row2, col2 int
|
||||||
|
fmt.Sscanf(line, "toggle %d,%d through %d,%d", &row1, &col1, &row2, &col2)
|
||||||
|
for i := row1; i <= row2; i++ {
|
||||||
|
for j := col1; j <= col2; j++ {
|
||||||
|
grid[i][j] = !grid[i][j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "turn on"):
|
||||||
|
var row1, col1, row2, col2 int
|
||||||
|
fmt.Sscanf(line, "turn on %d,%d through %d,%d", &row1, &col1, &row2, &col2)
|
||||||
|
for i := row1; i <= row2; i++ {
|
||||||
|
for j := col1; j <= col2; j++ {
|
||||||
|
grid[i][j] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "turn off"):
|
||||||
|
var row1, col1, row2, col2 int
|
||||||
|
fmt.Sscanf(line, "turn off %d,%d through %d,%d", &row1, &col1, &row2, &col2)
|
||||||
|
for i := row1; i <= row2; i++ {
|
||||||
|
for j := col1; j <= col2; j++ {
|
||||||
|
grid[i][j] = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("unhandled instruction")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var count int
|
||||||
|
for _, row := range grid {
|
||||||
|
for _, b := range row {
|
||||||
|
if b {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(input string) int {
|
||||||
|
grid := make([][]int, 1000)
|
||||||
|
for i := range grid {
|
||||||
|
grid[i] = make([]int, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(input, "\n") {
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(line, "toggle"):
|
||||||
|
var row1, col1, row2, col2 int
|
||||||
|
fmt.Sscanf(line, "toggle %d,%d through %d,%d", &row1, &col1, &row2, &col2)
|
||||||
|
for i := row1; i <= row2; i++ {
|
||||||
|
for j := col1; j <= col2; j++ {
|
||||||
|
grid[i][j] += 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "turn on"):
|
||||||
|
var row1, col1, row2, col2 int
|
||||||
|
fmt.Sscanf(line, "turn on %d,%d through %d,%d", &row1, &col1, &row2, &col2)
|
||||||
|
for i := row1; i <= row2; i++ {
|
||||||
|
for j := col1; j <= col2; j++ {
|
||||||
|
grid[i][j]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case strings.HasPrefix(line, "turn off"):
|
||||||
|
var row1, col1, row2, col2 int
|
||||||
|
fmt.Sscanf(line, "turn off %d,%d through %d,%d", &row1, &col1, &row2, &col2)
|
||||||
|
for i := row1; i <= row2; i++ {
|
||||||
|
for j := col1; j <= col2; j++ {
|
||||||
|
if grid[i][j] > 0 {
|
||||||
|
grid[i][j]--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("unhandled instruction")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var brightness int
|
||||||
|
for _, row := range grid {
|
||||||
|
for _, v := range row {
|
||||||
|
brightness += v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return brightness
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_part1(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"actual", util.ReadFile("input.txt"), 400410},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := part1(tt.input); got != tt.want {
|
||||||
|
t.Errorf("part1() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_part2(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"actual", util.ReadFile("input.txt"), 15343601},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := part2(tt.input); got != tt.want {
|
||||||
|
t.Errorf("part2() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user