summaryrefslogtreecommitdiff
path: root/14
diff options
context:
space:
mode:
authorPrefetch2022-12-31 22:21:39 +0100
committerPrefetch2022-12-31 22:21:39 +0100
commit68615a9ad2c942254135cffb00cf25a84a3b1356 (patch)
tree1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /14
Initial commit
Diffstat (limited to '14')
-rw-r--r--14/input.txt9
-rwxr-xr-x14/main.py75
-rwxr-xr-x14/test.py21
3 files changed, 105 insertions, 0 deletions
diff --git a/14/input.txt b/14/input.txt
new file mode 100644
index 0000000..6cf5489
--- /dev/null
+++ b/14/input.txt
@@ -0,0 +1,9 @@
+Dancer can fly 27 km/s for 5 seconds, but then must rest for 132 seconds.
+Cupid can fly 22 km/s for 2 seconds, but then must rest for 41 seconds.
+Rudolph can fly 11 km/s for 5 seconds, but then must rest for 48 seconds.
+Donner can fly 28 km/s for 5 seconds, but then must rest for 134 seconds.
+Dasher can fly 4 km/s for 16 seconds, but then must rest for 55 seconds.
+Blitzen can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
+Prancer can fly 3 km/s for 21 seconds, but then must rest for 40 seconds.
+Comet can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.
+Vixen can fly 18 km/s for 5 seconds, but then must rest for 84 seconds.
diff --git a/14/main.py b/14/main.py
new file mode 100755
index 0000000..4af4940
--- /dev/null
+++ b/14/main.py
@@ -0,0 +1,75 @@
+#!/usr/bin/python
+
+
+
+class Reindeer:
+ pass
+
+
+
+def parse_input(lines):
+ result = []
+ for line in lines:
+ words = line.split()
+
+ r = Reindeer()
+ r.name = words[0]
+ r.dist = 0
+ r.score = 0
+ r.state = "moving"
+ r.speed = int(words[ 3])
+ r.tmove = int(words[ 6])
+ r.trest = int(words[13])
+ r.timer = 0
+
+ result.append(r)
+
+ return result
+
+
+
+def solve_partn(partn, lines, tmax):
+ contestants = parse_input(lines)
+
+ for t in range(tmax):
+ for r in contestants:
+ if r.state == "moving":
+ r.dist += r.speed
+ r.timer += 1
+ if r.timer >= r.tmove:
+ r.state = "resting"
+ r.timer = 0
+ else: # r.state == "resting"
+ r.timer += 1
+ if r.timer >= r.trest:
+ r.state = "moving"
+ r.timer = 0
+
+ # Award 1 point to leader (and to others in case of a tie)
+ # Only for part 2, but doesn't hurt part 1, so not disabled.
+ leader = max(contestants, key = lambda r: r.dist)
+ for r in contestants:
+ if r.dist == leader.dist:
+ r.score += 1
+
+ if partn == 1:
+ winner = max(contestants, key = lambda r: r.dist)
+ return winner.dist
+ else: # partn == 2
+ winner = max(contestants, key = lambda r: r.score)
+ return winner.score
+
+
+
+def main():
+ # Read reindeer stats from input text file
+ with open("input.txt", "r") as f:
+ lines = f.read().splitlines()
+
+ print("Part 1 solution:", solve_partn(1, lines, 2503)) # 2640 for me
+ print("Part 2 solution:", solve_partn(2, lines, 2503)) # 1102 for me
+
+
+
+if __name__ == "__main__":
+ main()
diff --git a/14/test.py b/14/test.py
new file mode 100755
index 0000000..25012d4
--- /dev/null
+++ b/14/test.py
@@ -0,0 +1,21 @@
+#!/usr/bin/python
+
+import unittest
+
+import main
+
+
+
+class Examples(unittest.TestCase):
+ def test_example1(self):
+ lines = [
+ "Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
+ "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."
+ ]
+ self.assertEqual(main.solve_partn(1, lines, 1000), 1120)
+ self.assertEqual(main.solve_partn(2, lines, 1000), 689)
+
+
+
+if __name__ == "__main__":
+ unittest.main()