-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
65 lines (49 loc) · 1.48 KB
/
day20.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
62
63
64
65
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_range_from_str(s, sep="-"):
left, mid, right = s.partition(sep)
assert mid == sep
return int(left), int(right)
def get_ip_range_from_file(file_path=top_dir + "resources/year2016_day20_input.txt"):
with open(file_path) as f:
return [get_range_from_str(l.strip()) for l in f]
def lowest_ip(ip_range):
ip_range = sorted(ip_range)
last_allowed = 0
for beg, end in ip_range:
if beg > last_allowed:
break
last_allowed = max(end + 1, last_allowed)
return last_allowed
def nb_ip(ip_range, maxi):
ip_range = sorted(ip_range)
nb_ip = 0
range_beg = 0
for beg, end in ip_range:
if beg > range_beg:
nb_ip += beg - range_beg
range_beg = max(end + 1, range_beg)
if maxi > range_beg:
nb_ip += maxi - range_beg
return nb_ip
def run_tests():
ip_range = [
"5-8",
"0-2",
"4-7",
]
ip_range = [get_range_from_str(s) for s in ip_range]
assert lowest_ip(ip_range) == 3
assert nb_ip(ip_range, 10) == 2
def get_solutions():
ip_range = get_ip_range_from_file()
print(lowest_ip(ip_range) == 19449262)
print(nb_ip(ip_range, 4294967295) == 119)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)