Skip to content

Commit 67a0ecb

Browse files
committed
2024, day 6
1 parent 0bf263e commit 67a0ecb

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

2024/6/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/6/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go
6+
7+
main2:
8+
go build -o main2 main2.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2024/6/common.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
const Size = 130
9+
10+
const (
11+
N uint8 = iota
12+
E
13+
S
14+
W
15+
)
16+
17+
type P struct{ x, y int }
18+
19+
func parseMap() (map[P]any, P, uint8) {
20+
var pos P
21+
scanner := bufio.NewScanner(os.Stdin)
22+
m := map[P]any{}
23+
for i := 0; scanner.Scan(); i++ {
24+
line := scanner.Text()
25+
for j, c := range line {
26+
switch c {
27+
case '#':
28+
m[P{i, j}] = struct{}{}
29+
case '^':
30+
pos = P{i, j}
31+
}
32+
}
33+
}
34+
return m, pos, N
35+
}

2024/6/main1.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/lucianoq/container/set"
7+
)
8+
9+
func main() {
10+
m, p, dir := parseMap()
11+
12+
visited := set.Set[P]{}
13+
visited.Add(p)
14+
15+
for {
16+
var next P
17+
18+
switch dir {
19+
case N:
20+
next = P{p.x - 1, p.y}
21+
case E:
22+
next = P{p.x, p.y + 1}
23+
case S:
24+
next = P{p.x + 1, p.y}
25+
case W:
26+
next = P{p.x, p.y - 1}
27+
}
28+
29+
if next.x < 0 || next.x >= Size || next.y < 0 || next.y >= Size {
30+
break
31+
}
32+
33+
if _, ok := m[next]; ok {
34+
dir = (dir + 1) % 4
35+
} else {
36+
p = next
37+
visited.Add(p)
38+
}
39+
}
40+
41+
fmt.Println(visited.Len())
42+
}

2024/6/main2.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/lucianoq/container/set"
7+
)
8+
9+
func main() {
10+
m, p, dir := parseMap()
11+
12+
var count int
13+
for i := 0; i < Size; i++ {
14+
for j := 0; j < Size; j++ {
15+
if testObstacle(m, P{i, j}, p, dir) {
16+
count++
17+
}
18+
}
19+
}
20+
21+
fmt.Println(count)
22+
}
23+
24+
type State struct {
25+
Pos P
26+
Dir uint8
27+
}
28+
29+
func testObstacle(m map[P]any, obstacle, pos P, dir uint8) bool {
30+
31+
// impossible if the guard is there
32+
if obstacle == pos {
33+
return false
34+
}
35+
36+
// impossible if there is already an obstacle there
37+
if _, ok := m[obstacle]; ok {
38+
return false
39+
}
40+
41+
m[obstacle] = struct{}{}
42+
defer func() {
43+
delete(m, obstacle)
44+
}()
45+
46+
visited := set.Set[State]{}
47+
visited.Add(State{pos, dir})
48+
49+
for {
50+
var next P
51+
52+
switch dir {
53+
case N:
54+
next = P{pos.x - 1, pos.y}
55+
case E:
56+
next = P{pos.x, pos.y + 1}
57+
case S:
58+
next = P{pos.x + 1, pos.y}
59+
case W:
60+
next = P{pos.x, pos.y - 1}
61+
}
62+
63+
if next.x < 0 || next.x >= Size || next.y < 0 || next.y >= Size {
64+
return false
65+
}
66+
67+
if _, ok := m[next]; ok {
68+
dir = (dir + 1) % 4
69+
} else {
70+
pos = next
71+
}
72+
73+
newState := State{pos, dir}
74+
if visited.Contains(newState) {
75+
return true
76+
}
77+
visited.Add(newState)
78+
}
79+
}

0 commit comments

Comments
 (0)