-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.py
53 lines (38 loc) · 1.25 KB
/
day6.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
import unittest
import re
def parseInput(input):
numbers = [int(s) for s in re.findall(r'\d+', input)]
count = round(len(numbers) / 2)
return [(numbers[i], numbers[i + count]) for i in range(0, count)]
def solveRace(time, dist):
first = next(h for h in range(1, time) if h * (time - h) > dist)
last = next(h for h in reversed(range(1, time)) if h * (time - h) > dist)
return last - first + 1
def parse1(input):
data = parseInput(input)
result = 1
for time, dist in data:
result *= solveRace(time, dist)
return result
def mergeNumbers(data):
time = ''
distance = ''
for (t, d) in data:
time += str(t)
distance += str(d)
return (int(time), int(distance))
def parse2(input):
time, dist = mergeNumbers(parseInput(input))
return solveRace(time, dist)
class Test(unittest.TestCase):
def test_part1(self):
self.assertEqual(parse1("""Time: 7 15 30
Distance: 9 40 200"""), 288)
def test_part2(self):
self.assertEqual(parse2("""Time: 7 15 30
Distance: 9 40 200"""), 71503)
unittest.main(exit=False)
with open('input6.txt') as file:
data = file.read()
print("Part 1:", parse1(data))
print("Part 2:", parse2(data))