Progress on 2018.11

This commit is contained in:
Brian Buller 2018-12-12 19:17:16 -06:00
parent 18d07e81db
commit 0cff916625

View File

@ -23,26 +23,25 @@ func main() {
serial: Atoi(os.Args[1]),
fuelLevels: make(map[string]int),
}
fmt.Println(timeMachine.calcSquareFuelLevel(90, 269, 16))
fmt.Println(timeMachine.getSquareFuelLevel(283, 169, 13))
os.Exit(0)
highest := MinInt
highestX, highestY := 0, 0
highestSize := 1
for size := 1; size <= 20; size++ {
fmt.Println(size)
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.getSquareLevel(i, j, size)
w := timeMachine.getSquareFuelLevel(i, j, size)
if w > highest {
highestX, highestY = i, j
highest = w
highestSize = size
highest = w
fmt.Printf("New Highest: %d,%d,%d : %d\n", highestX, highestY, highestSize, highest)
}
}
}
}
fmt.Print(highestX, ",", highestY, ",", highestSize, " : ", highest, "\n")
fmt.Printf("Highest: %d,%d,%d : %d\n", highestX, highestY, highestSize, highest)
}
type TimeMachine struct {
@ -57,34 +56,30 @@ func (t *TimeMachine) getKeyForSquare(x, y, size int) string {
return bx + "," + by + "," + bs
}
func (t *TimeMachine) setSquareLevel(x, y, size, val int) {
t.fuelLevels[t.getKeyForSquare(x, y, size)] = val
}
func (t *TimeMachine) getSquareLevel(x, y, size int) int {
key := t.getKeyForSquare(x, y, size)
if ret, ok := t.fuelLevels[key]; !ok {
t.fuelLevels[key] = t.calcSquareLevel(x, y, size)
return t.fuelLevels[key]
} else {
return ret
func (t *TimeMachine) getSquareFuelLevel(x, y, size int) int {
k := t.getKeyForSquare(x, y, size)
if v, ok := t.fuelLevels[k]; ok {
return v
}
}
func (t *TimeMachine) calcSquareLevel(x, y, size int) int {
var ret int
if size == 1 {
return t.calcFuelLevel(x, y)
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)
}
ret := t.getSquareLevel(x, y, size-1)
for wx := x; wx < wx+size; wx++ {
ret += t.getSquareLevel(wx, y+size-1, 1)
// Add the bottom cells
for i := 0; i < size-1; i++ {
ret += t.calcFuelLevel(x+i, y+(size-1))
}
for wy := y; wy < wy+size-1; wy++ {
ret += t.getSquareLevel(x+size-1, wy, 1)
}
t.fuelLevels[k] = ret
return ret
}
// calcFuelLevel calculates one cells fuel level
func (t *TimeMachine) calcFuelLevel(x, y int) int {
rackId := x + 10
startingLevel := rackId * y
@ -99,11 +94,12 @@ func (t *TimeMachine) calcFuelLevel(x, y int) int {
return ret
}
// calcSquareFuelLevel calculates all cells from x, y -> x+size, y+size
func (t *TimeMachine) calcSquareFuelLevel(x, y, size int) int {
var ret int
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
ret += t.calcFuelLevel(i, j)
ret += t.calcFuelLevel(x+i, y+j)
}
}
return ret