Skip to content

Commit 242430b

Browse files
committed
Improving solution 2024, day 6
1 parent 6961dff commit 242430b

File tree

3 files changed

+60
-69
lines changed

3 files changed

+60
-69
lines changed

2024/6/common.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,32 @@ import (
77

88
const Size = 130
99

10+
type Direction uint8
11+
1012
const (
11-
N uint8 = iota
13+
N Direction = iota
1214
E
1315
S
1416
W
1517
)
1618

1719
type P struct{ x, y int }
1820

19-
func parseMap() (map[P]struct{}, P, uint8) {
21+
func (p P) Next(d Direction) P {
22+
switch d {
23+
case N:
24+
return P{p.x - 1, p.y}
25+
case E:
26+
return P{p.x, p.y + 1}
27+
case S:
28+
return P{p.x + 1, p.y}
29+
case W:
30+
return P{p.x, p.y - 1}
31+
}
32+
return P{0, 0}
33+
}
34+
35+
func parseMap() (map[P]struct{}, P, Direction) {
2036
var pos P
2137
scanner := bufio.NewScanner(os.Stdin)
2238
m := map[P]struct{}{}
@@ -33,3 +49,24 @@ func parseMap() (map[P]struct{}, P, uint8) {
3349
}
3450
return m, pos, N
3551
}
52+
53+
func run(m map[P]struct{}, pos P, dir Direction) map[P]struct{} {
54+
visited := map[P]struct{}{
55+
pos: {},
56+
}
57+
58+
for {
59+
next := pos.Next(dir)
60+
61+
if next.x < 0 || next.x >= Size || next.y < 0 || next.y >= Size {
62+
return visited
63+
}
64+
65+
if _, ok := m[next]; ok {
66+
dir = (dir + 1) % 4
67+
} else {
68+
pos = next
69+
visited[pos] = struct{}{}
70+
}
71+
}
72+
}

2024/6/main1.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
11
package main
22

3-
import (
4-
"fmt"
5-
6-
"github.com/lucianoq/container/set"
7-
)
3+
import "fmt"
84

95
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-
}
6+
m, pos, dir := parseMap()
327

33-
if _, ok := m[next]; ok {
34-
dir = (dir + 1) % 4
35-
} else {
36-
p = next
37-
visited.Add(p)
38-
}
39-
}
8+
visited := run(m, pos, dir)
409

41-
fmt.Println(visited.Len())
10+
fmt.Println(len(visited))
4211
}

2024/6/main2.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
package main
22

3-
import (
4-
"fmt"
5-
6-
"github.com/lucianoq/container/set"
7-
)
3+
import "fmt"
84

95
func main() {
10-
m, p, dir := parseMap()
6+
m, pos, dir := parseMap()
7+
8+
visited := run(m, pos, dir)
119

1210
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-
}
11+
for obstacle := range visited {
12+
if testObstacle(m, obstacle, pos, dir) {
13+
count++
1814
}
1915
}
20-
2116
fmt.Println(count)
2217
}
2318

24-
type State struct {
19+
type status struct {
2520
Pos P
26-
Dir uint8
21+
Dir Direction
2722
}
2823

29-
func testObstacle(m map[P]struct{}, obstacle, pos P, dir uint8) bool {
24+
func testObstacle(m map[P]struct{}, obstacle P, pos P, dir Direction) bool {
3025

3126
// impossible if the guard is there
3227
if obstacle == pos {
@@ -43,22 +38,12 @@ func testObstacle(m map[P]struct{}, obstacle, pos P, dir uint8) bool {
4338
delete(m, obstacle)
4439
}()
4540

46-
visited := set.Set[State]{}
47-
visited.Add(State{pos, dir})
41+
visited := map[status]struct{}{
42+
status{pos, dir}: {},
43+
}
4844

4945
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-
}
46+
next := pos.Next(dir)
6247

6348
if next.x < 0 || next.x >= Size || next.y < 0 || next.y >= Size {
6449
return false
@@ -70,10 +55,10 @@ func testObstacle(m map[P]struct{}, obstacle, pos P, dir uint8) bool {
7055
pos = next
7156
}
7257

73-
newState := State{pos, dir}
74-
if visited.Contains(newState) {
58+
st := status{pos, dir}
59+
if _, ok := visited[st]; ok {
7560
return true
7661
}
77-
visited.Add(newState)
62+
visited[st] = struct{}{}
7863
}
7964
}

0 commit comments

Comments
 (0)