Progress on 2018.11

This commit is contained in:
Brian Buller 2018-12-12 19:17:16 -06:00
parent 18d07e81db
commit 0cff916625
1 changed files with 28 additions and 32 deletions

View File

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