Skip to content

Commit a1bc715

Browse files
committed
Added days 2016-08, 2016-09 and 2016-10
1 parent 5793a2b commit a1bc715

File tree

3 files changed

+320
-0
lines changed

3 files changed

+320
-0
lines changed

2016/08-Two-Factor Authentication.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, re
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """rect 3x2
8+
rotate column x=1 by 1
9+
rotate row y=0 by 4
10+
rotate column x=1 by 1""",
11+
"expected": ['6', 'N/A'],
12+
}
13+
14+
test = 'real'
15+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
16+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
17+
"expected": ['110', 'ZJHRKCPLYJ'],
18+
}
19+
20+
# -------------------------------- Control program execution -------------------------------- #
21+
22+
case_to_test = 'real'
23+
part_to_test = 2
24+
verbose_level = 1
25+
26+
# -------------------------------- Initialize some variables -------------------------------- #
27+
28+
puzzle_input = test_data[case_to_test]['input']
29+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
30+
puzzle_actual_result = 'Unknown'
31+
32+
33+
if case_to_test == 'real':
34+
width = 50
35+
height = 6
36+
else:
37+
width = 7
38+
height = 3
39+
40+
# -------------------------------- Actual code execution -------------------------------- #
41+
42+
def print_screen(screen):
43+
print('_____')
44+
for x in screen:
45+
print (''.join(x))
46+
47+
48+
screen = [['.' for x in range (width)] for y in range (height)]
49+
50+
51+
for string in puzzle_input.split('\n'):
52+
if string == '':
53+
continue
54+
if string[0:4] == 'rect':
55+
_, ab = string.split(' ')
56+
a, b = ab.split('x')
57+
a, b = int(a), int(b)
58+
59+
for y in range (b):
60+
screen[y][0:a] = ['#']*a
61+
62+
elif string[0:10] == 'rotate row':
63+
_, _, row, _, movement = string.split(' ')
64+
row_moved = int(row[2:])
65+
movement = int(movement)
66+
67+
row_moved_init = ''.join([screen[row_moved][x] for x in range (width)])
68+
row_moved_new = row_moved_init[width-movement:] + row_moved_init[:width-movement]
69+
70+
screen[row_moved] = list(row_moved_new)
71+
72+
elif string[0:10] == 'rotate col':
73+
_, _, column, _, movement = string.split(' ')
74+
column_moved = int(column[2:])
75+
movement = int(movement)
76+
77+
column_moved_init = ''.join([screen[y][column_moved] for y in range (height)])
78+
column_moved_new = column_moved_init[height-movement:] + column_moved_init[:height-movement]
79+
80+
for y in range (height):
81+
screen[y][column_moved] = column_moved_new[y]
82+
83+
if part_to_test == 1:
84+
puzzle_actual_result = sum(screen[y].count('#') for y in range (height))
85+
else:
86+
puzzle_actual_result = print_screen(screen)
87+
88+
89+
# -------------------------------- Outputs / results -------------------------------- #
90+
91+
if verbose_level >= 3:
92+
print ('Input : ' + puzzle_input)
93+
print ('Expected result : ' + str(puzzle_expected_result))
94+
print ('Actual result : ' + str(puzzle_actual_result))
95+
96+
97+
98+

2016/09-Explosives in Cyberspace.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """ADVENT
8+
A(1x5)BC
9+
(3x3)XYZ
10+
A(2x2)BCD(2x2)EFG
11+
(6x1)(1x3)A
12+
X(8x2)(3x3)ABCY""",
13+
"expected": ["""ADVENT (length 6)
14+
ABBBBBC (length 7)
15+
XYZXYZXYZ (length 9)
16+
ABCBCDEFEFG (length 11)
17+
(1x3)A (length 6)
18+
X(3x3)ABC(3x3)ABCY (length 18)""", 'Unknown'],
19+
}
20+
21+
test += 1
22+
test_data[test] = {"input": """(3x3)XYZ
23+
X(8x2)(3x3)ABCY
24+
(27x12)(20x12)(13x14)(7x10)(1x12)A
25+
(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN""",
26+
"expected": ['Unknown', """XYZXYZXYZ - length 9
27+
XABCABCABCABCABCABCY - length 20
28+
A repeated 241920
29+
445 character long
30+
"""],
31+
}
32+
'''
33+
(3x3)XYZ
34+
X(8x2)(3x3)ABCY
35+
(27x12)(20x12)(13x14)(7x10)(1x12)A
36+
'''
37+
38+
test = 'real'
39+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
40+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
41+
"expected": ['70186', '10915059201'],
42+
}
43+
44+
# -------------------------------- Control program execution -------------------------------- #
45+
46+
case_to_test = 'real'
47+
part_to_test = 2
48+
verbose_level = 1
49+
50+
# -------------------------------- Initialize some variables -------------------------------- #
51+
52+
puzzle_input = test_data[case_to_test]['input']
53+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
54+
puzzle_actual_result = 'Unknown'
55+
56+
57+
# -------------------------------- Actual code execution -------------------------------- #
58+
decompressed = ''
59+
puzzle_actual_result = ''
60+
if part_to_test == 1:
61+
for string in puzzle_input.split('\n'):
62+
decompressed = ''
63+
if string == '':
64+
continue
65+
66+
position = 0
67+
while position < len(string):
68+
if string[position] == '(':
69+
position_closing = string.find(')', position)
70+
marker_data = string[position+1:position_closing]
71+
length, repeat = marker_data.split('x')
72+
length, repeat = int(length), int(repeat)
73+
decompressed += string[position_closing+1:position_closing+1+length] * repeat
74+
75+
position = position_closing+1+length
76+
else:
77+
decompressed += string[position]
78+
position += 1
79+
80+
puzzle_actual_result += decompressed + ' - length ' + str(len(decompressed)) + '\n'
81+
82+
83+
84+
else:
85+
for string in puzzle_input.split('\n'):
86+
if string == '':
87+
continue
88+
89+
def decompress(string):
90+
print (string)
91+
total_length = 0
92+
93+
if '(' in string:
94+
start = string.find('(')
95+
end = string.find(')')
96+
length, repeat = string[start+1:end].split('x')
97+
length, repeat = int(length), int(repeat)
98+
99+
repeated_string = string[end+1:end+1+length]
100+
101+
total_length = start + decompress(repeated_string) * repeat + decompress(string[end+1+length:])
102+
else:
103+
total_length = len(string)
104+
105+
return total_length
106+
107+
108+
109+
puzzle_actual_result += 'length ' + str(decompress(string)) + '\n'
110+
111+
112+
113+
# -------------------------------- Outputs / results -------------------------------- #
114+
115+
if verbose_level >= 3:
116+
print ('Input : ' + puzzle_input)
117+
print ('Expected result : ' + str(puzzle_expected_result))
118+
print ('Actual result : ' + str(puzzle_actual_result))
119+
120+
121+
122+

2016/10-Balance Bots.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """value 5 goes to bot 2
8+
bot 2 gives low to bot 1 and high to bot 0
9+
value 3 goes to bot 1
10+
bot 1 gives low to output 1 and high to bot 0
11+
bot 0 gives low to output 2 and high to output 0
12+
value 2 goes to bot 2""",
13+
"expected": ['0', 'Unknown'],
14+
}
15+
16+
test += 1
17+
test_data[test] = {"input": """""",
18+
"expected": ['Unknown', 'Unknown'],
19+
}
20+
21+
test = 'real'
22+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
23+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
24+
"expected": ['27', '13727'],
25+
}
26+
27+
# -------------------------------- Control program execution -------------------------------- #
28+
29+
case_to_test = 'real'
30+
part_to_test = 2
31+
verbose_level = 1
32+
33+
# -------------------------------- Initialize some variables -------------------------------- #
34+
35+
puzzle_input = test_data[case_to_test]['input']
36+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
37+
puzzle_actual_result = 'Unknown'
38+
39+
40+
# -------------------------------- Actual code execution -------------------------------- #
41+
42+
bots = {bot:[0,0] for bot in range(300)}
43+
outputs = [0] * 200
44+
45+
puzzle_actual_result = ''
46+
47+
while puzzle_actual_result == '':
48+
instructions = puzzle_input.split('\n')
49+
for string in instructions:
50+
if string[0:5] == 'value':
51+
_, value, _, _, _, bot = string.split(' ')
52+
value, bot = int(value), int(bot)
53+
bots[bot][1 if bots[bot][0] else 0] = value
54+
55+
else:
56+
_, bot, _, _, _, low_botout, low_bot, _, _, _, high_botout, high_bot = string.split(' ')
57+
bot, low_bot, high_bot = int(bot), int(low_bot), int(high_bot)
58+
if bots[bot][0] and bots[bot][1]:
59+
if bots[bot][0] > bots[bot][1]:
60+
low_value, high_value = bots[bot][1], bots[bot][0]
61+
else:
62+
low_value, high_value = bots[bot][0], bots[bot][1]
63+
64+
if part_to_test == 1 and low_value == 17 and high_value == 61:
65+
puzzle_actual_result = bot
66+
break
67+
elif part_to_test == 1 and low_value == 3 and high_value == 5:
68+
puzzle_actual_result = bot
69+
break
70+
elif part_to_test == 2 and outputs[0] * outputs[1] * outputs[2] != 0:
71+
puzzle_actual_result = outputs[0] * outputs[1] * outputs[2]
72+
break
73+
74+
if low_botout == 'bot':
75+
bots[low_bot][1 if bots[low_bot][0] else 0] = low_value
76+
elif low_botout == 'output':
77+
outputs[low_bot] = low_value
78+
79+
if high_botout == 'bot':
80+
bots[high_bot][1 if bots[high_bot][0] else 0] = high_value
81+
elif high_botout == 'output':
82+
outputs[high_bot] = high_value
83+
84+
bots[bot][0], bots[bot][1] = 0, 0
85+
else:
86+
instructions.append(string)
87+
88+
89+
90+
91+
# -------------------------------- Outputs / results -------------------------------- #
92+
93+
if verbose_level >= 3:
94+
print ('Input : ' + puzzle_input)
95+
print ('Expected result : ' + str(puzzle_expected_result))
96+
print ('Actual result : ' + str(puzzle_actual_result))
97+
98+
99+
100+

0 commit comments

Comments
 (0)