Skip to content

Commit e45e26a

Browse files
committed
fuck this one
1 parent 96af941 commit e45e26a

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

day09/main.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
7+
"io/ioutil"
68
"log"
79
"math"
810
"os"
911
"strconv"
1012
"strings"
1113
)
1214

15+
// 6181
16+
1317
type Point struct {
1418
X, Y int
1519
}
@@ -19,19 +23,33 @@ func (c *Point) String() string {
1923
}
2024

2125
func main() {
22-
positionVisitedByTail, err := MoveRope(os.Stdin, (len(os.Args) >= 2 && os.Args[1] == "debug"))
26+
rawBody, err := ioutil.ReadAll(os.Stdin)
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
positionVisitedByTail, err := MoveRope(ioutil.NopCloser(bytes.NewBuffer(rawBody)), (len(os.Args) >= 2 && os.Args[1] == "debug"), 1)
2332
if err != nil {
2433
log.Fatal(err)
2534
}
2635

2736
fmt.Printf("Part 1: %d\n", positionVisitedByTail)
37+
38+
x, err := MoveRope(ioutil.NopCloser(bytes.NewBuffer(rawBody)), (len(os.Args) >= 2 && os.Args[1] == "debug"), 9)
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
43+
fmt.Printf("Part 2: %d\n", x)
2844
}
2945

30-
func MoveRope(input io.Reader, debug bool) (positionVisitedByTail int, err error) {
46+
func MoveRope(input io.Reader, debug bool, tailSize int) (positionVisitedByTail int, err error) {
3147
head := &Point{X: 0, Y: 0}
32-
tail := &Point{X: 0, Y: 0}
48+
tail := []*Point{}
3349

34-
positionsVisited := []*Point{}
50+
for i := 0; i < tailSize; i++ {
51+
tail = append(tail, &Point{X: 0, Y: 0})
52+
}
3553

3654
pointsVisited := map[string]struct{}{}
3755

@@ -45,7 +63,6 @@ func MoveRope(input io.Reader, debug bool) (positionVisitedByTail int, err error
4563
}
4664

4765
for i := 0; i < distance; i++ {
48-
4966
switch direction {
5067
case "U":
5168
head.Y++
@@ -57,24 +74,19 @@ func MoveRope(input io.Reader, debug bool) (positionVisitedByTail int, err error
5774
head.X++
5875
}
5976

60-
move := DrawGrid(head, tail, []*Point{})
61-
62-
Follow(head, tail)
63-
64-
if _, ok := pointsVisited[tail.String()]; !ok {
65-
pointsVisited[tail.String()] = struct{}{}
66-
positionsVisited = append(positionsVisited, &Point{X: tail.X, Y: tail.Y})
67-
positionVisitedByTail = len(positionsVisited)
77+
h := head
78+
for i := 0; i < len(tail); i++ {
79+
Follow(h, tail[i])
80+
h = tail[i]
6881
}
6982

83+
lastTail := tail[len(tail)-1]
84+
pointsVisited[lastTail.String()] = struct{}{}
85+
positionVisitedByTail = len(pointsVisited)
86+
7087
if debug {
7188
fmt.Printf("Head: %v, Tail: %v Direction: %s, Distance: %d/%d Positions: %d \n", head, tail, direction, i+1, distance, positionVisitedByTail)
72-
73-
fmt.Printf("%s", DrawLinesNextToEachOther(
74-
"Head:\n"+move.String(),
75-
"Tail:\n"+DrawGrid(head, tail, []*Point{}).String(),
76-
"visited:\n"+DrawGrid(nil, nil, positionsVisited).String(),
77-
))
89+
fmt.Printf("%s", DrawGrid(head, tail).String())
7890
}
7991
}
8092

@@ -93,7 +105,6 @@ func Follow(head *Point, tail *Point) {
93105
return
94106
}
95107

96-
//fmt.Println(fmt.Sprintf("Tail moving (%s) to meet head (%s)", tail.String(), head.String()))
97108
xDirection := 1
98109
yDirection := 1
99110

@@ -120,7 +131,7 @@ func Follow(head *Point, tail *Point) {
120131
tail.Y += 1 * yDirection
121132
}
122133

123-
func DrawGrid(head *Point, tail *Point, otherPoints []*Point) *strings.Builder {
134+
func DrawGrid(head *Point, otherPoints []*Point) *strings.Builder {
124135
var strBuilder = &strings.Builder{}
125136

126137
const width = 6
@@ -131,15 +142,13 @@ func DrawGrid(head *Point, tail *Point, otherPoints []*Point) *strings.Builder {
131142
for x := 0; x < width; x++ {
132143
if head != nil && x == head.X && y == head.Y {
133144
strBuilder.WriteString("H")
134-
} else if tail != nil && x == tail.X && y == tail.Y {
135-
strBuilder.WriteString("T")
136145
} else if x == 0 && y == 0 {
137146
strBuilder.WriteString("s")
138147
} else {
139148
match := false
140-
for _, p := range otherPoints {
149+
for i, p := range otherPoints {
141150
if p.X == x && p.Y == y {
142-
strBuilder.WriteString("#")
151+
strBuilder.WriteString(fmt.Sprintf("%d", i+1))
143152
match = true
144153
break
145154
}

day09/main_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ func Test_main(t *testing.T) {
4545

4646
func TestMoveRope(t *testing.T) {
4747
type args struct {
48-
input io.Reader
49-
debug bool
48+
input io.Reader
49+
debug bool
50+
tailSize int
5051
}
5152
tests := []struct {
5253
name string
@@ -57,8 +58,9 @@ func TestMoveRope(t *testing.T) {
5758
{
5859
name: "example",
5960
args: args{
60-
input: strings.NewReader(testInput),
61-
debug: true,
61+
input: strings.NewReader(testInput),
62+
debug: true,
63+
tailSize: 9,
6264
},
6365
wantUniquePositionsVisitedByTail: 13,
6466
wantErr: false,
@@ -121,7 +123,7 @@ func TestMoveRope(t *testing.T) {
121123
}
122124
for _, tt := range tests {
123125
t.Run(tt.name, func(t *testing.T) {
124-
gotUniquePositionsVisitedByTail, err := MoveRope(tt.args.input, tt.args.debug)
126+
gotUniquePositionsVisitedByTail, err := MoveRope(tt.args.input, tt.args.debug, tt.args.tailSize)
125127
if (err != nil) != tt.wantErr {
126128
t.Errorf("MoveRope() error = %v, wantErr %v", err, tt.wantErr)
127129
return

0 commit comments

Comments
 (0)