Skip to content

Commit

Permalink
Day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Nov 8, 2024
1 parent 6b9531f commit 01ce479
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A small practice project created to get familiar with Go syntax.
- [x] day 14
- [x] day 15
- [x] day 16
- [ ] day 17
- [x] day 17
- [ ] day 18
- [ ] day 19
- [ ] day 20
Expand Down
46 changes: 46 additions & 0 deletions day17/day17.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,52 @@ func Solution1(filepath string) int {
}

func Solution2(filepath string) int {
m := readInput(filepath)

pq := PQ{}
seen := make(map[string]bool)
pq.push([6]int{0, 0, 0, 0, 0, 0}) // heat loss, y, x, dY, dX, n of steps in straight line

for pq.len() > 0 {
heatLoss, y, x, dY, dX, n := pq.pop()
// fmt.Printf("DEBUG: hl=%d, y=%d, x=%d, dY=%d, dX=%d, n=%d\n", heatLoss, y, x, dY, dX, n)
if y == len(m)-1 && x == len(m[y])-1 && n >= 4 {
return heatLoss
}

seenKey := fmt.Sprintf("%d;%d;%d;%d;%d", y, x, dY, dX, n)
if seen[seenKey] {
continue
}
seen[seenKey] = true

// keep going in the same direction
if n < 10 && [2]int{dY, dX} != [2]int{0, 0} {
newY, newX := y+dY, x+dX
if newY < 0 || newY >= len(m) || newX < 0 || newX >= len(m[0]) {
// do nothing, I don't want to invert this condition
} else {
pq.push([6]int{heatLoss + m[newY][newX], newY, newX, dY, dX, n + 1})
}
}

if n != 0 && n < 4 {
continue
}
// explore all the other directions except for the current and its reverse
for _, diffs := range [][2]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
newDY, newDX := diffs[0], diffs[1]
newY, newX := y+newDY, x+newDX
if newDir := [2]int{newDY, newDX}; newDir == [2]int{dY, dX} || newDir == [2]int{-dY, -dX} {
continue
}
if newY < 0 || newY >= len(m) || newX < 0 || newX >= len(m[newY]) {
continue
}
pq.push([6]int{heatLoss + m[newY][newX], newY, newX, newDY, newDX, 1})
}

}
return -1
}

Expand Down
7 changes: 4 additions & 3 deletions day17/day17_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ func TestSolution1(t *testing.T) {
for input, expectedResult := range cases {
result := Solution1(input)

assert.Equal(t, result, expectedResult, "Solution1()")
assert.Equal(t, expectedResult, result, "Solution1()")
}
}

func TestSolution2(t *testing.T) {
cases := map[string]int{
"test_input11": 0,
"test_input11": 94,
"test_input12": 71,
}

for input, expectedResult := range cases {
result := Solution2(input)

assert.Equal(t, result, expectedResult, "Solution2()")
assert.Equal(t, expectedResult, result, "Solution2()")
}
}

Expand Down
5 changes: 5 additions & 0 deletions day17/test_input12
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
111111111111
999999999991
999999999991
999999999991
999999999991

0 comments on commit 01ce479

Please sign in to comment.