Skip to content

Commit

Permalink
day 16 wip again
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Chessa committed Dec 17, 2024
1 parent 0477362 commit a75d84c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 33 deletions.
89 changes: 76 additions & 13 deletions solutions/alessio/day16/day16.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"aoc16/graph"
"fmt"
"math"
"os"
"strings"
"time"
Expand Down Expand Up @@ -133,11 +134,13 @@ func getGraph(grid []string, rows int, cols int) (*graph.Graph[Pos], Pos, Pos) {
return &g, start, end
}

func solve1(grid []string) {
func solve1(grid []string) int {
rows, cols := len(grid), len(grid[0])
g, start, end := getGraph(grid, rows, cols)
// printMazeWithNodes(grid, rows, cols, g)
fmt.Println(g.ShortestPath(start, end, dirs[0]))
min := g.ShortestPath(start, end, dirs[0])
fmt.Println(min)
return min
}

func abs(x int) int {
Expand All @@ -162,16 +165,76 @@ func getDir(from Pos, to Pos) Pos {
}
}

func solve2(grid []string) {
type PathPos struct {
pos Pos
dir Pos
edges []PathEdge
score int
}

type PathEdge struct {
from Pos
to Pos
}

func BFS(graph *graph.Graph[Pos], start Pos, end Pos) []PathEdge {
queue := []PathPos{{start, dirs[0], []PathEdge{}, 0}}
visited := map[PosDir]bool{}
minScore := math.MaxInt
bestPathsEdges := []PathEdge{}
distinct := 0

for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]

if curr.pos == end {
// do something when path is found
if curr.score < minScore {
distinct = 1
minScore = curr.score
bestPathsEdges = curr.edges
} else if curr.score == minScore {
distinct++
bestPathsEdges = append(bestPathsEdges, curr.edges...)
}
continue
}

if visited[PosDir{curr.pos, curr.dir}] {
continue
}

for _, e := range graph.AdjList[curr.pos] {
next := e.To.Val
dir := getDir(curr.pos, next)
if !visited[PosDir{next, dir}] {
weight := e.Weight
if dir != curr.dir {
weight += 1000
}
if curr.score+weight <= minScore {
queue = append(queue, PathPos{next, dir, append(curr.edges, PathEdge{curr.pos, next}), curr.score + weight})
}
}
}
}

fmt.Printf("found min %d with %d distinct paths\n", minScore, distinct)

return bestPathsEdges
}

func solve2(grid []string, minFound int) {
rows, cols := len(grid), len(grid[0])
g, start, end := getGraph(grid, rows, cols)
edges := g.GetFullPathsEdges(start, end, dirs[0])
edges := BFS(g, start, end)

tiles := map[Pos]bool{}
for _, e := range edges {
dir := getDir(e.From, e.To.Val)
curr := Pos{e.From.r, e.From.c}
for curr.r != e.To.Val.r || curr.c != e.To.Val.c {
dir := getDir(e.from, e.to)
curr := Pos{e.from.r, e.from.c}
for curr.r != e.to.r || curr.c != e.to.c {
tiles[curr] = true
curr.r += dir.r
curr.c += dir.c
Expand All @@ -193,12 +256,12 @@ func solve2(grid []string) {
fmt.Println(len(tiles))
}

func part1(grid []string) {
solve1(grid)
func part1(grid []string) int {
return solve1(grid)
}

func part2(grid []string) {
solve2(grid)
func part2(grid []string, minFound int) {
solve2(grid, minFound)
}

func main() {
Expand All @@ -208,9 +271,9 @@ func main() {
lines := strings.Split(strings.Trim(dataStr, "\n"), "\n")

start := time.Now()
part1(lines)
min := part1(lines)
fmt.Printf("part1: %s\n", time.Since(start))
start = time.Now()
part2(lines)
part2(lines, min)
fmt.Printf("part2: %s\n", time.Since(start))
}
9 changes: 6 additions & 3 deletions solutions/alessio/day16/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ func (graph *Graph[T]) getClosest(nodes []T, dist map[T]int, visited map[T]bool)
return minNode
}

func (graph *Graph[T]) GetFullPathsEdges(start T, end T, dir T) []Edge[T] {
func (graph *Graph[T]) GetFullPathsEdges(start T, end T, dir T, minFound *int) []Edge[T] {
allEdges := []Edge[T]{}
min := math.MaxInt
graph.getBestPathsRec(start, dir, &map[T]bool{}, []Edge[T]{}, &allEdges, end, 0, &min)
// oldMin := minFound
graph.getBestPathsRec(start, dir, &map[T]bool{}, []Edge[T]{}, &allEdges, end, 0, minFound)
// if minFound != oldMin {
// fmt.Println("min changed from", oldMin, "to", minFound, ". Should not happen.")
// }
return allEdges
}

Expand Down
32 changes: 15 additions & 17 deletions solutions/alessio/day16/input16.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################
###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############

0 comments on commit a75d84c

Please sign in to comment.