-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge-04-12-2019.py
57 lines (45 loc) · 1.23 KB
/
challenge-04-12-2019.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
#!/usr/bin/env python3
debug = 1
if debug:
import time
start = time.perf_counter()
puzzle_input = range(372304, 847060)
def part1(input):
matches = []
for x in input:
double = False
increases = True
xlist = list(str(x))
for i in range(1, len(xlist)):
if (xlist[i] == xlist[i - 1]):
double = True
if (int(xlist[i]) < int(xlist[i - 1])):
increases = False
if (double and increases):
matches.append(x)
return(matches)
def part2(passwds):
nmatches = []
for x in passwds:
double = False
xlist = list(str(x))
for i in range(1, len(xlist)):
if (xlist[i] == xlist[i - 1]):
if (i + 1 < len(xlist) and i - 2 >= 0):
if (xlist[i] != xlist[i + 1] and xlist[i] != xlist[i - 2]):
double = True
elif (i + 1 == len(xlist)):
if (xlist[i] != xlist[i - 2]):
double = True
elif (i == 1):
if (xlist[i] != xlist[i + 1]):
double = True
if (double):
nmatches.append(x)
return(len(nmatches))
amatches = part1(puzzle_input)
print("PartI:", len(amatches))
print("PartII:", part2(amatches))
if debug:
end = time.perf_counter()
print("Runtime: {:5.3f}".format(end-start))