-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.dwl
44 lines (41 loc) · 1.49 KB
/
day05.dwl
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
%dw 2.0
output application/json
import * from dw::core::Strings
import * from dw::core::Arrays
type State = Array<Array<String>>
var initialState: State = payload splitBy "\n\n"
then lines($[0])[0 to -2]
map ($ splitBy "" divideBy 4 map $[1])
reduce ((item, acc = null) -> //convert columns to rows (transpose)
if (acc == null) item map [$]
else acc map $ + item[$$])
map ($ filter $ != " ")
type Move = {| amount: Number, from: Number, to: Number |}
var moves: Array<Move> = payload splitBy "\n\n"
then lines($[1])
map words($)
map {
amount: $[1] as Number,
from: $[3] as Number - 1,
to: $[5] as Number - 1
}
fun simulate(state: State, move: Move, reverse = false): State = do {
var moving = state[move.from] take move.amount then
if (reverse) $[-1 to 0]
else $
---
state update {
case [move.from] -> $ drop move.amount
case [move.to] -> moving ++ $
}
// or with map
//state map
// if ($$ == move.from) $ drop move.amount
// else if ($$ == move.to) moving ++ $
// else $
}
---
{
part1: moves reduce ((move, state = initialState) -> simulate(state, move, true)) map $[0] joinBy "",
part2: moves reduce ((move, state = initialState) -> simulate(state, move)) map $[0] joinBy "",
}