Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 9aa7b41

Browse files
committed
Update parser for day 15 for part 2
1 parent 6a8ce93 commit 9aa7b41

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed

solutions/day15/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
func part1(
1414
input string,
1515
) string {
16-
p, err := parse(input)
16+
p, err := parse(input, false)
1717
if err != nil {
1818
return fmt.Sprintf("Failed to parse input: %v", err)
1919
}
@@ -27,8 +27,8 @@ func part1(
2727
newRobot := move(p.robot)
2828
if !newRobot.Equals(endPosition) {
2929
// End position is more than one step, meaning we need to move boxes
30-
p.warehouse[newRobot.Y][newRobot.X] = GROUND
31-
p.warehouse[endPosition.Y][endPosition.X] = BOX
30+
p.warehouse[newRobot.Y][newRobot.X] = Ground
31+
p.warehouse[endPosition.Y][endPosition.X] = Box
3232
}
3333
p.robot = newRobot
3434
}
@@ -44,11 +44,11 @@ func findEndPosition(
4444
np := d(s)
4545
ch := w[np.Y][np.X]
4646
switch ch {
47-
case GROUND:
47+
case Ground:
4848
return np, nil
49-
case BOX:
49+
case Box:
5050
return findEndPosition(w, np, d)
51-
case WALL:
51+
case Wall:
5252
return np, fmt.Errorf("wall hit")
5353
}
5454

@@ -61,7 +61,7 @@ func score(
6161
sum := 0
6262
for y, row := range wh {
6363
for x, ch := range row {
64-
if ch != BOX {
64+
if ch != Box {
6565
continue
6666
}
6767
sum += x + 100*y

solutions/day15/parse.go

+54-35
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ type problem struct {
1717
}
1818

1919
const (
20-
WALL = '#'
21-
BOX = 'O'
22-
ROBOT = '@'
23-
GROUND = '.'
20+
Wall = '#'
21+
Box = 'O'
22+
Robot = '@'
23+
Ground = '.'
24+
LeftBox = '['
25+
RightBox = ']'
2426
)
2527

2628
func parse(
2729
input string,
30+
makeWide bool,
2831
) (problem, error) {
2932
whRaw, mRaw, err := splitInput(input)
3033
if err != nil {
3134
return problem{}, err
3235
}
3336

34-
wh, r, err := parseWarehouse(whRaw)
37+
wh, r, err := parseWarehouse(whRaw, makeWide)
3538

3639
m, err := parseMoves(mRaw)
3740
if err != nil {
@@ -57,26 +60,51 @@ func splitInput(
5760

5861
func parseWarehouse(
5962
wh string,
63+
makeWide bool,
6064
) (warehouseMatrix, util.Coordinate, error) {
6165
lines := strings.Split(wh, "\n")
6266
out := make(warehouseMatrix, len(lines))
6367
robot := util.Coordinate{}
6468
foundRobot := false
6569

6670
for y, line := range lines {
67-
out[y] = []int32(line)
68-
for x, ch := range line {
69-
if ch != WALL && ch != ROBOT && ch != BOX && ch != GROUND {
70-
return out, robot, fmt.Errorf("invalid character in warehouse matrix: %c", ch)
71-
}
71+
var row []int32
72+
if makeWide {
73+
row = make([]int32, 0, len(line)*2)
74+
} else {
75+
row = make([]int32, 0, len(line))
76+
}
7277

73-
if ch != ROBOT {
74-
continue
78+
for x, ch := range line {
79+
switch ch {
80+
case Wall:
81+
if makeWide {
82+
row = append(row, Wall)
83+
}
84+
row = append(row, Wall)
85+
case Box:
86+
if makeWide {
87+
row = append(row, LeftBox, RightBox)
88+
} else {
89+
row = append(row, Box)
90+
}
91+
case Ground:
92+
if makeWide {
93+
row = append(row, Ground)
94+
}
95+
row = append(row, Ground)
96+
case Robot:
97+
foundRobot = true
98+
if makeWide {
99+
row = append(row, Ground, Ground)
100+
robot = util.Coordinate{X: 2 * x, Y: y}
101+
} else {
102+
row = append(row, Ground)
103+
robot = util.Coordinate{X: x, Y: y}
104+
}
75105
}
76-
foundRobot = true
77-
robot = util.Coordinate{X: x, Y: y}
78-
out[y][x] = GROUND
79106
}
107+
out[y] = row
80108
}
81109

82110
if !foundRobot {
@@ -96,27 +124,18 @@ func parseMoves(
96124
continue
97125
}
98126

99-
newMove, err := charToDirection(ch)
100-
if err != nil {
101-
return ms, err
127+
switch ch {
128+
case '^':
129+
ms = append(ms, util.Coordinate.North)
130+
case 'v':
131+
ms = append(ms, util.Coordinate.South)
132+
case '>':
133+
ms = append(ms, util.Coordinate.East)
134+
case '<':
135+
ms = append(ms, util.Coordinate.West)
136+
default:
137+
return ms, fmt.Errorf("invalid direction '%c'", ch)
102138
}
103-
ms = append(ms, newMove)
104139
}
105140
return ms, nil
106141
}
107-
108-
func charToDirection(
109-
ch int32,
110-
) (util.Direction, error) {
111-
switch ch {
112-
case '^':
113-
return util.Coordinate.North, nil
114-
case 'v':
115-
return util.Coordinate.South, nil
116-
case '>':
117-
return util.Coordinate.East, nil
118-
case '<':
119-
return util.Coordinate.West, nil
120-
}
121-
return nil, fmt.Errorf("invalid direction '%c'", ch)
122-
}

0 commit comments

Comments
 (0)