@@ -17,21 +17,24 @@ type problem struct {
17
17
}
18
18
19
19
const (
20
- WALL = '#'
21
- BOX = 'O'
22
- ROBOT = '@'
23
- GROUND = '.'
20
+ Wall = '#'
21
+ Box = 'O'
22
+ Robot = '@'
23
+ Ground = '.'
24
+ LeftBox = '['
25
+ RightBox = ']'
24
26
)
25
27
26
28
func parse (
27
29
input string ,
30
+ makeWide bool ,
28
31
) (problem , error ) {
29
32
whRaw , mRaw , err := splitInput (input )
30
33
if err != nil {
31
34
return problem {}, err
32
35
}
33
36
34
- wh , r , err := parseWarehouse (whRaw )
37
+ wh , r , err := parseWarehouse (whRaw , makeWide )
35
38
36
39
m , err := parseMoves (mRaw )
37
40
if err != nil {
@@ -57,26 +60,51 @@ func splitInput(
57
60
58
61
func parseWarehouse (
59
62
wh string ,
63
+ makeWide bool ,
60
64
) (warehouseMatrix , util.Coordinate , error ) {
61
65
lines := strings .Split (wh , "\n " )
62
66
out := make (warehouseMatrix , len (lines ))
63
67
robot := util.Coordinate {}
64
68
foundRobot := false
65
69
66
70
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
+ }
72
77
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
+ }
75
105
}
76
- foundRobot = true
77
- robot = util.Coordinate {X : x , Y : y }
78
- out [y ][x ] = GROUND
79
106
}
107
+ out [y ] = row
80
108
}
81
109
82
110
if ! foundRobot {
@@ -96,27 +124,18 @@ func parseMoves(
96
124
continue
97
125
}
98
126
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 )
102
138
}
103
- ms = append (ms , newMove )
104
139
}
105
140
return ms , nil
106
141
}
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