Skip to content

Commit d68ad2b

Browse files
committed
Added day 2016-20
1 parent 3dd1091 commit d68ad2b

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

2016/20-Firewall Rules.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """5-8
8+
0-2
9+
4-7""",
10+
"expected": ['Unknown', 'Unknown'],
11+
}
12+
13+
test += 1
14+
test_data[test] = {"input": """""",
15+
"expected": ['Unknown', 'Unknown'],
16+
}
17+
18+
test = 'real'
19+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
20+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
21+
"expected": ['32259706', '113'],
22+
}
23+
24+
# -------------------------------- Control program execution -------------------------------- #
25+
26+
case_to_test = 'real'
27+
part_to_test = 2
28+
verbose_level = 1
29+
30+
# -------------------------------- Initialize some variables -------------------------------- #
31+
32+
puzzle_input = test_data[case_to_test]['input']
33+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
34+
puzzle_actual_result = 'Unknown'
35+
36+
37+
# -------------------------------- Actual code execution -------------------------------- #
38+
39+
max_IP = 4294967295
40+
41+
puzzle_actual_result = 0
42+
reset_max_blocked = False
43+
44+
blocked = []
45+
for string in puzzle_input.split('\n'):
46+
a, b = string.split('-')
47+
a, b = int(a), int(b)
48+
blocked.append((a, b))
49+
50+
blocked.sort()
51+
max_blocked = blocked[0][1]
52+
53+
for block in blocked:
54+
print (block, max_blocked, 'start')
55+
if max_blocked + 1 >= block[0]:
56+
max_blocked = max(max_blocked, block[1])
57+
else:
58+
if part_to_test == 1:
59+
puzzle_actual_result = max_blocked + 1
60+
break
61+
else:
62+
puzzle_actual_result += block[0] - max_blocked - 1
63+
print ('Reset', puzzle_actual_result)
64+
max_blocked = block[1]
65+
reset_max_blocked = True
66+
print (block, max_blocked, 'end')
67+
68+
69+
print (reset_max_blocked, max_blocked)
70+
71+
if part_to_test == 2:
72+
if reset_max_blocked:
73+
max_blocked = max([block[1] for block in blocked])
74+
if max_blocked != max_IP:
75+
puzzle_actual_result += max_IP - max_blocked - 1
76+
# 544541374 too high
77+
# 544541246 too high
78+
79+
# -------------------------------- Outputs / results -------------------------------- #
80+
81+
if verbose_level >= 3:
82+
print ('Input : ' + puzzle_input)
83+
print ('Expected result : ' + str(puzzle_expected_result))
84+
print ('Actual result : ' + str(puzzle_actual_result))
85+
86+
87+
88+

0 commit comments

Comments
 (0)