File tree Expand file tree Collapse file tree 4 files changed +60
-1
lines changed Expand file tree Collapse file tree 4 files changed +60
-1
lines changed Original file line number Diff line number Diff line change
1
+ cqjxxyzz
2
+ cqkaabcc
Original file line number Diff line number Diff line change
1
+ cqjxjnds
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env python3
2
+
3
+ def load_data (filename ):
4
+ with open (filename , 'r' ) as f :
5
+ for line in f :
6
+ line = line .rstrip ('\n ' )
7
+ yield line
8
+
9
+ password = next (load_data ('input.txt' ))
10
+
11
+ # Part One
12
+
13
+ def next_valid_after (password ):
14
+ def next_after (password , i = - 1 ):
15
+ a = chr (ord (password [i ]) + 1 )
16
+ carry = False
17
+ if a in 'iol' :
18
+ a = chr (ord (a ) + 1 )
19
+ elif a == chr (ord ('z' ) + 1 ):
20
+ a = 'a'
21
+ carry = True
22
+ password = password [:i ] + a + password [len (password )+ i + 1 :len (password )]
23
+ if carry :
24
+ return next_after (password , i - 1 )
25
+ return password
26
+ def valid (password ):
27
+ if 'i' in password or 'o' in password or 'l' in password :
28
+ return False
29
+ pairs = []
30
+ straight3 = False
31
+ for i in range (1 , len (password )):
32
+ if password [i ] == password [i - 1 ] and len (pairs ) < 2 :
33
+ if not pairs :
34
+ pairs .append (i - 1 )
35
+ elif password [pairs [0 ]] != password [i ]:
36
+ pairs .append (i - 1 )
37
+ elif ord (password [i ]) == ord (password [i - 1 ]) + 1 :
38
+ if i > 1 and ord (password [i - 1 ]) == ord (password [i - 2 ]) + 1 :
39
+ straight3 = True
40
+ return len (pairs ) == 2 and straight3
41
+ while password != 'z' * len (password ):
42
+ password = next_after (password )
43
+ if valid (password ):
44
+ return password
45
+ raise Exception ("Not found" )
46
+
47
+ result = next_valid_after (password )
48
+
49
+ print (result )
50
+
51
+ # Part Two
52
+
53
+ result = next_valid_after (result )
54
+
55
+ print (result )
56
+
Original file line number Diff line number Diff line change 1
1
```
2
2
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
3
- 2015 ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
3
+ 2015 ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- +
4
4
2016 ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
5
5
2017 ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
6
6
2018 ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
You can’t perform that action at this time.
0 commit comments