aoc2021/d05/d05_2.awk

24 lines
515 B
Awk
Raw Normal View History

2021-12-08 00:12:56 +00:00
BEGIN { FS = ",| -> " }
function min(a, b) { return a < b ? a : b }
function max(a, b) { return a > b ? a : b }
function abs(a) { return a < 0 ? -a : a }
function sign(a) { return a < 0 ? -1 : 1 }
$1 == $3 {
for (i = min($2, $4); i <= max($2, $4); i++)
n+= (++floor[$1, i] == 2)
next
}
$2 == $4 {
for (i = min($1, $3); i <= max($1, $3); i++)
n+= (++floor[i, $2] == 2)
next
}
{
i = sign($3-$1)
j = sign($4-$2)
for (a = 0; a <= abs($3-$1); a++)
n+= (++floor[$1+i*a, $2+a*j] == 2)
next
}
END { print n }