Day 9
This commit is contained in:
116
day09/common/bridge.go
Normal file
116
day09/common/bridge.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type Direction int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Right Direction = iota
|
||||||
|
Left
|
||||||
|
Down
|
||||||
|
Up
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type Movement struct {
|
||||||
|
direction Direction
|
||||||
|
distance int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type Position struct {
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPosition(x int, y int) *Position {
|
||||||
|
return &Position{x, y}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type Grid struct {
|
||||||
|
rope []Position
|
||||||
|
head *Position
|
||||||
|
tail *Position
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGrid(size int) *Grid {
|
||||||
|
rope := make([]Position, size)
|
||||||
|
return &Grid{rope, &rope[0], &rope[size-1]}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (grid *Grid) Move(movement Movement) (map[Position]struct{}) {
|
||||||
|
visitedTail := make(map[Position]struct{})
|
||||||
|
|
||||||
|
visitedTail[*grid.tail] = struct{}{}
|
||||||
|
|
||||||
|
for i := 0; i < movement.distance; i++ {
|
||||||
|
switch movement.direction {
|
||||||
|
case Right:
|
||||||
|
grid.head.x++
|
||||||
|
case Left:
|
||||||
|
grid.head.x--
|
||||||
|
case Up:
|
||||||
|
grid.head.y--
|
||||||
|
case Down:
|
||||||
|
grid.head.y++
|
||||||
|
}
|
||||||
|
grid.Update()
|
||||||
|
visitedTail[*grid.tail] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return visitedTail
|
||||||
|
}
|
||||||
|
|
||||||
|
func (grid *Grid) Update() {
|
||||||
|
for i := 1; i < len(grid.rope); i++ {
|
||||||
|
head := &(grid.rope[i-1])
|
||||||
|
tail := &(grid.rope[i])
|
||||||
|
if tail.x < head.x - 1 || tail.x > head.x + 1 ||
|
||||||
|
tail.y < head.y - 1 || tail.y > head.y + 1 {
|
||||||
|
if tail.x == head.x {
|
||||||
|
tail.y = (tail.y + head.y) / 2
|
||||||
|
} else if tail.y == head.y {
|
||||||
|
tail.x = (tail.x + head.x) / 2
|
||||||
|
} else if tail.x == head.x - 1 || tail.x == head.x + 1 {
|
||||||
|
tail.x = head.x
|
||||||
|
tail.y = (tail.y + head.y) / 2
|
||||||
|
} else if tail.y == head.y - 1 || tail.y == head.y + 1 {
|
||||||
|
tail.x = (tail.x + head.x) / 2
|
||||||
|
tail.y = head.y
|
||||||
|
} else {
|
||||||
|
tail.x = (tail.x + head.x) / 2
|
||||||
|
tail.y = (tail.y + head.y) / 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func Parse(scanner bufio.Scanner) []Movement {
|
||||||
|
movements := []Movement{}
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
var movement Movement
|
||||||
|
distance, _ := strconv.Atoi(line[2:])
|
||||||
|
switch line[0] {
|
||||||
|
case 'L':
|
||||||
|
movement = Movement{Left, distance}
|
||||||
|
case 'R':
|
||||||
|
movement = Movement{Right, distance}
|
||||||
|
case 'U':
|
||||||
|
movement = Movement{Up, distance}
|
||||||
|
case 'D':
|
||||||
|
movement = Movement{Down, distance}
|
||||||
|
}
|
||||||
|
movements = append(movements, movement)
|
||||||
|
}
|
||||||
|
|
||||||
|
return movements
|
||||||
|
}
|
||||||
23
day09/ex1/main.go
Normal file
23
day09/ex1/main.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"aoc2022/day09/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
movements := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||||
|
grid := common.NewGrid(2)
|
||||||
|
visited := make(map[common.Position]struct{})
|
||||||
|
|
||||||
|
for _, movement := range movements {
|
||||||
|
tailVisited := grid.Move(movement)
|
||||||
|
for position := range tailVisited {
|
||||||
|
visited[position] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(len(visited))
|
||||||
|
}
|
||||||
23
day09/ex2/main.go
Normal file
23
day09/ex2/main.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"aoc2022/day09/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
movements := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||||
|
grid := common.NewGrid(10)
|
||||||
|
visited := make(map[common.Position]struct{})
|
||||||
|
|
||||||
|
for _, movement := range movements {
|
||||||
|
tailVisited := grid.Move(movement)
|
||||||
|
for position := range tailVisited {
|
||||||
|
visited[position] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(len(visited))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user