-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
56 lines (45 loc) · 1.19 KB
/
day1.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
#!/usr/bin/env python
"""
Advent of Code 2017 - Day 1 (http://adventofcode.com/2017/day/1)
"""
def is_valid(input_val):
try:
int(input_val)
if len(input_val) % 2 == 0:
return True
else:
return False
except ValueError:
return False
def solve_a(value):
z = 0
x = len(value)
for i in range(x-1):
if value[i] == value[i+1]:
z += int(value[i])
if value[0] == value[-1]:
z += int(value[0])
return z
def solve_b(value):
z = 0
x = len(value)
step = int(len(value) / 2)
for i in range(x):
target = i + step
if target > len(value)-1:
target = target - x
if value[i] == value[target]:
z += int(value[i])
return z
if __name__ == "__main__":
input_val = ''
while not is_valid(input_val):
input_val = input("Please enter valid input value: ")
puzzle = ''
while puzzle not in ['A', 'B']:
puzzle = input("Solve puzzle A, or Puzzle B: ").upper()
if puzzle == 'A':
result = solve_a(input_val)
elif puzzle == 'B':
result = solve_b(input_val)
print("Output = {output}".format(output=result))