From: Neil Smith Date: Thu, 20 Sep 2018 14:04:16 +0000 (+0100) Subject: awk solution to task 1 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=62ffc4273743b4e3727982f17a1abfb130edd0f1;p=summerofcode2018soln.git awk solution to task 1 --- diff --git a/src/task1/task1.awk b/src/task1/task1.awk new file mode 100755 index 0000000..adc2d86 --- /dev/null +++ b/src/task1/task1.awk @@ -0,0 +1,26 @@ +#!/usr/bin/awk -f + +BEGIN { + x = 0 + y = 0 + d = 90 +} + +function forward (distance) { + if (d == 0) y += distance + if (d == 90) x += distance + if (d == 180) y -= distance + if (d == 270) x -= distance +} + +function abs(v) { + return v < 0 ? -v : v + } + +/^C/ {d = (d + 90) % 360} +/^A/ {d = (d + 270) % 360} +/^F[0-9]+/ {forward(substr($0, 2))} + +END { + print abs(x) + abs(y) +}