Skip to content

Commit b9ee4d4

Browse files
committed
Clean up
1 parent 6f2ce7d commit b9ee4d4

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

src/aoc/year2024/day15.gleam

+13-41
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,15 @@ fn push(
8181
case next_v {
8282
"." -> Ok(grid)
8383
"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)
10084
"[" | "]" if dir == Left || dir == Right ->
101-
push2(grid, next_c, next_v, v, dir)
85+
push(grid, next_c, next_v, v, dir)
10286
"[" -> {
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)
10589
}
10690
"]" -> {
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)
10993
}
11094
_ -> Error(Nil)
11195
}
@@ -120,8 +104,8 @@ fn expand(c: String) -> List(String) {
120104
}
121105
}
122106

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)
125109
let robot = find_robot(grid)
126110
let #(grid, _) =
127111
list.fold(dirs, #(grid, robot), fn(acc, dir) {
@@ -134,27 +118,15 @@ pub fn part1(input: String) -> Int {
134118
result.unwrap(res, acc)
135119
})
136120
grid
137-
|> dict.filter(fn(_k, v) { v == "O" })
121+
|> dict.filter(fn(_k, v) { v == block })
138122
|> dict.keys
139123
|> list.fold(0, fn(s, k) { s + k.0 + 100 * k.1 })
140124
}
141125

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+
}
155129

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)
160132
}

0 commit comments

Comments
 (0)