@@ -81,31 +81,15 @@ fn push(
81
81
case next_v {
82
82
"." -> Ok ( grid )
83
83
"O" -> push ( grid , next_c , next_v , v , dir )
84
- _ -> Error ( Nil )
85
- }
86
- }
87
-
88
- fn push2 (
89
- grid : Grid ,
90
- coord : Coord ,
91
- v : String ,
92
- rep : String ,
93
- dir : Dir ,
94
- ) -> Result ( Grid , Nil ) {
95
- let grid = dict . insert ( grid , coord , rep )
96
- use # ( next_c , next_v ) <- result . try ( next ( grid , coord , dir ) )
97
- let grid = dict . insert ( grid , next_c , v )
98
- case next_v {
99
- "." -> Ok ( grid )
100
84
"[" | "]" if dir == Left || dir == Right ->
101
- push2 ( grid , next_c , next_v , v , dir )
85
+ push ( grid , next_c , next_v , v , dir )
102
86
"[" -> {
103
- use grid <- result . try ( push2 ( grid , next_c , next_v , v , dir ) )
104
- push2 ( grid , # ( next_c . 0 + 1 , next_c . 1 ) , "]" , "." , dir )
87
+ use grid <- result . try ( push ( grid , next_c , next_v , v , dir ) )
88
+ push ( grid , # ( next_c . 0 + 1 , next_c . 1 ) , "]" , "." , dir )
105
89
}
106
90
"]" -> {
107
- use grid <- result . try ( push2 ( grid , next_c , next_v , v , dir ) )
108
- push2 ( grid , # ( next_c . 0 - 1 , next_c . 1 ) , "[" , "." , dir )
91
+ use grid <- result . try ( push ( grid , next_c , next_v , v , dir ) )
92
+ push ( grid , # ( next_c . 0 - 1 , next_c . 1 ) , "[" , "." , dir )
109
93
}
110
94
_ -> Error ( Nil )
111
95
}
@@ -120,8 +104,8 @@ fn expand(c: String) -> List(String) {
120
104
}
121
105
}
122
106
123
- pub fn part1 ( input : String ) -> Int {
124
- let # ( grid , dirs ) = parse ( input , fn ( c ) { [ c ] } )
107
+ pub fn solve ( input : String , block : String , f : fn ( String ) -> List ( String ) ) -> Int {
108
+ let # ( grid , dirs ) = parse ( input , f )
125
109
let robot = find_robot ( grid )
126
110
let # ( grid , _ ) =
127
111
list . fold ( dirs , # ( grid , robot ) , fn ( acc , dir ) {
@@ -134,27 +118,15 @@ pub fn part1(input: String) -> Int {
134
118
result . unwrap ( res , acc )
135
119
} )
136
120
grid
137
- |> dict . filter ( fn ( _k , v ) { v == "O" } )
121
+ |> dict . filter ( fn ( _k , v ) { v == block } )
138
122
|> dict . keys
139
123
|> list . fold ( 0 , fn ( s , k ) { s + k . 0 + 100 * k . 1 } )
140
124
}
141
125
142
- pub fn part2 ( input : String ) -> Int {
143
- let # ( grid , dirs ) = parse ( input , expand )
144
- let robot = find_robot ( grid )
145
- let # ( grid , _ ) =
146
- list . fold ( dirs , # ( grid , robot ) , fn ( acc , dir ) {
147
- let # ( grid , robot ) = acc
148
- let res = {
149
- use next_g <- result . try ( push2 ( grid , robot , "@" , "." , dir ) )
150
- use # ( next_r , _ ) <- result . map ( next ( grid , robot , dir ) )
151
- # ( next_g , next_r )
152
- }
153
- result . unwrap ( res , acc )
154
- } )
126
+ pub fn part1 ( input : String ) -> Int {
127
+ solve ( input , "O" , fn ( c ) { [ c ] } )
128
+ }
155
129
156
- grid
157
- |> dict . filter ( fn ( _k , v ) { v == "[" } )
158
- |> dict . keys
159
- |> list . fold ( 0 , fn ( s , k ) { s + k . 0 + 100 * k . 1 } )
130
+ pub fn part2 ( input : String ) -> Int {
131
+ solve ( input , "[" , expand )
160
132
}
0 commit comments