aboutsummaryrefslogtreecommitdiff
path: root/2015/14
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
committerThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
commite7c9108b95e39d7ea5a29ae06d619c4727f11027 (patch)
tree237261eef3afd0720be77dbcbb9599fa66a24b67 /2015/14
Initial commit
Diffstat (limited to '2015/14')
-rw-r--r--2015/14/input9
-rw-r--r--2015/14/puzzle-1.bc25
-rw-r--r--2015/14/puzzle-2.bc33
-rwxr-xr-x2015/14/puzzles.sh11
4 files changed, 78 insertions, 0 deletions
diff --git a/2015/14/input b/2015/14/input
new file mode 100644
index 0000000..2af1170
--- /dev/null
+++ b/2015/14/input
@@ -0,0 +1,9 @@
+Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
+Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
+Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
+Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
+Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
+Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
+Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
+Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
+Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.
diff --git a/2015/14/puzzle-1.bc b/2015/14/puzzle-1.bc
new file mode 100644
index 0000000..d36cafd
--- /dev/null
+++ b/2015/14/puzzle-1.bc
@@ -0,0 +1,25 @@
+/* Return the largest element from array `a` of length `l` */
+define max(a[], l)
+{
+ auto m, i
+ for (i = 0; i < l; i++) {
+ if (a[i] > m)
+ m = a[i]
+ }
+ return m
+}
+
+/* Return the kilometers traveled by a raindeer that flys with speed `s` km/s for `t` seconds before
+ * needing to rest for `r` seconds.
+ */
+define calc(s, t, r)
+{
+ auto a, d
+ while (d + t <= 2503) {
+ a += s * t
+ d += t + r
+ }
+ if (d < 2503)
+ a += s * (2503 - d)
+ return a
+}
diff --git a/2015/14/puzzle-2.bc b/2015/14/puzzle-2.bc
new file mode 100644
index 0000000..4a41277
--- /dev/null
+++ b/2015/14/puzzle-2.bc
@@ -0,0 +1,33 @@
+scale = 0
+
+/* Return the largest element from array `a` of length `l` */
+define max(a[], l)
+{
+ auto m, i
+ for (i = 0; i < l; i++) {
+ if (a[i] > m)
+ m = a[i]
+ }
+ return m
+}
+
+define calc(speed[], time[], rest[], n)
+{
+ auto i, j, k, p[], d[]
+
+ for (i = 0; i < 2503; i++) {
+ for (j = k = 0; j < n; j++) {
+ if (i % (time[j] + rest[j]) < time[j])
+ d[j] += speed[j]
+ if (d[j] > k)
+ k = d[j]
+ }
+
+ for (j = 0; j < n; j++) {
+ if (d[j] == k)
+ p[j] += 1
+ }
+ }
+
+ return max(p[], n)
+}
diff --git a/2015/14/puzzles.sh b/2015/14/puzzles.sh
new file mode 100755
index 0000000..b55642d
--- /dev/null
+++ b/2015/14/puzzles.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env sh
+
+# I use bc-gh(1), but it's just Gavin Howards implementation of bc(1)
+
+sed -e 's|[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\).*|x[y++] = calc(\1, \2, \3)|' \
+ -e '$amax(x[], y)' input \
+ | bc-gh -q puzzle-1.bc
+
+sed -e 's|[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\)[^0-9]*\([0-9]*\).*|speed[n] = \1; time[n] = \2; rest[n++] = \3|' \
+ -e '$acalc(speed[], time[], rest[], n)' input \
+ | bc-gh -q puzzle-2.bc