projects
/
summerofcode2018soln.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
9a82c8c
)
awk solution to task 1
author
Neil Smith
<neil.git@njae.me.uk>
Thu, 20 Sep 2018 14:04:16 +0000
(15:04 +0100)
committer
Neil Smith
<neil.git@njae.me.uk>
Thu, 20 Sep 2018 14:04:16 +0000
(15:04 +0100)
src/task1/task1.awk
[new file with mode: 0755]
patch
|
blob
diff --git a/src/task1/task1.awk
b/src/task1/task1.awk
new file mode 100755
(executable)
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)
+}