diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-18 21:59:32 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-18 21:59:32 +0100 |
commit | 42e03dfbde7ec58c4d16cc671c3c2271b8d6d4c6 (patch) | |
tree | de8b0240cc359494fff516327009727849378a85 /2019/06 | |
parent | 35e57f6e59423272fd4a45b2535c369cb61182b5 (diff) |
Add 2019 day 6 solutions
Diffstat (limited to '2019/06')
-rwxr-xr-x | 2019/06/puzzle-1.awk | 21 | ||||
-rwxr-xr-x | 2019/06/puzzle-2.awk | 32 |
2 files changed, 53 insertions, 0 deletions
diff --git a/2019/06/puzzle-1.awk b/2019/06/puzzle-1.awk new file mode 100755 index 0000000..e694b4d --- /dev/null +++ b/2019/06/puzzle-1.awk @@ -0,0 +1,21 @@ +#!/usr/bin/awk -f + +function norbits(o) +{ + if (o in memo) + return memo[o] + return memo[o] = 1 + norbits(orbits[o]) +} + +BEGIN { + FS = ")" + memo["COM"] = 0 +} + +{ orbits[$2] = $1 } + +END { + for (o in orbits) + n += norbits(o) + print n +}
\ No newline at end of file diff --git a/2019/06/puzzle-2.awk b/2019/06/puzzle-2.awk new file mode 100755 index 0000000..3d0e42c --- /dev/null +++ b/2019/06/puzzle-2.awk @@ -0,0 +1,32 @@ +#!/usr/bin/awk -f + +function trace_to_com(xs, o) +{ + if (o == "COM") + return + o = orbits[o] + xs[length(xs) + 1] = o + trace_to_com(xs, o) +} + +BEGIN { FS = ")" } + +{ orbits[$2] = $1 } + +END { + # Declare ‘xs’ and ‘ys’ as arrays + xs[0]; delete xs + ys[0]; delete ys + + trace_to_com(xs, "YOU") + trace_to_com(ys, "SAN") + + for (i = 1; i <= length(xs); i++) { + for (j = 1; j <= length(ys); j++) { + if (xs[i] == ys[j]) { + print i + j - 2 + exit + } + } + } +}
\ No newline at end of file |