aboutsummaryrefslogtreecommitdiff
path: root/2024/07/puzzles.awk
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-12-07 13:28:12 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-12-07 13:28:12 +0100
commitdb863f2beb82a5da9ec6d4fabda498756422ad7a (patch)
tree7e1e5480aca984423b65d7ac9dbb6e98519bf18e /2024/07/puzzles.awk
parent124f57d99286e2b9a0f7818be7742540dbded5b7 (diff)
Add 2024 day 7 solutions
Diffstat (limited to '2024/07/puzzles.awk')
-rw-r--r--2024/07/puzzles.awk19
1 files changed, 19 insertions, 0 deletions
diff --git a/2024/07/puzzles.awk b/2024/07/puzzles.awk
new file mode 100644
index 0000000..75564e8
--- /dev/null
+++ b/2024/07/puzzles.awk
@@ -0,0 +1,19 @@
+#!/usr/bin/gawk -f
+
+function eval(i, acc)
+{
+ if (acc > $1)
+ return 0
+ if (i == NF + 1)
+ return acc == $1
+# START PART 1
+ return eval(i + 1, acc * $i) || eval(i + 1, acc + $i)
+# END PART 1 START PART 2
+ return eval(i + 1, (acc $i) + 0) || eval(i + 1, acc * $i) ||
+ eval(i + 1, acc + $i)
+# END PART 2
+}
+
+BEGIN { FS = ":? " }
+eval(3, $2) { n += $1 }
+END { print n } \ No newline at end of file