aboutsummaryrefslogtreecommitdiff
path: root/2024/02/puzzles.awk
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-12-02 13:39:38 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-12-02 13:39:38 +0100
commit5f8556807a6adae632f3646727a2bb1b16b68614 (patch)
treecf3722366c826c2fdbedebcdf71ef6957393099b /2024/02/puzzles.awk
parent425c822826314a31f26cee06b9ac2520351c8829 (diff)
Add solutions for 2024 day 2
Diffstat (limited to '2024/02/puzzles.awk')
-rw-r--r--2024/02/puzzles.awk49
1 files changed, 49 insertions, 0 deletions
diff --git a/2024/02/puzzles.awk b/2024/02/puzzles.awk
new file mode 100644
index 0000000..789a193
--- /dev/null
+++ b/2024/02/puzzles.awk
@@ -0,0 +1,49 @@
+#!/usr/bin/gawk -f
+
+function sign(x)
+{
+ return x < 0 ? -1 : +1
+}
+
+function abs(x)
+{
+ return x < 0 ? -x : x
+}
+
+function check(skip, xs, s, i, j, d, D)
+{
+ for (i = 1; i <= NF; i++) {
+ if (i != skip)
+ xs[++j] = $i
+ }
+ s = sign(xs[1] - xs[2])
+ for (i = 2; i <= length(xs); i++) {
+ d = xs[i - 1] - xs[i]
+ D = abs(d)
+ if (sign(d) != s || D < 1 || D > 3)
+ return 0
+ }
+ return 1
+}
+
+# START PART 1
+{
+ s = sign($1 - $2)
+ n += check()
+}
+# END PART 1 START PART 2
+{
+ s = sign($1 - $2)
+ if (!check()) {
+ for (i = 1; i <= NF; i++) {
+ if (check(i))
+ break
+ }
+ if (i > NF)
+ next
+ }
+ n++
+}
+# END PART 2
+
+END { print n } \ No newline at end of file