Skip to content

Commit

Permalink
[2024] Solution for Day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarnung committed Dec 6, 2024
1 parent de94105 commit b5f0a88
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 1 deletion.
153 changes: 153 additions & 0 deletions 2024/day06/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package main

import (
"fmt"
"log"
"os"
"strings"

"github.com/kfarnung/advent-of-code/2024/lib"
)

type position struct {
x int
y int
}

func part1(input string) int64 {
mappedArea, x, y := parseMap(input)
positionMap := getVisitedPositions(mappedArea, x, y)

return int64(len(positionMap))
}

func part2(input string) int64 {
mappedArea, startX, startY := parseMap(input)
positionMap := getVisitedPositions(mappedArea, startX, startY)

count := int64(0)
for position := range positionMap {
i, j := position.x, position.y
if mappedArea[i][j] != '.' {
continue
}

mappedArea[i][j] = '#'

x, y := startX, startY
positionMap := make(map[string]bool)
direction := mappedArea[x][y]

for {
if positionMap[fmt.Sprintf("%c,%d,%d", direction, x, y)] {
count++
break
}

positionMap[fmt.Sprintf("%c,%d,%d", direction, x, y)] = true

nextX := x
nextY := y
var nextDirection rune

switch direction {
case '>':
nextY++
nextDirection = 'v'
case '<':
nextY--
nextDirection = '^'
case 'v':
nextX++
nextDirection = '<'
case '^':
nextX--
nextDirection = '>'
default:
log.Fatal("Invalid direction")
}

if nextX < 0 || nextY < 0 || nextX >= len(mappedArea) || nextY >= len(mappedArea[nextX]) {
break
} else if mappedArea[nextX][nextY] == '#' {
direction = nextDirection
} else {
x = nextX
y = nextY
}
}

mappedArea[i][j] = '.'
}

return count
}

func getVisitedPositions(mappedArea [][]rune, x, y int) map[position]bool {
direction := mappedArea[x][y]
positionMap := make(map[position]bool)

for {
positionMap[position{x, y}] = true
nextX := x
nextY := y
var nextDirection rune

switch direction {
case '>':
nextY++
nextDirection = 'v'
case '<':
nextY--
nextDirection = '^'
case 'v':
nextX++
nextDirection = '<'
case '^':
nextX--
nextDirection = '>'
default:
log.Fatal("Invalid direction")
}

if nextX < 0 || nextY < 0 || nextX >= len(mappedArea) || nextY >= len(mappedArea[nextX]) {
break
} else if mappedArea[nextX][nextY] == '#' {
direction = nextDirection
} else {
x = nextX
y = nextY
}
}

return positionMap
}

func parseMap(input string) (mappedArea [][]rune, x, y int) {
for i, line := range lib.SplitLines(input) {
if line == "" {
break
}

startingPoint := strings.Index(line, "^")
if startingPoint != -1 {
x = i
y = startingPoint
}

mappedArea = append(mappedArea, []rune(line))
}

return
}

func main() {
name := os.Args[1]
content, err := lib.LoadFileContent(name)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Part 1: %d\n", part1(content))
fmt.Printf("Part 2: %d\n", part2(content))
}
45 changes: 45 additions & 0 deletions 2024/day06/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"strings"
"testing"

"github.com/kfarnung/advent-of-code/2024/lib"
"github.com/stretchr/testify/assert"
)

var input = strings.Join([]string{
"....#.....",
".........#",
"..........",
"..#.......",
".......#..",
"..........",
".#..^.....",
"........#.",
"#.........",
"......#...",
"",
}, "\n")

func TestPart1(t *testing.T) {
assert.Equal(t, int64(41), part1(input))

inputContent, err := lib.GetInputContent()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, int64(5409), part1(inputContent))
}

func TestPart2(t *testing.T) {
assert.Equal(t, int64(6), part2(input))

inputContent, err := lib.GetInputContent()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, int64(2022), part2(inputContent))
}
2 changes: 1 addition & 1 deletion private

0 comments on commit b5f0a88

Please sign in to comment.