-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6v2.py
executable file
·61 lines (47 loc) · 1.47 KB
/
6v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import lib
import sys, math, re, functools, operator, itertools, bisect
import numpy as np
from collections import defaultdict, deque, Counter
# 14:05
infile = sys.argv[1] if len(sys.argv) > 1 else '6.in'
f = open(infile, 'r') if infile != '-' else sys.stdin
# data = f.read().strip()
lines = list(map(str.strip, f))
# grid = list(list(ln) for ln in map(str.strip, f))
'''
Time: 40 70 98 79
Distance: 215 1051 2147 1005
---
ms, mm
speed = 1mm/ms held
dist = (time - held) * held
= time*held - held^2
held^2 - time*held + dist = 0
charge to move
can only charge at the beginning
find # ways we can win each race
then return product of these # ways
'''
times = lib.get_ints(lines[0])
record_dists = lib.get_ints(lines[1])
def ways_to_win(time, record_dist):
# ax^2 + bx + c = 0
# held^2 - time*held + dist = 0
d = math.sqrt(time*time - 4*record_dist)
if d == 0:
# only meet record dist one time, can't pass it.
return 0
else:
# first time we pass record dist
start = int((time - d) / 2 + 1)
# second time we pass record dist
end = math.ceil((time + d) / 2)
return end - start
ways = map(ways_to_win, times, record_dists)
part1 = functools.reduce(operator.mul, ways)
print('part 1: ', part1)
times = int(''.join(map(str, times)))
record_dists = int(''.join(map(str, record_dists)))
part2 = ways_to_win(times, record_dists)
print('part 2: ', part2)