diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-05 09:47:59 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-05 09:47:59 +0100 |
commit | 73f7b696264d9db21a6e7c03b89eb2b1135af83b (patch) | |
tree | 8817329ef6ee76928ec1ebd463cfbbdfe8384a28 /2024 | |
parent | 1e9979b8dd0cb4c7535f500e5d35a4f11c6bf859 (diff) |
Merge solutions into one
Diffstat (limited to '2024')
-rw-r--r-- | 2024/05/.gitignore | 1 | ||||
-rw-r--r-- | 2024/05/Makefile | 1 | ||||
-rw-r--r-- | 2024/05/puzzles.awk | 36 |
3 files changed, 38 insertions, 0 deletions
diff --git a/2024/05/.gitignore b/2024/05/.gitignore new file mode 100644 index 0000000..82eaf49 --- /dev/null +++ b/2024/05/.gitignore @@ -0,0 +1 @@ +./puzzle-[12].awk
\ No newline at end of file diff --git a/2024/05/Makefile b/2024/05/Makefile new file mode 100644 index 0000000..e7d8b6c --- /dev/null +++ b/2024/05/Makefile @@ -0,0 +1 @@ +include ../../Makefiles/awk.mk
\ No newline at end of file diff --git a/2024/05/puzzles.awk b/2024/05/puzzles.awk new file mode 100644 index 0000000..6dbfcd2 --- /dev/null +++ b/2024/05/puzzles.awk @@ -0,0 +1,36 @@ +#!/usr/bin/gawk -f + +function record_valid( seen) +{ + for (i = 1; i <= NF; i++) { + for (j in seen) { + if (j in rules[$i]) + return 0 + } + seen[$i] = 1 + } + return 1 +} + +function sort_page_numbers(i1, v1, i2, v2) +{ + if (v1 in rules[v2]) + return +1 + if (v2 in rules[v1]) + return -1 + return 0 +} + +BEGIN { FS = "[|,]" } +/\|/ { rules[$1][$2] = 1 } +END { print mid } + +# START PART 1 +/,/ && record_valid() { mid += $(i / 2) } +# END PART 1 START PART 2 +/,/ && !record_valid() { + split($0, xs) + asort(xs, xs, "sort_page_numbers") + mid += xs[(NF + 1) / 2] +} +# END PART 2
\ No newline at end of file |