2018 day 11 complete!
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user