From 093de3f52b0b7dfb7cc42e05c9c4267c7011c001 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 2 Dec 2022 16:54:18 +0100 Subject: Add 2016 day 1 solutions --- 2016/01/puzzles.awk | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2016/01/puzzles.awk (limited to '2016/01/puzzles.awk') diff --git a/2016/01/puzzles.awk b/2016/01/puzzles.awk new file mode 100644 index 0000000..c20a12a --- /dev/null +++ b/2016/01/puzzles.awk @@ -0,0 +1,27 @@ +#!/usr/bin/gawk -f + +function abs(n) { return n < 0 ? -n : n } + +function move(n) { + # START PART 1 + d == 0 ? y += n : \ + d == 1 ? x += n : \ + d == 2 ? y -= n : \ + x -= n + # END PART 1 START PART 2 + for (i = 1; i <= n; i++) { + d == 0 ? y++ : \ + d == 1 ? x++ : \ + d == 2 ? y-- : \ + x-- + if (map[x, y]++) + exit + } + # END PART 2 +} + +BEGIN { RS = ", "; FPAT = "[LR]|[0-9]+" } +/L/ { if (--d == -1) d = 3 } +/R/ { if (++d == +4) d = 0 } + { move($2) } +END { print abs(x) + abs(y) } -- cgit v1.2.3