-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccfaf60
commit 6b9531f
Showing
6 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package day17 | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"os" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type PQ struct { | ||
queue [][6]int | ||
} | ||
|
||
func (pq *PQ) len() int { | ||
return len((*pq).queue) | ||
} | ||
func (pq *PQ) pop() (int, int, int, int, int, int) { | ||
pq.sort() | ||
r := (*pq).queue[0] | ||
(*pq).queue = (*pq).queue[1:] | ||
|
||
return r[0], r[1], r[2], r[3], r[4], r[5] | ||
} | ||
func (pq *PQ) push(path [6]int) { | ||
(*pq).queue = append((*pq).queue, path) | ||
} | ||
func (pq *PQ) sort() { | ||
slices.SortFunc((*pq).queue, func(a, b [6]int) int { | ||
if a[0] > b[0] { | ||
return 1 | ||
} else if a[0] < b[0] { | ||
return -1 | ||
} else { | ||
return 0 | ||
} | ||
}) | ||
} | ||
|
||
func Solution1(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() | ||
if y == len(m)-1 && x == len(m[y])-1 { | ||
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 < 3 && [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}) | ||
} | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
func Solution2(filepath string) int { | ||
return -1 | ||
} | ||
|
||
func readInput(filepath string) [][]int { | ||
file, err := os.Open(filepath) | ||
if err != nil { | ||
log.Fatalf("Failed to open the input file with: %v\n", err.Error()) | ||
} | ||
|
||
var matrix [][]int | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
currentRow := strings.Split(scanner.Text(), "") | ||
currentRowNums := make([]int, len(currentRow)) | ||
for i, el := range currentRow { | ||
currentRowNums[i] = parseInt(el) | ||
} | ||
matrix = append(matrix, currentRowNums) | ||
} | ||
if err = scanner.Err(); err != nil { | ||
log.Fatalf("Error during input file read: %v\n", err.Error()) | ||
} | ||
|
||
return matrix | ||
} | ||
|
||
func parseInt(s string) int { | ||
num, err := strconv.ParseInt(s, 10, 64) | ||
if err != nil { | ||
log.Fatalf("Failed to parse number %v with: %s\n", s, err.Error()) | ||
} | ||
|
||
return int(num) | ||
} | ||
|
||
func printMatrix(m [][]int) { | ||
fmt.Println("-----MATRIX START-----") | ||
for _, row := range m { | ||
fmt.Println(row) | ||
} | ||
fmt.Println("------MATRIX END------") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package day17 | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSolution1(t *testing.T) { | ||
cases := map[string]int{ | ||
"test_input11": 102, | ||
} | ||
|
||
for input, expectedResult := range cases { | ||
result := Solution1(input) | ||
|
||
assert.Equal(t, result, expectedResult, "Solution1()") | ||
} | ||
} | ||
|
||
func TestSolution2(t *testing.T) { | ||
cases := map[string]int{ | ||
"test_input11": 0, | ||
} | ||
|
||
for input, expectedResult := range cases { | ||
result := Solution2(input) | ||
|
||
assert.Equal(t, result, expectedResult, "Solution2()") | ||
} | ||
} | ||
|
||
func TestPriorityQueue(t *testing.T) { | ||
pq := PQ{} | ||
pq.push([6]int{0, 0, 0, 0, 0, 0}) | ||
pq.push([6]int{4, 0, 0, 0, 0, 0}) | ||
pq.push([6]int{2, 0, 0, 0, 0, 0}) | ||
pq.push([6]int{6, 0, 0, 0, 0, 0}) | ||
pq.push([6]int{99, 0, 0, 0, 0, 0}) | ||
pq.push([6]int{1, 0, 0, 0, 0, 0}) | ||
|
||
cases := map[int]int{ | ||
0: 0, | ||
1: 1, | ||
2: 2, | ||
3: 4, | ||
4: 6, | ||
5: 99, | ||
} | ||
|
||
for i := 0; i <= 5; i++ { | ||
hl, _, _, _, _, _ := pq.pop() | ||
assert.Equal(t, cases[i], hl, "PQ push(), pop()") | ||
} | ||
assert.Equal(t, 0, pq.len(), "PQ len()") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
2413432311323 | ||
3215453535623 | ||
3255245654254 | ||
3446585845452 | ||
4546657867536 | ||
1438598798454 | ||
4457876987766 | ||
3637877979653 | ||
4654967986887 | ||
4564679986453 | ||
1224686865563 | ||
2546548887735 | ||
4322674655533 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
module advent-of-code | ||
|
||
go 1.23.2 | ||
|
||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters