-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.dwl
44 lines (40 loc) · 1.63 KB
/
day08.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::Arrays
import * from dw::core::Strings
var parsed = lines(payload) map (line, idx) -> (line splitBy "" map $ as Number)
var parsedTransposed = parsed map (line, x) -> parsed map $[x]
fun part1(in: Array<Array<Number>>): Array<Array<Number>> =
in map (line, x) ->
(line map (item, y) -> do{
var lr = line splitAt y
var ud = parsedTransposed[y] splitAt x
---
if(
isEmpty(lr.l) or (not (lr.l some $ >= item)) or
isEmpty(lr.r drop 1) or (not ((lr.r drop 1) some $ >= item)) or
isEmpty(ud.l) or (not (ud.l some $ >= item)) or
isEmpty(ud.r drop 1) or (not ((ud.r drop 1) some $ >= item)))
1 else 0
})
fun part2(in: Array<Array<Number>>): Array<Array<Number>> =
in map (line, x) ->
(line map (item, y) -> do{
var lr = line splitAt y
var ud = parsedTransposed[y] splitAt x
var sl = sizeOf((lr.l[-1 to 0] default []) takeWhile $ < item)
var sr = sizeOf((lr.r drop 1) takeWhile $ < item)
var su = sizeOf((ud.l[-1 to 0] default []) takeWhile $ < item)
var sd = sizeOf((ud.r drop 1) takeWhile $ < item)
---
(if(sl == sizeOf(lr.l)) sl else sl + 1) *
(if(sr == sizeOf(lr.r) - 1) sr else sr + 1) *
(if(su == sizeOf(ud.l)) su else su + 1) *
(if(sd == sizeOf(ud.r) - 1) sd else sd + 1)
})
---
//Solution is quite inefficient. In the playground you can only execute either part1 or part2 but not both or else you get a timeout.
{
part1: part1(parsed) sumBy sum($),
part2: max(part2(parsed) flatMap $)
}