diff --git a/day_06/solution.go b/day_06/solution.go index 571a00e..af695dd 100644 --- a/day_06/solution.go +++ b/day_06/solution.go @@ -16,25 +16,27 @@ const ( const ( VISITED = 'X' OCCUPIED = '#' + FREE = '.' ) -var directions = map[rune]Direction{ +var toDirections = map[rune]Direction{ '^': UP, '>': RIGHT, 'v': DOWN, '<': LEFT, } +var fromDirections = map[Direction]rune{ + UP: '^', + RIGHT: '>', + DOWN: 'v', + LEFT: '<', +} + // task:https://adventofcode.com/2024/day/5 // short summary - count visited steps func SolveBasic(input string) int { - grid := loadGrid(input) - x, y, dir := findPlayer(grid) - - for inGrid(grid, x, y) { - grid[y][x] = VISITED - x, y, dir = nextSpot(grid, x, y, dir) - } + grid := visitationGrid(input) sum := 0 for _, row := range grid { @@ -47,6 +49,55 @@ func SolveBasic(input string) int { return sum } +// task:https://adventofcode.com/2024/day/5#part2 +// short summary - find all position that will create an infinite loop +func SolveComplex(input string) int { + grid := visitationGrid(input) + + dirGrid := loadGrid(input) + startX, startY, startDir := findPlayer(dirGrid) + dirGrid[startY][startX] = FREE + + positions := 0 + + for y, row := range grid { + for x, value := range row { + if value == VISITED && dirGrid[y][x] == FREE { + dirGrid[y][x] = OCCUPIED + + curX := startX + curY := startY + curDir := startDir + + for inGrid(dirGrid, curX, curY) { + if dirGrid[curY][curX] == fromDirections[curDir] { + // we visited a same point second time in the same direction + // which means we are in an infinite list + positions++ + break + } + dirGrid[curY][curX] = fromDirections[curDir] + curX, curY, curDir = nextSpot(dirGrid, curX, curY, curDir) + + } + dirGrid[y][x] = FREE + clearDirGrid(dirGrid) + } + } + } + return positions +} + +func clearDirGrid(grid [][]rune) { + for y, row := range grid { + for x, value := range row { + if value != OCCUPIED { + grid[y][x] = FREE + } + } + } +} + func nextSpot(grid [][]rune, x int, y int, dir Direction) (int, int, Direction) { for { nx, ny := calculateNext(x, y, dir) @@ -81,7 +132,7 @@ func inGrid(grid [][]rune, x int, y int) bool { func findPlayer(grid [][]rune) (int, int, Direction) { for y, row := range grid { for x, value := range row { - if dir, present := directions[value]; present { + if dir, present := toDirections[value]; present { return x, y, dir } } @@ -98,8 +149,13 @@ func loadGrid(input string) [][]rune { return grid } -// task:https://adventofcode.com/2024/day/5#part2 -// short summary - fix and count invalid columns -func SolveComplex(input string) int { - return 0 +func visitationGrid(input string) [][]rune { + grid := loadGrid(input) + x, y, dir := findPlayer(grid) + + for inGrid(grid, x, y) { + grid[y][x] = VISITED + x, y, dir = nextSpot(grid, x, y, dir) + } + return grid } diff --git a/day_06/solution_test.go b/day_06/solution_test.go index 943dfc1..6d5aa10 100644 --- a/day_06/solution_test.go +++ b/day_06/solution_test.go @@ -18,7 +18,7 @@ func TestBasicSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) { result := day_06.SolveComplex(ExampleData) - if result != 123 { - t.Fatalf("Expected 123 received %d", result) + if result != 6 { + t.Fatalf("Expected 6 received %d", result) } }