Skip to content

Commit b5f0a88

Browse files
committed
[2024] Solution for Day 6
1 parent de94105 commit b5f0a88

File tree

3 files changed

+199
-1
lines changed

3 files changed

+199
-1
lines changed

2024/day06/main.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strings"
8+
9+
"github.com/kfarnung/advent-of-code/2024/lib"
10+
)
11+
12+
type position struct {
13+
x int
14+
y int
15+
}
16+
17+
func part1(input string) int64 {
18+
mappedArea, x, y := parseMap(input)
19+
positionMap := getVisitedPositions(mappedArea, x, y)
20+
21+
return int64(len(positionMap))
22+
}
23+
24+
func part2(input string) int64 {
25+
mappedArea, startX, startY := parseMap(input)
26+
positionMap := getVisitedPositions(mappedArea, startX, startY)
27+
28+
count := int64(0)
29+
for position := range positionMap {
30+
i, j := position.x, position.y
31+
if mappedArea[i][j] != '.' {
32+
continue
33+
}
34+
35+
mappedArea[i][j] = '#'
36+
37+
x, y := startX, startY
38+
positionMap := make(map[string]bool)
39+
direction := mappedArea[x][y]
40+
41+
for {
42+
if positionMap[fmt.Sprintf("%c,%d,%d", direction, x, y)] {
43+
count++
44+
break
45+
}
46+
47+
positionMap[fmt.Sprintf("%c,%d,%d", direction, x, y)] = true
48+
49+
nextX := x
50+
nextY := y
51+
var nextDirection rune
52+
53+
switch direction {
54+
case '>':
55+
nextY++
56+
nextDirection = 'v'
57+
case '<':
58+
nextY--
59+
nextDirection = '^'
60+
case 'v':
61+
nextX++
62+
nextDirection = '<'
63+
case '^':
64+
nextX--
65+
nextDirection = '>'
66+
default:
67+
log.Fatal("Invalid direction")
68+
}
69+
70+
if nextX < 0 || nextY < 0 || nextX >= len(mappedArea) || nextY >= len(mappedArea[nextX]) {
71+
break
72+
} else if mappedArea[nextX][nextY] == '#' {
73+
direction = nextDirection
74+
} else {
75+
x = nextX
76+
y = nextY
77+
}
78+
}
79+
80+
mappedArea[i][j] = '.'
81+
}
82+
83+
return count
84+
}
85+
86+
func getVisitedPositions(mappedArea [][]rune, x, y int) map[position]bool {
87+
direction := mappedArea[x][y]
88+
positionMap := make(map[position]bool)
89+
90+
for {
91+
positionMap[position{x, y}] = true
92+
nextX := x
93+
nextY := y
94+
var nextDirection rune
95+
96+
switch direction {
97+
case '>':
98+
nextY++
99+
nextDirection = 'v'
100+
case '<':
101+
nextY--
102+
nextDirection = '^'
103+
case 'v':
104+
nextX++
105+
nextDirection = '<'
106+
case '^':
107+
nextX--
108+
nextDirection = '>'
109+
default:
110+
log.Fatal("Invalid direction")
111+
}
112+
113+
if nextX < 0 || nextY < 0 || nextX >= len(mappedArea) || nextY >= len(mappedArea[nextX]) {
114+
break
115+
} else if mappedArea[nextX][nextY] == '#' {
116+
direction = nextDirection
117+
} else {
118+
x = nextX
119+
y = nextY
120+
}
121+
}
122+
123+
return positionMap
124+
}
125+
126+
func parseMap(input string) (mappedArea [][]rune, x, y int) {
127+
for i, line := range lib.SplitLines(input) {
128+
if line == "" {
129+
break
130+
}
131+
132+
startingPoint := strings.Index(line, "^")
133+
if startingPoint != -1 {
134+
x = i
135+
y = startingPoint
136+
}
137+
138+
mappedArea = append(mappedArea, []rune(line))
139+
}
140+
141+
return
142+
}
143+
144+
func main() {
145+
name := os.Args[1]
146+
content, err := lib.LoadFileContent(name)
147+
if err != nil {
148+
log.Fatal(err)
149+
}
150+
151+
fmt.Printf("Part 1: %d\n", part1(content))
152+
fmt.Printf("Part 2: %d\n", part2(content))
153+
}

2024/day06/main_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/kfarnung/advent-of-code/2024/lib"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var input = strings.Join([]string{
12+
"....#.....",
13+
".........#",
14+
"..........",
15+
"..#.......",
16+
".......#..",
17+
"..........",
18+
".#..^.....",
19+
"........#.",
20+
"#.........",
21+
"......#...",
22+
"",
23+
}, "\n")
24+
25+
func TestPart1(t *testing.T) {
26+
assert.Equal(t, int64(41), part1(input))
27+
28+
inputContent, err := lib.GetInputContent()
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
assert.Equal(t, int64(5409), part1(inputContent))
34+
}
35+
36+
func TestPart2(t *testing.T) {
37+
assert.Equal(t, int64(6), part2(input))
38+
39+
inputContent, err := lib.GetInputContent()
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
assert.Equal(t, int64(2022), part2(inputContent))
45+
}

private

0 commit comments

Comments
 (0)