Skip to content

Commit 6b93886

Browse files
committed
Added days 2017-01 to 2017-04
1 parent d1afc54 commit 6b93886

File tree

5 files changed

+766
-0
lines changed

5 files changed

+766
-0
lines changed

2017/01-Inverse Captcha.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """1212""",
8+
"expected": ['3', 'Unknown'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
18+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
19+
"expected": ['Unknown', 'Unknown'],
20+
}
21+
22+
# -------------------------------- Control program execution -------------------------------- #
23+
24+
case_to_test = 'real'
25+
part_to_test = 2
26+
verbose_level = 1
27+
28+
# -------------------------------- Initialize some variables -------------------------------- #
29+
30+
puzzle_input = test_data[case_to_test]['input']
31+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
32+
puzzle_actual_result = 'Unknown'
33+
34+
35+
# -------------------------------- Actual code execution -------------------------------- #
36+
37+
captcha = 0
38+
if part_to_test == 1:
39+
puzzle_input += puzzle_input[0]
40+
for i in range (len(puzzle_input)-1):
41+
if puzzle_input[i] == puzzle_input[i+1]:
42+
captcha += int(puzzle_input[i])
43+
44+
puzzle_actual_result = captcha
45+
46+
47+
else:
48+
for i in range (len(puzzle_input)-1):
49+
if puzzle_input[i] == puzzle_input[(i+len(puzzle_input)//2)%len(puzzle_input)]:
50+
captcha += int(puzzle_input[i])
51+
52+
puzzle_actual_result = captcha
53+
54+
55+
56+
# -------------------------------- Outputs / results -------------------------------- #
57+
58+
if verbose_level >= 3:
59+
print ('Input : ' + puzzle_input)
60+
print ('Expected result : ' + str(puzzle_expected_result))
61+
print ('Actual result : ' + str(puzzle_actual_result))
62+
63+
64+
65+

2017/02-Corruption Checksum.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, itertools
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """5 1 9 5
8+
7 5 3
9+
2 4 6 8""",
10+
"expected": ['Unknown', 'Unknown'],
11+
}
12+
13+
test += 1
14+
test_data[test] = {"input": """5 9 2 8
15+
9 4 7 3
16+
3 8 6 5""",
17+
"expected": ['Unknown', 'Unknown'],
18+
}
19+
20+
test = 'real'
21+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
22+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
23+
"expected": ['Unknown', 'Unknown'],
24+
}
25+
26+
# -------------------------------- Control program execution -------------------------------- #
27+
28+
case_to_test = 'real'
29+
part_to_test = 2
30+
verbose_level = 1
31+
32+
# -------------------------------- Initialize some variables -------------------------------- #
33+
34+
puzzle_input = test_data[case_to_test]['input']
35+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
36+
puzzle_actual_result = 'Unknown'
37+
38+
39+
# -------------------------------- Actual code execution -------------------------------- #
40+
41+
checksum = 0
42+
puzzle_input = puzzle_input.replace('\t', ' ')
43+
if part_to_test == 1:
44+
for string in puzzle_input.split('\n'):
45+
digits = list(map(int, string.split(' ')))
46+
checksum += max (digits)
47+
checksum -= min (digits)
48+
puzzle_actual_result = checksum
49+
50+
else:
51+
for string in puzzle_input.split('\n'):
52+
digits = list(map(int, string.split(' ')))
53+
for val in itertools.permutations(digits, 2):
54+
if val[1] % val[0] == 0:
55+
checksum += val[1] // val[0]
56+
break
57+
puzzle_actual_result = checksum
58+
59+
60+
61+
# -------------------------------- Outputs / results -------------------------------- #
62+
63+
if verbose_level >= 3:
64+
print ('Input : ' + puzzle_input)
65+
print ('Expected result : ' + str(puzzle_expected_result))
66+
print ('Actual result : ' + str(puzzle_actual_result))
67+
68+
69+
70+

2017/03-Spiral Memory.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, math
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": 17,
8+
"expected": ['Unknown', 'Unknown'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
test_data[test] = {"input": 312051,
18+
"expected": ['430', '312453'],
19+
}
20+
21+
# -------------------------------- Control program execution -------------------------------- #
22+
23+
case_to_test = 'real'
24+
part_to_test = 2
25+
verbose_level = 1
26+
27+
# -------------------------------- Initialize some variables -------------------------------- #
28+
29+
puzzle_input = test_data[case_to_test]['input']
30+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
31+
puzzle_actual_result = 'Unknown'
32+
33+
34+
# -------------------------------- Actual code execution -------------------------------- #
35+
36+
if part_to_test == 1:
37+
square_size = int(math.sqrt(puzzle_input))
38+
if square_size % 2 == 0:
39+
square_size += 1
40+
else:
41+
square_size += 2
42+
43+
44+
distance_from_square = (square_size ** 2 - puzzle_input) % (square_size-1)
45+
46+
if distance_from_square <= square_size // 2:
47+
distance_from_square = square_size // 2 - distance_from_square
48+
else:
49+
distance_from_square -= square_size // 2
50+
51+
puzzle_actual_result = (square_size - 1) // 2 + distance_from_square
52+
53+
54+
55+
else:
56+
vals = {}
57+
direction = (1, 0)
58+
current = (1,0)
59+
vals[(0,0)] = 1
60+
61+
max_square = 1000
62+
63+
corner_SE = {x**2+1: (0, -1) for x in range(1, max_square) if x % 2 == 1}
64+
corner_SW = {x**2 - (x-1): (1, 0) for x in range(1, max_square) if x % 2 == 1}
65+
corner_NW = {x**2 - (x-1)*2: (0, 1) for x in range(2, max_square) if x % 2 == 1}
66+
corner_NE = {x**2 - (x-1)*3: (-1, 0) for x in range(2, max_square) if x % 2 == 1}
67+
corners = corner_SE.copy()
68+
corners.update(corner_SW)
69+
corners.update(corner_NW)
70+
corners.update(corner_NE)
71+
72+
for i in range (2, max_square):
73+
value = 0
74+
75+
for neighbor in [(x, y) for x in (-1, 0, 1) for y in (-1, 0, 1) if not((x, y) == (0,0))]:
76+
x, y = (current[0] + neighbor[0], current[1] + neighbor[1])
77+
if (x, y) in vals:
78+
value += vals[(x, y)]
79+
80+
vals[current] = value
81+
82+
# In which direction are we going?
83+
if i in corners:
84+
direction = corners[i]
85+
86+
current = (current[0] + direction[0], current[1] + direction[1])
87+
88+
if value > puzzle_input:
89+
puzzle_actual_result = value
90+
break
91+
92+
93+
94+
# -------------------------------- Outputs / results -------------------------------- #
95+
96+
if verbose_level >= 3:
97+
print ('Input : ' + puzzle_input)
98+
print ('Expected result : ' + str(puzzle_expected_result))
99+
print ('Actual result : ' + str(puzzle_actual_result))
100+
101+
102+
103+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, itertools
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """aa bb cc dd aaa""",
8+
"expected": ['Unknown', 'Unknown'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """abcde fghij""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
18+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
19+
"expected": ['455', '186'],
20+
}
21+
22+
# -------------------------------- Control program execution -------------------------------- #
23+
24+
case_to_test = 'real'
25+
part_to_test = 2
26+
verbose_level = 1
27+
28+
# -------------------------------- Initialize some variables -------------------------------- #
29+
30+
puzzle_input = test_data[case_to_test]['input']
31+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
32+
puzzle_actual_result = 'Unknown'
33+
34+
35+
# -------------------------------- Actual code execution -------------------------------- #
36+
37+
if part_to_test == 1:
38+
valid = 0
39+
for string in puzzle_input.split('\n'):
40+
vals = string.split(' ')
41+
duplicates = [vals.count(a) for a in vals if vals.count(a) != 1]
42+
if not duplicates:
43+
valid += 1
44+
puzzle_actual_result = valid
45+
46+
47+
else:
48+
valid = 0
49+
for string in puzzle_input.split('\n'):
50+
vals = string.split(' ')
51+
duplicates = [vals.count(a) for a in vals if vals.count(a) != 1]
52+
53+
for val in vals:
54+
anagram = [vals.count(''.join(permut)) for x in vals for permut in itertools.permutations(x) if x != ''.join(permut)]
55+
56+
if not duplicates and not any(anagram):
57+
valid += 1
58+
puzzle_actual_result = valid
59+
60+
61+
62+
# -------------------------------- Outputs / results -------------------------------- #
63+
64+
if verbose_level >= 3:
65+
print ('Input : ' + puzzle_input)
66+
print ('Expected result : ' + str(puzzle_expected_result))
67+
print ('Actual result : ' + str(puzzle_actual_result))
68+
69+
70+
71+

0 commit comments

Comments
 (0)