From dd94ac5a9379ca5abc4bfc0a5efd2c3d6f373435 Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Fri, 9 Dec 2022 07:45:55 +0100 Subject: [PATCH] Day 9 --- day09/common/bridge.go | 116 +++++++++++++++++++++++++++++++++++++++++ day09/ex1/main.go | 23 ++++++++ day09/ex2/main.go | 23 ++++++++ 3 files changed, 162 insertions(+) create mode 100644 day09/common/bridge.go create mode 100644 day09/ex1/main.go create mode 100644 day09/ex2/main.go diff --git a/day09/common/bridge.go b/day09/common/bridge.go new file mode 100644 index 0000000..50c0f6e --- /dev/null +++ b/day09/common/bridge.go @@ -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 +} diff --git a/day09/ex1/main.go b/day09/ex1/main.go new file mode 100644 index 0000000..7620986 --- /dev/null +++ b/day09/ex1/main.go @@ -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)) +} diff --git a/day09/ex2/main.go b/day09/ex2/main.go new file mode 100644 index 0000000..98fdb95 --- /dev/null +++ b/day09/ex2/main.go @@ -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)) +}