r/adventofcode • • Dec 06 '15

SOLUTION MEGATHREAD --- Day 6 Solutions ---

--- Day 6: Probably a Fire Hazard ---

Post your solution as a comment. Structure your post like the Day Five thread.

22 Upvotes

171 comments sorted by

View all comments

1

u/segfaultvicta Dec 06 '15

Horribly ugly Go / golang solution.

I initially tried to do something cute with structs and then it took forever and ate up my entire RAM. Complexity theory wooo! Then I actually thought about the problem and went back to my initial idea, which I'd given up on because I was trying to make the leaderboards and I was having trouble testing today's code in a meaningful way (I really hope things don't continue to get less and less automated-testable, although I feel like they might) - at this point I understand how I'd do it, actually, but I got hasty and thought I would run into #mathfail (which, it's true, I -did- in fact #mathfail really hard - damn you, off by one errors!)

Anyways, who needs nested structures when you can have flat arrays and do offset arithmetic! Who needs regexes when you can do snappy linear-time string comparison!

package main

import (
	"strconv"
	"strings"
)

func day6sideA(lines []string) string {
	var lights [1000000]bool
	for _, line := range lines {
		split := strings.Split(line, " ")

		if split[0] == "toggle" {
			a_strs := strings.Split(split[1], ",")
			b_strs := strings.Split(split[3], ",")
			a_x, _ := strconv.Atoi(a_strs[0])
			a_y, _ := strconv.Atoi(a_strs[1])
			b_x, _ := strconv.Atoi(b_strs[0])
			b_y, _ := strconv.Atoi(b_strs[1])
			for i := a_x; i <= b_x; i++ {
				for j := a_y; j <= b_y; j++ {
					lights[j*1000+i] = !lights[j*1000+i]
				}
			}

		} else if split[1] == "on" {
			a_strs := strings.Split(split[2], ",")
			b_strs := strings.Split(split[4], ",")
			a_x, _ := strconv.Atoi(a_strs[0])
			a_y, _ := strconv.Atoi(a_strs[1])
			b_x, _ := strconv.Atoi(b_strs[0])
			b_y, _ := strconv.Atoi(b_strs[1])
			for i := a_x; i <= b_x; i++ {
				for j := a_y; j <= b_y; j++ {
					lights[j*1000+i] = true
				}
			}
		} else {
			a_strs := strings.Split(split[2], ",")
			b_strs := strings.Split(split[4], ",")
			a_x, _ := strconv.Atoi(a_strs[0])
			a_y, _ := strconv.Atoi(a_strs[1])
			b_x, _ := strconv.Atoi(b_strs[0])
			b_y, _ := strconv.Atoi(b_strs[1])
			for i := a_x; i <= b_x; i++ {
				for j := a_y; j <= b_y; j++ {
					lights[j*1000+i] = false
				}
			}
		}
	}
	count := 0
	for i := 0; i < 1000000; i++ {
		if lights[i] == true {
			count += 1
		}
	}
	return strconv.Itoa(count)
}

func day6sideB(lines []string) string {
	var lights [1000000]int
	for _, line := range lines {
		split := strings.Split(line, " ")

		if split[0] == "toggle" {
			a_strs := strings.Split(split[1], ",")
			b_strs := strings.Split(split[3], ",")
			a_x, _ := strconv.Atoi(a_strs[0])
			a_y, _ := strconv.Atoi(a_strs[1])
			b_x, _ := strconv.Atoi(b_strs[0])
			b_y, _ := strconv.Atoi(b_strs[1])
			for i := a_x; i <= b_x; i++ {
				for j := a_y; j <= b_y; j++ {
					lights[j*1000+i] = lights[j*1000+i] + 2
				}
			}

		} else if split[1] == "on" {
			a_strs := strings.Split(split[2], ",")
			b_strs := strings.Split(split[4], ",")
			a_x, _ := strconv.Atoi(a_strs[0])
			a_y, _ := strconv.Atoi(a_strs[1])
			b_x, _ := strconv.Atoi(b_strs[0])
			b_y, _ := strconv.Atoi(b_strs[1])
			for i := a_x; i <= b_x; i++ {
				for j := a_y; j <= b_y; j++ {
					lights[j*1000+i] = lights[j*1000+i] + 1
				}
			}
		} else {
			a_strs := strings.Split(split[2], ",")
			b_strs := strings.Split(split[4], ",")
			a_x, _ := strconv.Atoi(a_strs[0])
			a_y, _ := strconv.Atoi(a_strs[1])
			b_x, _ := strconv.Atoi(b_strs[0])
			b_y, _ := strconv.Atoi(b_strs[1])
			for i := a_x; i <= b_x; i++ {
				for j := a_y; j <= b_y; j++ {
					lights[j*1000+i] = lights[j*1000+i] - 1
					if lights[j*1000+i] < 0 {
						lights[j*1000+i] = 0
					}
				}
			}
		}
	}
	var count int = 0
	for i := 0; i < 1000000; i++ {
		count += lights[i]
	}
	return strconv.Itoa(count)
}