-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15_1.go
98 lines (87 loc) · 2.02 KB
/
day15_1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package day15
import (
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
type DefineTile int
const (
Wall DefineTile = iota
Empty
Robot
Box
None
)
type Tile struct {
Kind DefineTile
Row int
Col int
}
func findBoxes(from Tile, themap [][]Tile, mov DefinedDir) []Tile {
result := make([]Tile, 0)
r, c := directionOfMov(mov)
cantake := true
emptyfound := false
current := themap[from.Row][from.Col]
for cantake {
current = themap[current.Row+r][current.Col+c]
switch current.Kind {
case Empty:
result = append(result, current)
emptyfound = true
cantake = false
case Box:
result = append(result, current)
case Wall:
cantake = false
}
}
if emptyfound {
utilities.ReverseArray(result)
return result
} else {
return []Tile{}
}
}
func move(robot Tile, themap *[][]Tile, movs []DefinedDir) {
if len(movs) > 0 {
mov := movs[0]
r, c := directionOfMov(mov)
nextPos := (*themap)[robot.Row+r][robot.Col+c]
switch nextPos.Kind {
case Wall:
move(robot, themap, movs[1:])
case Empty:
(*themap)[robot.Row][robot.Col].Kind = Empty
(*themap)[nextPos.Row][nextPos.Col].Kind = Robot
robot.Row, robot.Col = nextPos.Row, nextPos.Col
move(robot, themap, movs[1:])
default:
tilesToMov := findBoxes(robot, *themap, mov)
if len(tilesToMov) > 0 {
for tIdx := 0; tIdx < len(tilesToMov)-1; tIdx++ {
t := tilesToMov[tIdx]
(*themap)[t.Row][t.Col].Kind = Box
}
(*themap)[robot.Row][robot.Col].Kind = Empty
(*themap)[nextPos.Row][nextPos.Col].Kind = Robot
robot.Row, robot.Col = nextPos.Row, nextPos.Col
move(robot, themap, movs[1:])
} else {
move(robot, themap, movs[1:])
}
}
}
}
func Executepart1() int {
var result int = 0
var fileName string = "./day15/day15.txt"
if fileContent, err := utilities.ReadFileAsText(fileName); err == nil {
robotinit, themap, movements := parseContent(fileContent)
move(robotinit, &themap, movements)
for _, v := range themap {
for _, t := range v {
result += calculateGPS(t)
}
}
}
return result
}