Taddeus Kroes před 2 roky
rodič
revize
07e0ecb4c1
2 změnil soubory, kde provedl 22 přidání a 0 odebrání
  1. 20 0
      2023/06_boatrace.py
  2. 2 0
      2023/input/6

+ 20 - 0
2023/06_boatrace.py

@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+import sys
+import re
+from functools import reduce
+from itertools import starmap
+from math import ceil, sqrt
+from operator import mul
+
+def wins(time, distance):
+    # invariant: hold * (time - hold) > distance
+    # solve: -hold^2 + time*hold - distance = 0
+    #        hold = (time +- sqrt(time^2 - 4 * distance)) / 2
+    #             = (time +- d) / 2
+    # solution: (time - d) / 2 < hold < (time - d) / 2
+    d = sqrt(time ** 2 - 4 * distance)
+    return int((time + d) / 2) - ceil((time - d) / 2) + 1 - 2 * (d % 1 == 0)
+
+times, distances = (re.findall(r'\d+', line) for line in sys.stdin)
+print(reduce(mul, starmap(wins, zip(map(int, times), map(int, distances)))))
+print(wins(int(''.join(times)), int(''.join(distances))))

+ 2 - 0
2023/input/6

@@ -0,0 +1,2 @@
+Time:        63     78     94     68
+Distance:   411   1274   2047   1035