-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2023day3ex1.dwl
35 lines (35 loc) · 1.19 KB
/
aoc2023day3ex1.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
import takeWhile from dw::core::Arrays
import isNumeric from dw::core::Strings
var schematic = payload
splitBy '\n'
map (line) -> 0 to sizeOf(line)-1 map line[$]
var symbols = schematic
map ((line, y) -> line map { pos: { x: $$, y: y }, char: $ })
flatMap ($ filter (not isNumeric($.char)) and $.char != '.')
var possibleNumberPositions = symbols
flatMap [
{ x: $.pos.x + 0, y: $.pos.y + 1 },
{ x: $.pos.x + 0, y: $.pos.y - 1 },
{ x: $.pos.x + 1, y: $.pos.y + 0 },
{ x: $.pos.x + 1, y: $.pos.y + 1 },
{ x: $.pos.x + 1, y: $.pos.y - 1 },
{ x: $.pos.x - 1, y: $.pos.y + 0 },
{ x: $.pos.x - 1, y: $.pos.y + 1 },
{ x: $.pos.x - 1, y: $.pos.y - 1 },
]
var numberPositions = possibleNumberPositions
filter isNumeric(schematic[$.y][$.x])
var partNumbers = numberPositions
map do {
var lhs = (schematic[$.y][$.x to 0] takeWhile (isNumeric($)))[-1 to 0]
var rhs = (schematic[$.y][($.x+1) to -1] takeWhile (isNumeric($)))
---
{
row: $.y,
col: $.x - sizeOf(lhs) + 1,
part: ((lhs ++ rhs) joinBy '') as Number
}
}
distinctBy $
---
sum(partNumbers.part)