adventofcode/2015/day06/main.go
2016-12-16 16:21:15 -06:00

71 lines
1.3 KiB
Go

package main
import (
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
func main() {
var input []string
var readInp string
for {
_, err := fmt.Scan(&readInp)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
input = append(input, readInp)
}
var lights [][]int
for i := 0; i < 1000; i++ {
lights = append(lights, make([]int, 1000))
}
for i := range input {
if input[i] == "on" || input[i] == "off" || input[i] == "toggle" {
stX, stY := parseLocations(input[i+1])
endX, endY := parseLocations(input[i+3])
for ix := stX; ix <= endX; ix++ {
for iy := stY; iy <= endY; iy++ {
if input[i] == "on" {
lights[ix][iy]++
} else if input[i] == "off" {
if lights[ix][iy] > 0 {
lights[ix][iy]--
}
} else if input[i] == "toggle" {
lights[ix][iy] += 2
}
}
}
}
}
var brightness int
for i := 0; i < 1000; i++ {
for j := 0; j < 1000; j++ {
brightness += lights[i][j]
}
}
fmt.Printf("Brightness: %d\n", brightness)
}
func parseLocations(s string) (int, int) {
dirSplit := strings.Split(s, ",")
return assertAtoi(dirSplit[0]), assertAtoi(dirSplit[1])
}
func assertAtoi(s string) int {
ret, err := strconv.Atoi(s)
if err != nil {
fmt.Println("Error in Atoi: " + s)
os.Exit(1)
}
return ret
}