Skip to content

Instantly share code, notes, and snippets.

@emanuele6
Created December 5, 2021 17:27
Show Gist options
  • Save emanuele6/3fa9cd4d1f34e7819bdbdbe4e1832100 to your computer and use it in GitHub Desktop.
Save emanuele6/3fa9cd4d1f34e7819bdbdbe4e1832100 to your computer and use it in GitHub Desktop.
aoc day 5 2021
function hvcoo(p, f, hv) {
if (hv == "h")
return p SUBSEP f
return f SUBSEP p
}
BEGIN { FS = ",| -> " }
$1 == $3 { hv = "v"; f = $1; a = $2; b = $4 }
$2 == $4 { hv = "h"; f = $2; a = $1; b = $3 }
a > b { t = a; a = b; b = t }
hv {
while (a <= b)
tot += (points[hvcoo(a++, f, hv)]++ == 1)
hv = ""
}
END { print tot }
BEGIN { FS = ",| -> " }
{ dx = dy = 0 }
$1 != $3 { dx = 2 * ($1 < $3) - 1 }
$2 != $4 { dy = 2 * ($2 < $4) - 1 }
{ x = $1; ex = $3 + dx }
{ y = $2; ey = $4 + dy }
{
while (x != ex || y != ey) {
tot += (points[x, y]++ == 1)
x += dx; y += dy
}
}
END { print tot }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment