@@ -11,8 +11,13 @@ import (
11
11
)
12
12
13
13
func Solve () {
14
+
15
+ testInput := strings .Split ("R 1,U 1,L 2,D 2,R 2,U 2,L 1,D 1" , "," )
16
+ drawMap (testInput )
17
+ testInput2 := strings .Split ("R 4,U 4,L 3,D 1,R 4,D 1,L 5,R 2" , "," )
18
+ drawMap (testInput2 )
19
+
14
20
input := advent .ReadInput ("day9" )
15
- // testInput := strings.Split("R 1,U 1,L 2,D 2,R 2,U 2,L 1,D 1", ",")
16
21
splitInput := input .Split ("\n " )
17
22
drawMap (splitInput )
18
23
}
@@ -33,7 +38,6 @@ func drawMap(input []string) {
33
38
xAxis := 0
34
39
xAxisMax := 0
35
40
xAxisMin := 0
36
-
37
41
headMap := SnakeMap {positions : []positions {}}
38
42
snekMap := SnakeMap {positions : []positions {}}
39
43
for _ , row := range input {
@@ -47,7 +51,6 @@ func drawMap(input []string) {
47
51
if yAxis < yAxisMin {
48
52
yAxisMin = yAxis
49
53
}
50
-
51
54
}
52
55
if splitRow [0 ] == "U" {
53
56
for step := 0 ; step < conv ; step ++ {
@@ -77,12 +80,8 @@ func drawMap(input []string) {
77
80
}
78
81
}
79
82
}
80
- fmt .Printf ("X: %v, Xmin: %v, Xmax: %v, Y: %v, Ymin: %v, Ymax: %v\n " , xAxis , xAxisMin , xAxisMax , yAxis , yAxisMin , yAxisMax )
81
- fmt .Println (headMap .positions )
82
83
drawTail (headMap .positions , & snekMap )
83
- fmt .Println (snekMap .positions )
84
- fmt .Printf ("Unique positions for input: %v, snake: %v\n " , len (lo .Uniq (headMap .positions )), len (lo .Uniq (snekMap .positions )))
85
-
84
+ fmt .Printf ("Unique positions for head: %v, tail: %v\n " , len (lo .Uniq (headMap .positions )), len (lo .Uniq (snekMap .positions )))
86
85
}
87
86
func (snakeMap * SnakeMap ) addPosition (xValue int , yValue int ) {
88
87
position := positions {x : xValue , y : yValue }
@@ -92,17 +91,15 @@ func drawTail(headPositions []positions, snekMap *SnakeMap) {
92
91
snekMap .positions = []positions {{x : 0 , y : 0 }, {x : 0 , y : 0 }}
93
92
for posIndex , position := range headPositions {
94
93
if posIndex > 1 {
95
- tailPosition := headPositions [posIndex - 2 ]
96
94
prevPosition := headPositions [posIndex - 1 ]
97
- pointDiff := float64 (position .x - tailPosition .x + position .y - tailPosition .y )
98
- pointDiff2 := float64 (position .x - snekMap .positions [posIndex - 1 ].x + position .y - snekMap .positions [posIndex - 1 ].y )
99
-
100
- if posIndex > 4 {
101
- fmt .Printf ("PointDiff: %v, PointDiff2: %v, Pos: %v, Snake: %v\n " , pointDiff , pointDiff2 , position , snekMap .positions [posIndex - 1 ])
102
- }
103
- if math .Abs (pointDiff ) > 1 && math .Abs (pointDiff2 ) > 0 {
104
- bothAx := position .x != tailPosition .x && position .y != tailPosition .y
105
- if bothAx {
95
+ xAx := math .Abs (float64 (position .x - snekMap .positions [posIndex - 1 ].x ))
96
+ yAx := math .Abs (float64 (position .y - snekMap .positions [posIndex - 1 ].y ))
97
+ // distance := math.Abs(float64(position.x - tailPosition.x + position.y - tailPosition.y))
98
+ if xAx + yAx >= 2 {
99
+ bothAx := position .x != snekMap .positions [posIndex - 1 ].x && position .y != snekMap .positions [posIndex - 1 ].y
100
+ if xAx + yAx >= 3 {
101
+ snekMap .positions = append (snekMap .positions , prevPosition )
102
+ } else if bothAx {
106
103
snekMap .positions = append (snekMap .positions , snekMap .positions [posIndex - 1 ])
107
104
} else {
108
105
snekMap .positions = append (snekMap .positions , prevPosition )
@@ -113,12 +110,3 @@ func drawTail(headPositions []positions, snekMap *SnakeMap) {
113
110
}
114
111
}
115
112
}
116
-
117
- // [{0 0} {0 -1} {0 -2} {1 -2} {2 -2} {2 -3} {2 -2} {1 -2} {0 -2} {0 -1} {0 0} {0 -1}]
118
- // [{0 0} {0 0} {0 -1} {0 -1} {1 -2} {1 -2} {1 -2} {1 -2} {1 -2} {1 -2} {0 -1} {0 -1}]
119
-
120
- // [{0 0} {1 0} {1 1} {0 1} {-1 1} {-1 0} {-1 -1} {0 -1} {1 -1} {1 0} {1 1} {0 1}]
121
- // [{0 0} {0 0} {0 0} {0 0} { 0 0} { 0 0} {-1 0} {-1 0} {0 -1} {0 -1} {1 0} {1 0}]
122
-
123
- // {3 4} {2 4} {1 4} {1 3} {2 3} {3 3} {4 3}
124
- // {4 3} {3 4} {2 4} {2 4} {2 4} {2 4} {3 3}
0 commit comments