Skip to content

Instantly share code, notes, and snippets.

@martinlindhe
Created December 7, 2015 10:42
Show Gist options
  • Save martinlindhe/9ff3c2c422868fda36f0 to your computer and use it in GitHub Desktop.
Save martinlindhe/9ff3c2c422868fda36f0 to your computer and use it in GitHub Desktop.
aoc 6 in go
package aoc
import (
"fmt"
"regexp"
"strconv"
)
func newChristmasLightsPart2() [1000][1000]int {
// 1 million lights in a 1000x1000 grid
return [1000][1000]int{}
}
func newChristmasLights() [1000][1000]bool {
// 1 million lights in a 1000x1000 grid
return [1000][1000]bool{}
}
func ChangeChristmasLights(lights *[1000][1000]bool, s string) {
regex := "(turn on|turn off|toggle) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)"
r, err := regexp.Compile(regex)
if err != nil {
fmt.Println("Error", err)
}
matches := r.FindStringSubmatch(s)
action := matches[1]
x1, _ := strconv.Atoi(matches[2])
y1, _ := strconv.Atoi(matches[3])
x2, _ := strconv.Atoi(matches[4])
y2, _ := strconv.Atoi(matches[5])
for x := x1; x <= x2; x++ {
for y := y1; y <= y2; y++ {
if action == "turn on" {
lights[x][y] = true
} else if action == "turn off" {
lights[x][y] = false
} else if action == "toggle" {
lights[x][y] = !lights[x][y]
}
}
}
}
func christmasLightsOn(lights *[1000][1000]bool) int {
cnt := 0
for x := 0; x < 1000; x++ {
for y := 0; y < 1000; y++ {
if lights[x][y] {
cnt++
}
}
}
return cnt
}
func christmasLightsBrightness(lights *[1000][1000]int) int {
cnt := 0
for x := 0; x < 1000; x++ {
for y := 0; y < 1000; y++ {
cnt += lights[x][y]
}
}
return cnt
}
func ChangeChristmasLightsPart2(lights *[1000][1000]int, s string) {
regex := "(turn on|turn off|toggle) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)"
r, err := regexp.Compile(regex)
if err != nil {
fmt.Println("Error", err)
}
matches := r.FindStringSubmatch(s)
action := matches[1]
x1, _ := strconv.Atoi(matches[2])
y1, _ := strconv.Atoi(matches[3])
x2, _ := strconv.Atoi(matches[4])
y2, _ := strconv.Atoi(matches[5])
for x := x1; x <= x2; x++ {
for y := y1; y <= y2; y++ {
if action == "turn on" {
lights[x][y]++
} else if action == "turn off" {
if lights[x][y] > 0 {
lights[x][y]--
}
} else if action == "toggle" {
lights[x][y] = lights[x][y] + 2
}
}
}
}
package aoc
import (
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestChristmasLights(t *testing.T) {
lights1 := newChristmasLights()
ChangeChristmasLights(&lights1, "turn on 0,0 through 999,999")
// turn on (or leave on) every light
assert.Equal(t, 1000000, christmasLightsOn(&lights1))
lights2 := newChristmasLights()
ChangeChristmasLights(&lights2, "toggle 0,0 through 999,0")
// toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off
assert.Equal(t, 1000, christmasLightsOn(&lights2))
ChangeChristmasLights(&lights1, "turn off 499,499 through 500,500")
// turn off (or leave off) the middle four lights
assert.Equal(t, 1000000-4, christmasLightsOn(&lights1))
}
func TestDay6Questions(t *testing.T) {
data, err := ioutil.ReadFile("../day6_input")
if err != nil {
t.Fatal("Error ", err)
}
s := string(data)
arr := strings.Split(s, "\n")
lights1 := newChristmasLights()
lights2 := newChristmasLightsPart2()
for _, val := range arr {
if len(val) > 0 {
ChangeChristmasLights(&lights1, val)
ChangeChristmasLightsPart2(&lights2, val)
}
}
// part 1 and 2
assert.Equal(t, 400410, christmasLightsOn(&lights1))
assert.Equal(t, 15343601, christmasLightsBrightness(&lights2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment