Skip to content

Commit 647e6b8

Browse files
committed
Yes
1 parent b30a47a commit 647e6b8

File tree

2 files changed

+87
-36
lines changed

2 files changed

+87
-36
lines changed

day09/main.go

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"log"
7+
"math"
78
"os"
89
"strconv"
910
"strings"
@@ -13,6 +14,10 @@ type Point struct {
1314
X, Y int
1415
}
1516

17+
func (c *Point) String() string {
18+
return fmt.Sprintf("%d,%d", c.X, c.Y)
19+
}
20+
1621
func main() {
1722
positionVisitedByTail, err := MoveRope(os.Stdin, (len(os.Args) >= 2 && os.Args[1] == "debug"))
1823
if err != nil {
@@ -28,6 +33,8 @@ func MoveRope(input io.Reader, debug bool) (positionVisitedByTail int, err error
2833

2934
positionsVisited := []*Point{}
3035

36+
pointsVisited := map[string]struct{}{}
37+
3138
err = IterateLines(input, func(s string) Step {
3239
direction := ""
3340
distance := 0
@@ -52,13 +59,20 @@ func MoveRope(input io.Reader, debug bool) (positionVisitedByTail int, err error
5259

5360
move := DrawGrid(head, tail, []*Point{})
5461

55-
x, y := Follow(tail, head)
56-
tail.X += x
57-
tail.Y += y
62+
// x := tail.X
63+
// y := tail.Y
64+
65+
Follow(head, tail)
5866

59-
if x != 0 || y != 0 { // tail moved
67+
// if x != tail.X || y != tail.Y { // tail moved
68+
// positionsVisited = append(positionsVisited, &Point{X: tail.X, Y: tail.Y})
69+
// positionVisitedByTail++
70+
// }
71+
72+
if _, ok := pointsVisited[tail.String()]; !ok {
73+
pointsVisited[tail.String()] = struct{}{}
6074
positionsVisited = append(positionsVisited, &Point{X: tail.X, Y: tail.Y})
61-
positionVisitedByTail++
75+
positionVisitedByTail = len(positionsVisited)
6276
}
6377

6478
if debug {
@@ -78,6 +92,42 @@ func MoveRope(input io.Reader, debug bool) (positionVisitedByTail int, err error
7892
return
7993
}
8094

95+
func Follow(head *Point, tail *Point) {
96+
// If the head is right next to or directly on top of the tail then we do nothing
97+
diffInX := math.Abs(float64(tail.X - head.X))
98+
diffInY := math.Abs(float64(tail.Y - head.Y))
99+
100+
if diffInY <= 1 && diffInX <= 1 {
101+
return
102+
}
103+
104+
//fmt.Println(fmt.Sprintf("Tail moving (%s) to meet head (%s)", tail.String(), head.String()))
105+
xDirection := 1
106+
yDirection := 1
107+
108+
if head.Y < tail.Y {
109+
yDirection = -1
110+
}
111+
112+
if head.X < tail.X {
113+
xDirection = -1
114+
}
115+
116+
if head.X == tail.X {
117+
tail.Y += 1 * yDirection
118+
return
119+
}
120+
121+
if head.Y == tail.Y {
122+
tail.X += 1 * xDirection
123+
return
124+
}
125+
126+
// Diagonal
127+
tail.X += 1 * xDirection
128+
tail.Y += 1 * yDirection
129+
}
130+
81131
func DrawGrid(head *Point, tail *Point, otherPoints []*Point) *strings.Builder {
82132
var strBuilder = &strings.Builder{}
83133

@@ -114,33 +164,3 @@ func DrawGrid(head *Point, tail *Point, otherPoints []*Point) *strings.Builder {
114164

115165
return strBuilder
116166
}
117-
118-
func Follow(point *Point, target *Point) (x, y int) {
119-
// only follow if distance between two point is greater then 1
120-
if point.X == target.X && point.Y == target.Y {
121-
return 0, 0
122-
}
123-
124-
// check if adjacent
125-
for tx := -1; tx <= 1; tx++ {
126-
for ty := -1; ty <= 1; ty++ {
127-
if point.X+tx == target.X && point.Y+ty == target.Y {
128-
return 0, 0
129-
}
130-
}
131-
}
132-
133-
if point.X < target.X {
134-
x = 1
135-
} else if point.X > target.X {
136-
x = -1
137-
}
138-
139-
if point.Y < target.Y {
140-
y = 1
141-
} else if point.Y > target.Y {
142-
y = -1
143-
}
144-
145-
return
146-
}

day09/main_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ func TestMoveRope(t *testing.T) {
6464
wantErr: false,
6565
},
6666
// {
67+
// name: "example",
68+
// args: args{
69+
// input: strings.NewReader(strings.Join([]string{
70+
// "U 1",
71+
// "R 1",
72+
// "U 1",
73+
// "R 1",
74+
// "U 1",
75+
// "R 3",
76+
// }, "\n")),
77+
// debug: true,
78+
// },
79+
// wantUniquePositionsVisitedByTail: 13,
80+
// wantErr: false,
81+
// },
82+
// {
83+
// name: "example",
84+
// args: args{
85+
// input: strings.NewReader(strings.Join([]string{
86+
// "U 3",
87+
// "R 2",
88+
// }, "\n")),
89+
// debug: true,
90+
// },
91+
// wantUniquePositionsVisitedByTail: 13,
92+
// wantErr: false,
93+
// },
94+
// {
6795
// name: "example2",
6896
// args: args{
6997
// input: strings.NewReader(testInput2),
@@ -120,7 +148,10 @@ func TestFollow(t *testing.T) {
120148
}
121149
for _, tt := range tests {
122150
t.Run(tt.name, func(t *testing.T) {
123-
gotX, gotY := Follow(tt.args.point, tt.args.target)
151+
Follow(tt.args.point, tt.args.target)
152+
gotX := tt.args.point.X
153+
gotY := tt.args.point.Y
154+
124155
if gotX != tt.wantX {
125156
t.Errorf("Follow() gotX = %v, want %v", gotX, tt.wantX)
126157
}

0 commit comments

Comments
 (0)