-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.go
111 lines (89 loc) · 2.09 KB
/
day9.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright © 2022 Jacob Saporito
*/
package cmd
import (
"fmt"
"strconv"
"time"
"github.com/japorito/merry/libxmas/toybag"
"github.com/japorito/merry/libxmas/xmas"
"github.com/spf13/cobra"
)
var directions = map[string]*Coordinate{
"R": {x: 1, y: 0},
"L": {x: -1, y: 0},
"U": {x: 0, y: 1},
"D": {x: 0, y: -1},
}
type Coordinate struct {
x, y int
}
func moveHead(head, direction *Coordinate) {
head.x = head.x + direction.x
head.y = head.y + direction.y
}
func abs(num int) int {
if num < 0 {
num = -num
}
return num
}
func moveTail(head, tail *Coordinate) {
dx, dy := (head.x - tail.x), (head.y - tail.y)
// only move if not already close
if abs(dx) > 1 || abs(dy) > 1 {
if dx != 0 {
dx = dx / abs(dx)
}
if dy != 0 {
dy = dy / abs(dy)
}
tail.x = tail.x + dx
tail.y = tail.y + dy
}
}
func runMoves(input [][]string, knots int) map[string]bool {
rope := make([]Coordinate, knots)
head := &rope[0]
tail := &rope[knots-1]
tailVisits := make(map[string]bool)
for _, move := range input {
direction := directions[move[0]]
count, _ := strconv.Atoi(move[1])
for i := 0; i < count; i++ {
moveHead(head, direction)
for j := 1; j < knots; j++ {
moveTail(&rope[j-1], &rope[j])
}
key := fmt.Sprintf("%d:%d", tail.x, tail.y)
tailVisits[key] = true
}
}
return tailVisits
}
// day9Cmd represents the day9 command
var day9Cmd = &cobra.Command{
Use: "day9 path/to/input/file",
Short: "AoC Day 9",
Long: `Advent of Code Day 9: Rope Bridge`,
Run: func(cmd *cobra.Command, args []string) {
if input := toybag.ReadAsTokenizedStringSlice(args...); input != nil {
fmt.Printf("%d input lines read.\n", len(input))
defer xmas.PrintHolidayMessage(time.Now())
if Parts.Has(1) {
fmt.Println("Part 1 running...")
visits := runMoves(input, 2)
fmt.Printf("Rope tail visits **%d** coordinates.\n", len(visits))
}
if Parts.Has(2) {
fmt.Println("Part 2 running...")
visits := runMoves(input, 10)
fmt.Printf("Longer rope tail visits **%d** coordinates.\n", len(visits))
}
}
},
}
func init() {
rootCmd.AddCommand(day9Cmd)
}