2018 day 11 complete!
This commit is contained in:
parent
0cff916625
commit
db575a07fc
@ -23,25 +23,48 @@ func main() {
|
||||
serial: Atoi(os.Args[1]),
|
||||
fuelLevels: make(map[string]int),
|
||||
}
|
||||
fmt.Println(timeMachine.getSquareFuelLevel(283, 169, 13))
|
||||
os.Exit(0)
|
||||
highest := MinInt
|
||||
highestX, highestY, highestSize := 0, 0, 1
|
||||
for size := 1; size <= 13; size++ {
|
||||
fmt.Println("=", size, "=")
|
||||
for i := 0; i < 300-size; i++ {
|
||||
for j := 0; j < 300-size; j++ {
|
||||
w := timeMachine.getSquareFuelLevel(i, j, size)
|
||||
if w > highest {
|
||||
highestX, highestY = i, j
|
||||
highestSize = size
|
||||
highest = w
|
||||
fmt.Printf("New Highest: %d,%d,%d : %d\n", highestX, highestY, highestSize, highest)
|
||||
timeMachine.fillGrid()
|
||||
part1()
|
||||
part2()
|
||||
}
|
||||
|
||||
func part1() {
|
||||
top := MinInt
|
||||
topX, topY := 0, 0
|
||||
for i := 1; i < 300; i++ {
|
||||
for j := 1; j < 300; j++ {
|
||||
w := timeMachine.getSquareFuelLevel(i, j, 3)
|
||||
if w > top {
|
||||
top = w
|
||||
topX, topY = i, j
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("%d,%d : %d\n", topX, topY, top)
|
||||
fmt.Println("= Part 1 =")
|
||||
timeMachine.printSquare(topX, topY, 3)
|
||||
}
|
||||
|
||||
func part2() {
|
||||
top := MinInt
|
||||
topX, topY, topS := 1, 1, 1
|
||||
for i := 1; i <= 300; i++ {
|
||||
for j := 1; j <= 300; j++ {
|
||||
maxSize := 300 - (i - 1)
|
||||
if i < j {
|
||||
maxSize = 300 - (j - 1)
|
||||
}
|
||||
for s := 1; s <= maxSize; s++ {
|
||||
wrk := timeMachine.getSquareFuelLevel(i, j, s)
|
||||
if wrk > top {
|
||||
top = wrk
|
||||
topX, topY, topS = i, j, s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("Highest: %d,%d,%d : %d\n", highestX, highestY, highestSize, highest)
|
||||
fmt.Println("= Part 2 =")
|
||||
fmt.Printf("%d,%d,%d : %d\n", topX, topY, topS, top)
|
||||
}
|
||||
|
||||
type TimeMachine struct {
|
||||
@ -49,6 +72,23 @@ type TimeMachine struct {
|
||||
fuelLevels map[string]int
|
||||
}
|
||||
|
||||
func (t *TimeMachine) printSquare(x, y, size int) {
|
||||
for j := y; j < y+size; j++ {
|
||||
for i := x; i < x+size; i++ {
|
||||
fmt.Printf("%2d ", t.getSquareFuelLevel(i, j, 1))
|
||||
}
|
||||
fmt.Println("")
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TimeMachine) fillGrid() {
|
||||
for i := 1; i <= 300; i++ {
|
||||
for j := 1; j <= 300; j++ {
|
||||
t.fuelLevels[t.getKeyForSquare(i, j, 1)] = t.calcFuelLevel(i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TimeMachine) getKeyForSquare(x, y, size int) string {
|
||||
bx := strconv.Itoa(x)
|
||||
by := strconv.Itoa(y)
|
||||
@ -61,19 +101,19 @@ func (t *TimeMachine) getSquareFuelLevel(x, y, size int) int {
|
||||
if v, ok := t.fuelLevels[k]; ok {
|
||||
return v
|
||||
}
|
||||
var ret int
|
||||
if size == 1 {
|
||||
ret = t.calcFuelLevel(x, y)
|
||||
} else {
|
||||
ret = t.getSquareFuelLevel(x, y, size-1)
|
||||
// Add the right-most cells
|
||||
for i := 0; i < size; i++ {
|
||||
ret += t.calcFuelLevel(x+(size-1), y+i)
|
||||
}
|
||||
// Add the bottom cells
|
||||
for i := 0; i < size-1; i++ {
|
||||
ret += t.calcFuelLevel(x+i, y+(size-1))
|
||||
}
|
||||
// We shouldn't be here if we filled the grid before running
|
||||
return 0
|
||||
}
|
||||
var ret int
|
||||
ret = t.getSquareFuelLevel(x, y, size-1)
|
||||
// Add the right-most cells
|
||||
for i := 0; i < size; i++ {
|
||||
ret += t.calcFuelLevel(x+(size-1), y+i)
|
||||
}
|
||||
// Add the bottom cells
|
||||
for i := 0; i < size-1; i++ {
|
||||
ret += t.calcFuelLevel(x+i, y+(size-1))
|
||||
}
|
||||
t.fuelLevels[k] = ret
|
||||
return ret
|
||||
|
103
2018/day11/problem
Normal file
103
2018/day11/problem
Normal file
@ -0,0 +1,103 @@
|
||||
Advent of Code
|
||||
|
||||
--- Day 11: Chronal Charge ---
|
||||
|
||||
You watch the Elves and their sleigh fade into the distance as they head toward the North Pole.
|
||||
|
||||
Actually, you're the one fading. The falling sensation returns.
|
||||
|
||||
The low fuel warning light is illuminated on your wrist-mounted device. Tapping it once causes it to project a hologram of the situation: a 300x300 grid of fuel cells and their
|
||||
current power levels, some negative. You're not sure what negative power means in the context of time travel, but it can't be good.
|
||||
|
||||
Each fuel cell has a coordinate ranging from 1 to 300 in both the X (horizontal) and Y (vertical) direction. In X,Y notation, the top-left cell is 1,1, and the top-right cell is
|
||||
300,1.
|
||||
|
||||
The interface lets you select any 3x3 square of fuel cells. To increase your chances of getting to your destination, you decide to choose the 3x3 square with the largest total power.
|
||||
|
||||
The power level in a given fuel cell can be found through the following process:
|
||||
|
||||
* Find the fuel cell's rack ID, which is its X coordinate plus 10.
|
||||
* Begin with a power level of the rack ID times the Y coordinate.
|
||||
* Increase the power level by the value of the grid serial number (your puzzle input).
|
||||
* Set the power level to itself multiplied by the rack ID.
|
||||
* Keep only the hundreds digit of the power level (so 12345 becomes 3; numbers with no hundreds digit become 0).
|
||||
* Subtract 5 from the power level.
|
||||
|
||||
For example, to find the power level of the fuel cell at 3,5 in a grid with serial number 8:
|
||||
|
||||
* The rack ID is 3 + 10 = 13.
|
||||
* The power level starts at 13 * 5 = 65.
|
||||
* Adding the serial number produces 65 + 8 = 73.
|
||||
* Multiplying by the rack ID produces 73 * 13 = 949.
|
||||
* The hundreds digit of 949 is 9.
|
||||
* Subtracting 5 produces 9 - 5 = 4.
|
||||
|
||||
So, the power level of this fuel cell is 4.
|
||||
|
||||
Here are some more example power levels:
|
||||
|
||||
* Fuel cell at 122,79, grid serial number 57: power level -5.
|
||||
* Fuel cell at 217,196, grid serial number 39: power level 0.
|
||||
* Fuel cell at 101,153, grid serial number 71: power level 4.
|
||||
|
||||
Your goal is to find the 3x3 square which has the largest total power. The square must be entirely within the 300x300 grid. Identify this square using the X,Y coordinate of its
|
||||
top-left fuel cell. For example:
|
||||
|
||||
For grid serial number 18, the largest total 3x3 square has a top-left corner of 33,45 (with a total power of 29); these fuel cells appear in the middle of this 5x5 region:
|
||||
|
||||
-2 -4 4 4 4
|
||||
-4 4 4 4 -5
|
||||
4 3 3 4 -4
|
||||
1 1 2 4 -3
|
||||
-1 0 2 -5 -2
|
||||
|
||||
For grid serial number 42, the largest 3x3 square's top-left is 21,61 (with a total power of 30); they are in the middle of this region:
|
||||
|
||||
-3 4 2 2 2
|
||||
-4 4 3 3 4
|
||||
-5 3 3 4 -4
|
||||
4 3 3 4 -3
|
||||
3 3 3 -5 -1
|
||||
|
||||
What is the X,Y coordinate of the top-left fuel cell of the 3x3 square with the largest total power?
|
||||
|
||||
Your puzzle answer was 243,49.
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
You discover a dial on the side of the device; it seems to let you select a square of any size, not just 3x3. Sizes from 1x1 to 300x300 are supported.
|
||||
|
||||
Realizing this, you now must find the square of any size with the largest total power. Identify this square by including its size as a third parameter after the top-left coordinate: a
|
||||
9x9 square with a top-left corner of 3,5 is identified as 3,5,9.
|
||||
|
||||
For example:
|
||||
|
||||
* For grid serial number 18, the largest total square (with a total power of 113) is 16x16 and has a top-left corner of 90,269, so its identifier is 90,269,16.
|
||||
* For grid serial number 42, the largest total square (with a total power of 119) is 12x12 and has a top-left corner of 232,251, so its identifier is 232,251,12.
|
||||
|
||||
What is the X,Y,size identifier of the square with the largest total power?
|
||||
|
||||
Your puzzle answer was 285,169,15.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
Your puzzle input was 5093.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
. https://adventofcode.com/
|
||||
. https://adventofcode.com/2018/about
|
||||
. https://adventofcode.com/2018/events
|
||||
. https://adventofcode.com/2018/settings
|
||||
. https://adventofcode.com/2018/auth/logout
|
||||
. Advent of Code Supporter
|
||||
https://adventofcode.com/2018/support
|
||||
. https://adventofcode.com/2018
|
||||
. https://adventofcode.com/2018
|
||||
. https://adventofcode.com/2018/support
|
||||
. https://adventofcode.com/2018/sponsors
|
||||
. https://adventofcode.com/2018/leaderboard
|
||||
. https://adventofcode.com/2018/stats
|
||||
. https://adventofcode.com/2018/sponsors
|
||||
. https://adventofcode.com/2018
|
Loading…
Reference in New Issue
Block a user