fine, I'll put them on github
This commit is contained in:
commit
c3bc233d82
9 changed files with 472 additions and 0 deletions
72
3/3.2.go
Normal file
72
3/3.2.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package main
|
||||
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "math"
|
||||
import "strconv"
|
||||
|
||||
// go run 3.2.go <input>
|
||||
|
||||
// Need to brute force part two anyway, should have done that for part one...
|
||||
|
||||
func main() {
|
||||
|
||||
target, _ := strconv.Atoi(os.Args[1])
|
||||
|
||||
// This is way overkill for the real solution, but needed it for testing initial grid size with value = i
|
||||
squareSize := int(math.Ceil(math.Sqrt(float64(target)))) + 2
|
||||
grid := make([][]int, squareSize)
|
||||
|
||||
for i := 0; i < squareSize; i++ {
|
||||
y := make([]int, squareSize)
|
||||
grid[i] = y
|
||||
}
|
||||
|
||||
dirX := 1
|
||||
dirY := 0
|
||||
startVal := 1
|
||||
posX := (squareSize - 1) / 2
|
||||
posY := (squareSize - 1) / 2
|
||||
grid[posX][posY] = 1
|
||||
|
||||
fmt.Println("sq", squareSize, "xy", posX, posY, "dir", dirX, dirY)
|
||||
|
||||
for i := startVal + 1; i <= target; i++ {
|
||||
|
||||
posX += dirX
|
||||
posY += dirY
|
||||
|
||||
adj := grid[posX-1][posY-1] + grid[posX-1][posY] + grid[posX-1][posY+1] +
|
||||
grid[posX][posY-1] + grid[posX][posY+1] +
|
||||
grid[posX+1][posY-1] + grid[posX+1][posY] + grid[posX+1][posY+1]
|
||||
|
||||
//adj = i
|
||||
|
||||
grid[posX][posY] = adj
|
||||
if adj > target {
|
||||
fmt.Println("Result:", adj)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if dirY == -1 && grid[posX+1][posY] == 0 {
|
||||
dirY = 0
|
||||
dirX = 1
|
||||
}
|
||||
if dirY == 1 && grid[posX-1][posY] == 0 {
|
||||
dirY = 0
|
||||
dirX = -1
|
||||
}
|
||||
if dirX == -1 && grid[posX][posY-1] == 0 {
|
||||
dirY = -1
|
||||
dirX = 0
|
||||
}
|
||||
if dirX == 1 && grid[posX][posY+1] == 0 {
|
||||
dirY = 1
|
||||
dirX = 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//fmt.Println(grid)
|
||||
|
||||
}
|
90
3/3.go
Normal file
90
3/3.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
import "math"
|
||||
import "time"
|
||||
import "os"
|
||||
import "strconv"
|
||||
|
||||
// go run 3.go <input>
|
||||
|
||||
/*
|
||||
|
||||
"Debugging is twice as hard as writing the code in the first place.
|
||||
Therefore, if you write the code as cleverly as possible, you are,
|
||||
by definition, not smart enough to debug it." -- Brian W. Kernighan
|
||||
|
||||
I probably should have just brute forced this, it took me ages to track
|
||||
down the off-by-one error I had for even squares *some* of the time...
|
||||
|
||||
*/
|
||||
|
||||
func main() {
|
||||
|
||||
start := time.Now()
|
||||
|
||||
target, _ := strconv.Atoi(os.Args[1])
|
||||
squareSize := int(math.Ceil(math.Sqrt(float64(target))))
|
||||
startVal := (squareSize-1)*(squareSize-1) + 1
|
||||
|
||||
dirX := 0
|
||||
dirY := 0
|
||||
min := 0
|
||||
max := 0
|
||||
posX := 0
|
||||
posY := 0
|
||||
|
||||
if (squareSize % 2) == 1 {
|
||||
min = -1 * (squareSize - 1) / 2
|
||||
max = (squareSize - 1) / 2
|
||||
dirX = 0
|
||||
dirY = -1
|
||||
posX = min
|
||||
posY = max
|
||||
|
||||
fmt.Println("odd", squareSize)
|
||||
fmt.Println("start:", startVal, "min:", min, "max:", max)
|
||||
|
||||
} else {
|
||||
min = (-1 * (squareSize) / 2) + 1
|
||||
max = (squareSize) / 2
|
||||
dirX = 0
|
||||
dirY = 1
|
||||
posX = max
|
||||
posY = min
|
||||
|
||||
fmt.Println("even", squareSize)
|
||||
fmt.Println("start:", startVal, "min:", min, "max:", max)
|
||||
|
||||
}
|
||||
|
||||
for i := startVal; i < target; i++ {
|
||||
fmt.Println(i, posX, posY)
|
||||
|
||||
if dirY == -1 && posY == min {
|
||||
dirY = 0
|
||||
dirX = 1
|
||||
}
|
||||
if dirY == 1 && posY == max {
|
||||
dirY = 0
|
||||
dirX = -1
|
||||
}
|
||||
if dirX == -1 && posX == min {
|
||||
dirY = -1
|
||||
dirX = 0
|
||||
}
|
||||
if dirX == 1 && posX == max {
|
||||
dirY = 1
|
||||
dirX = 0
|
||||
}
|
||||
|
||||
posX += dirX
|
||||
posY += dirY
|
||||
}
|
||||
|
||||
fmt.Println("result:", posX, posY, (math.Abs(float64(posX)) + math.Abs(float64(posY))))
|
||||
|
||||
t := time.Now()
|
||||
elapsed := t.Sub(start)
|
||||
fmt.Println(elapsed, "ms")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue