fine, I'll put them on github
This commit is contained in:
commit
c3bc233d82
9 changed files with 472 additions and 0 deletions
37
1/1.go
Normal file
37
1/1.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// go run 1.go <input>
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
s := strings.Split(os.Args[1], "")
|
||||||
|
checksum := 0
|
||||||
|
checksum2 := 0
|
||||||
|
|
||||||
|
for index, element := range s {
|
||||||
|
pre := index - 1
|
||||||
|
pre2 := (index + (len(s) / 2)) % len(s)
|
||||||
|
if pre == -1 {
|
||||||
|
pre = len(s) - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(index, element, pre, s[pre])
|
||||||
|
|
||||||
|
if element == s[pre] {
|
||||||
|
i, _ := strconv.Atoi(element)
|
||||||
|
checksum += i
|
||||||
|
}
|
||||||
|
if element == s[pre2] {
|
||||||
|
i, _ := strconv.Atoi(element)
|
||||||
|
checksum2 += i
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(checksum, checksum2)
|
||||||
|
}
|
51
2/2.2.go
Normal file
51
2/2.2.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
|
import "bufio"
|
||||||
|
|
||||||
|
// cat input | go run 2.2.go
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
checksum := 0
|
||||||
|
for scanner.Scan() {
|
||||||
|
|
||||||
|
s := strings.Split(scanner.Text(), "\t")
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
|
for index, element := range s {
|
||||||
|
fmt.Println(index, element)
|
||||||
|
i, _ := strconv.Atoi(element)
|
||||||
|
|
||||||
|
for index2, e2 := range s {
|
||||||
|
|
||||||
|
i2, _ := strconv.Atoi(e2)
|
||||||
|
|
||||||
|
if index == index2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if i2 > i {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result := float64(i) / float64(i2)
|
||||||
|
ir := float64(int(result))
|
||||||
|
|
||||||
|
if result == ir {
|
||||||
|
fmt.Println("yes!", i, i2, result)
|
||||||
|
checksum += int(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("---")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(checksum)
|
||||||
|
}
|
41
2/2.go
Normal file
41
2/2.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
|
import "bufio"
|
||||||
|
|
||||||
|
// cat input | go run 2.go
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
checksum := 0
|
||||||
|
for scanner.Scan() {
|
||||||
|
|
||||||
|
min := 9999999999999 // messy, but whatever.
|
||||||
|
max := 0
|
||||||
|
s := strings.Split(scanner.Text(), "\t")
|
||||||
|
|
||||||
|
for index, element := range s {
|
||||||
|
fmt.Println(index, element)
|
||||||
|
i, _ := strconv.Atoi(element)
|
||||||
|
if i < min {
|
||||||
|
min = i
|
||||||
|
}
|
||||||
|
if i > max {
|
||||||
|
max = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diff := max - min
|
||||||
|
checksum += diff
|
||||||
|
fmt.Println("min", min, "max", max, "diff", diff)
|
||||||
|
fmt.Println("---")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(checksum)
|
||||||
|
|
||||||
|
}
|
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")
|
||||||
|
}
|
43
4/4.2.go
Normal file
43
4/4.2.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "strings"
|
||||||
|
import "bufio"
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
// cat input | go run 4.2.go
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
failed := 0
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
count++
|
||||||
|
s := strings.Split(scanner.Text(), " ")
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
an := strings.Split(s[i], "")
|
||||||
|
sort.Strings(an)
|
||||||
|
san := strings.Join(an, "")
|
||||||
|
s[i] = san
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(s)
|
||||||
|
|
||||||
|
for i := 1; i < len(s); i++ {
|
||||||
|
fmt.Println(i, s[i])
|
||||||
|
if s[i] == s[i-1] {
|
||||||
|
failed++
|
||||||
|
fmt.Println("Failed!", s[i], s[i-1])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("---")
|
||||||
|
}
|
||||||
|
fmt.Println(failed, count, count-failed)
|
||||||
|
}
|
35
4/4.go
Normal file
35
4/4.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "strings"
|
||||||
|
import "bufio"
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
// cat input | go run 4.go
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
failed := 0
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
count++
|
||||||
|
s := strings.Split(scanner.Text(), " ")
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
|
sort.Strings(s)
|
||||||
|
for i := 1; i < len(s); i++ {
|
||||||
|
fmt.Println(i, s[i])
|
||||||
|
if s[i] == s[i-1] {
|
||||||
|
failed++
|
||||||
|
fmt.Println("Failed!")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("---")
|
||||||
|
}
|
||||||
|
fmt.Println(failed, count, count-failed)
|
||||||
|
}
|
41
5/5.go
Normal file
41
5/5.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "bufio"
|
||||||
|
import "os"
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// cat input | go run 5.go
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
var instructions []int
|
||||||
|
i := 0
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
i, _ := strconv.Atoi(scanner.Text())
|
||||||
|
instructions = append(instructions, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
|
||||||
|
if i >= len(instructions) {
|
||||||
|
fmt.Println(count)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
count++
|
||||||
|
current := instructions[i]
|
||||||
|
|
||||||
|
if instructions[i] < 3 {
|
||||||
|
instructions[i]++ // Just this one for the first solution
|
||||||
|
} else {
|
||||||
|
instructions[i]--
|
||||||
|
}
|
||||||
|
i += current
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
62
6/6.go
Normal file
62
6/6.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// go run 6.go N N N N
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
var banks []int
|
||||||
|
seen := make(map[string]int)
|
||||||
|
|
||||||
|
s := os.Args[1:]
|
||||||
|
for _, element := range s {
|
||||||
|
i, _ := strconv.Atoi(element)
|
||||||
|
banks = append(banks, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(banks)
|
||||||
|
|
||||||
|
count := 0
|
||||||
|
first := 0
|
||||||
|
for {
|
||||||
|
count++
|
||||||
|
|
||||||
|
// find largest (lowest indexed wins)
|
||||||
|
largest := 0
|
||||||
|
largestSize := -1
|
||||||
|
for pos, size := range banks {
|
||||||
|
if largestSize < size {
|
||||||
|
largestSize = size
|
||||||
|
largest = pos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(largest, largestSize)
|
||||||
|
|
||||||
|
// redistribute
|
||||||
|
banks[largest] = 0
|
||||||
|
for i := 0; i < largestSize; i++ {
|
||||||
|
largest = (largest + 1) % len(banks)
|
||||||
|
banks[largest]++
|
||||||
|
}
|
||||||
|
|
||||||
|
// laziness..
|
||||||
|
keep := fmt.Sprint(banks)
|
||||||
|
|
||||||
|
fmt.Println(count, keep)
|
||||||
|
|
||||||
|
if seen[keep] == 1 && first == 0 {
|
||||||
|
first = count
|
||||||
|
}
|
||||||
|
if seen[keep] == 2 {
|
||||||
|
fmt.Println("total:", count, "first:", first, "second:", (count - first))
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
seen[keep]++
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue