Day 06. part 2

This commit is contained in:
Borna Rajković 2024-12-08 13:25:17 +01:00
parent 4d8dd29a27
commit a0d56e0f14
2 changed files with 71 additions and 15 deletions

View File

@ -16,25 +16,27 @@ const (
const ( const (
VISITED = 'X' VISITED = 'X'
OCCUPIED = '#' OCCUPIED = '#'
FREE = '.'
) )
var directions = map[rune]Direction{ var toDirections = map[rune]Direction{
'^': UP, '^': UP,
'>': RIGHT, '>': RIGHT,
'v': DOWN, 'v': DOWN,
'<': LEFT, '<': LEFT,
} }
var fromDirections = map[Direction]rune{
UP: '^',
RIGHT: '>',
DOWN: 'v',
LEFT: '<',
}
// task:https://adventofcode.com/2024/day/5 // task:https://adventofcode.com/2024/day/5
// short summary - count visited steps // short summary - count visited steps
func SolveBasic(input string) int { func SolveBasic(input string) int {
grid := loadGrid(input) grid := visitationGrid(input)
x, y, dir := findPlayer(grid)
for inGrid(grid, x, y) {
grid[y][x] = VISITED
x, y, dir = nextSpot(grid, x, y, dir)
}
sum := 0 sum := 0
for _, row := range grid { for _, row := range grid {
@ -47,6 +49,55 @@ func SolveBasic(input string) int {
return sum 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) { func nextSpot(grid [][]rune, x int, y int, dir Direction) (int, int, Direction) {
for { for {
nx, ny := calculateNext(x, y, dir) 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) { func findPlayer(grid [][]rune) (int, int, Direction) {
for y, row := range grid { for y, row := range grid {
for x, value := range row { for x, value := range row {
if dir, present := directions[value]; present { if dir, present := toDirections[value]; present {
return x, y, dir return x, y, dir
} }
} }
@ -98,8 +149,13 @@ func loadGrid(input string) [][]rune {
return grid return grid
} }
// task:https://adventofcode.com/2024/day/5#part2 func visitationGrid(input string) [][]rune {
// short summary - fix and count invalid columns grid := loadGrid(input)
func SolveComplex(input string) int { x, y, dir := findPlayer(grid)
return 0
for inGrid(grid, x, y) {
grid[y][x] = VISITED
x, y, dir = nextSpot(grid, x, y, dir)
}
return grid
} }

View File

@ -18,7 +18,7 @@ func TestBasicSolutionExample(t *testing.T) {
func TestComplexSolutionExample(t *testing.T) { func TestComplexSolutionExample(t *testing.T) {
result := day_06.SolveComplex(ExampleData) result := day_06.SolveComplex(ExampleData)
if result != 123 { if result != 6 {
t.Fatalf("Expected 123 received %d", result) t.Fatalf("Expected 6 received %d", result)
} }
} }