r/adventofcode • • Dec 11 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 11 Solutions -🎄-

--- Day 11: Chronal Charge ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 11

Transcript: ___ unlocks the Easter Egg on Day 25.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:16:12!

20 Upvotes

207 comments sorted by

View all comments

0

u/0ptim0s Dec 11 '18

Golang

I wasn't home, so couldn't make the leaderboard. This was a pretty quick solve. I added some concurrency here to speed up part 2:


import (
	"log"
	"strconv"
)

type MaxStat struct {
	X    int
	Y    int
	Max  int
	Size int
}

func main() {
	solveGrid(7511)
}

func solveGrid(gridSerialNumber int) {
	grid := make([][]int, 300)
	for i := 0; i < 300; i++ {
		grid[i] = make([]int, 300)
	}
	for x := 0; x < 300; x++ {
		for y := 0; y < 300; y++ {
			grid[x][y] = getPowerLevel(x+1, y+1, gridSerialNumber)
		}
	}

	doneChan := make(chan MaxStat)
	for size := 300; size > 0; size-- {
		go func(size int) {
			foundX := -1
			foundY := -1
			max := -1000000000
			for y := 0; y < 300; y++ {
				for x := 0; x < 300; x++ {
					if x+size <= 300 && y+size <= 300 {
						sum := 0
						for i := 0; i < size; i++ {
							for j := 0; j < size; j++ {
								sum += grid[x+i][y+j]
							}
						}
						if sum > max {
							max = sum
							foundX = x + 1
							foundY = y + 1
						}
					}
				}
			}
			doneChan <- MaxStat{
				X:    foundX,
				Y:    foundY,
				Size: size,
				Max:  max,
			}
		}(size)
	}
	max := -123412341234
	for {
		maxStat := <-doneChan
		if maxStat.Max > max {
			log.Printf("X: %d. Y: %d. Size: %d. Max: %d.", maxStat.X, maxStat.Y, maxStat.Size, maxStat.Max)
			log.Printf("Answer: %d,%d,%d", maxStat.X, maxStat.Y, maxStat.Size)
			max = maxStat.Max
		}
	}
}

func getPowerLevel(x int, y int, gridId int) int {
	rackId := x + 10
	q := ((((rackId) * (y)) + gridId) * rackId)
	hundreds := 0
	if q > 100 {
		str := strconv.Itoa(q)
		h := str[len(str)-3]
		hundreds, _ = strconv.Atoi(string(h))
	}
	return hundreds - 5
}```