Skip to content

Commit ebf2224

Browse files
committed
advent: day9 part 1
1 parent 5e2fc52 commit ebf2224

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

advent/day9/day9.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ import (
1111
)
1212

1313
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+
1420
input := advent.ReadInput("day9")
15-
// testInput := strings.Split("R 1,U 1,L 2,D 2,R 2,U 2,L 1,D 1", ",")
1621
splitInput := input.Split("\n")
1722
drawMap(splitInput)
1823
}
@@ -33,7 +38,6 @@ func drawMap(input []string) {
3338
xAxis := 0
3439
xAxisMax := 0
3540
xAxisMin := 0
36-
3741
headMap := SnakeMap{positions: []positions{}}
3842
snekMap := SnakeMap{positions: []positions{}}
3943
for _, row := range input {
@@ -47,7 +51,6 @@ func drawMap(input []string) {
4751
if yAxis < yAxisMin {
4852
yAxisMin = yAxis
4953
}
50-
5154
}
5255
if splitRow[0] == "U" {
5356
for step := 0; step < conv; step++ {
@@ -77,12 +80,8 @@ func drawMap(input []string) {
7780
}
7881
}
7982
}
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)
8283
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)))
8685
}
8786
func (snakeMap *SnakeMap) addPosition(xValue int, yValue int) {
8887
position := positions{x: xValue, y: yValue}
@@ -92,17 +91,15 @@ func drawTail(headPositions []positions, snekMap *SnakeMap) {
9291
snekMap.positions = []positions{{x: 0, y: 0}, {x: 0, y: 0}}
9392
for posIndex, position := range headPositions {
9493
if posIndex > 1 {
95-
tailPosition := headPositions[posIndex-2]
9694
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 {
106103
snekMap.positions = append(snekMap.positions, snekMap.positions[posIndex-1])
107104
} else {
108105
snekMap.positions = append(snekMap.positions, prevPosition)
@@ -113,12 +110,3 @@ func drawTail(headPositions []positions, snekMap *SnakeMap) {
113110
}
114111
}
115112
}
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

Comments
 (0)