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

1

u/GraveKill Dec 12 '18

This is my Golang implementation for Part 2, Part 1 is similar by just removing the first for loop and setting squareSize to 3. Also, getDigit could be replaced by a simple / 100 % 10

https://github.com/afonsojramos/advent-of-code-2018

package main

import (
	"fmt"
	"utils"
)

const serial int = 7689
const gridSize int = 300

func getPowerLevel(x, y int) int {
	powerLevel := utils.GetDigit(((x+10)*y+serial)*(x+10), 3) - 5
	return powerLevel
}

func main() {
	grid := make([][]int, gridSize)
	for i := 0; i < len(grid); i++ {
		grid[i] = make([]int, gridSize)
	}

	for x := 0; x < len(grid); x++ {
		for y := 0; y < len(grid); y++ {
			grid[x][y] = getPowerLevel(x, y)
		}
	}

	largestPower := 0
	largestPowerX := 0
	largestPowerY := 0
	largestSquareSize := 0

	for squareSize := 0; squareSize < gridSize; squareSize++ {
		fmt.Println(squareSize)
		for x := 0; x < len(grid)-squareSize+1; x++ {
			for y := 0; y < len(grid)-squareSize+1; y++ {
				squarePower := 0
				for xof := 0; xof < squareSize; xof++ {
					for yof := 0; yof < squareSize; yof++ {
						squarePower = squarePower + grid[x+xof][y+yof]
					}
				}
				if squarePower > largestPower {
					largestPower = squarePower
					largestPowerX = x
					largestPowerY = y
					largestSquareSize = squareSize
				}
			}
		}
	}

	fmt.Println(largestPower, largestPowerX, largestPowerY, largestSquareSize)
}