Day9
This commit is contained in:
parent
bae13d779e
commit
a2670664e3
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"AOC2022/helper"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Tree struct {
|
||||
@ -11,8 +12,8 @@ type Tree struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
//args := os.Args[1:]
|
||||
lines := helper.ReadTextFile("day8/input")
|
||||
args := os.Args[1:]
|
||||
lines := helper.ReadTextFile(args[0])
|
||||
forest := make([][]Tree, len(lines))
|
||||
for i, line := range lines {
|
||||
forest[i] = make([]Tree, len(line))
|
||||
|
125
day9/day9.go
Normal file
125
day9/day9.go
Normal file
@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"AOC2022/helper"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Simulation struct {
|
||||
headPos [2]int
|
||||
tailPos [][2]int
|
||||
tailPast map[[2]int]bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
lines := helper.ReadTextFile(args[0])
|
||||
tails := [][2]int{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}
|
||||
simulation := Simulation{[2]int{0, 0}, tails, make(map[[2]int]bool)}
|
||||
for _, line := range lines {
|
||||
direction, distance := getDirectionAndDistance(line)
|
||||
for i := 0; i < distance; i++ {
|
||||
move(&simulation.headPos, direction)
|
||||
for i, _ := range tails {
|
||||
simulation.moveTail(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(len(simulation.tailPast))
|
||||
}
|
||||
|
||||
func getDirectionAndDistance(line string) ([2]int, int) {
|
||||
splitLine := strings.Split(line, " ")
|
||||
distance := helper.RemoveError(strconv.Atoi(splitLine[1]))
|
||||
switch splitLine[0] {
|
||||
case "U":
|
||||
return [2]int{0, 1}, distance
|
||||
case "D":
|
||||
return [2]int{0, -1}, distance
|
||||
case "R":
|
||||
return [2]int{1, 0}, distance
|
||||
case "L":
|
||||
return [2]int{-1, 0}, distance
|
||||
default:
|
||||
return [2]int{0, 0}, distance
|
||||
}
|
||||
}
|
||||
|
||||
func (simulation *Simulation) moveTail(i int) {
|
||||
activeTail := &simulation.tailPos[i]
|
||||
if len(simulation.tailPos)-1 == i {
|
||||
simulation.tailPast[*activeTail] = true
|
||||
}
|
||||
var nextKnot [2]int
|
||||
if i == 0 {
|
||||
nextKnot = simulation.headPos
|
||||
} else {
|
||||
nextKnot = simulation.tailPos[i-1]
|
||||
}
|
||||
direction := getDirectionFromTo(nextKnot[:], activeTail[:])
|
||||
if getLength(direction[:]) < 2 {
|
||||
return
|
||||
}
|
||||
if direction[0] > 0 {
|
||||
activeTail[0]++
|
||||
}
|
||||
if direction[0] < 0 {
|
||||
activeTail[0]--
|
||||
}
|
||||
if direction[1] > 0 {
|
||||
activeTail[1]++
|
||||
}
|
||||
if direction[1] < 0 {
|
||||
activeTail[1]--
|
||||
}
|
||||
if len(simulation.tailPos)-1 == i {
|
||||
simulation.tailPast[*activeTail] = true
|
||||
}
|
||||
}
|
||||
|
||||
func move(object *[2]int, direction [2]int) {
|
||||
object[0] += direction[0]
|
||||
object[1] += direction[1]
|
||||
}
|
||||
|
||||
func getDirectionFromTo(X, Y []int) []int {
|
||||
direction := []int{}
|
||||
for i, _ := range X {
|
||||
direction = append(direction, X[i]-Y[i])
|
||||
}
|
||||
return direction
|
||||
}
|
||||
|
||||
func getLength(X []int) float64 {
|
||||
temp := float64(0)
|
||||
for i, _ := range X {
|
||||
temp += math.Pow(float64(X[i]), 2)
|
||||
}
|
||||
return math.Sqrt(temp)
|
||||
}
|
||||
|
||||
func (simulation *Simulation) print(size int) {
|
||||
for i := 0; i < size; i++ {
|
||||
tempArr := ""
|
||||
for j := 0; j < size; j++ {
|
||||
pos := [2]int{j, size - 1 - i}
|
||||
if [2]int{0, 0} == pos {
|
||||
tempArr += "S"
|
||||
} else if simulation.tailPast[pos] {
|
||||
tempArr += "#"
|
||||
} else if simulation.headPos == pos {
|
||||
tempArr += "H"
|
||||
} else if helper.Contains(simulation.tailPos, pos) {
|
||||
tempArr += "T"
|
||||
} else {
|
||||
tempArr += "."
|
||||
}
|
||||
}
|
||||
fmt.Println(tempArr)
|
||||
}
|
||||
}
|
2000
day9/input
Normal file
2000
day9/input
Normal file
File diff suppressed because it is too large
Load Diff
2000
day9/inputGattix
Normal file
2000
day9/inputGattix
Normal file
File diff suppressed because it is too large
Load Diff
8
day9/testinput
Normal file
8
day9/testinput
Normal file
@ -0,0 +1,8 @@
|
||||
R 4
|
||||
U 4
|
||||
L 3
|
||||
D 1
|
||||
R 4
|
||||
D 1
|
||||
L 5
|
||||
R 2
|
8
day9/testinput2
Normal file
8
day9/testinput2
Normal file
@ -0,0 +1,8 @@
|
||||
R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20
|
@ -77,3 +77,12 @@ func Sum[T int | int64](slice []T) (s T) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Contains[T [2]int](elems []T, v T) bool {
|
||||
for _, s := range elems {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user