Working on 2015/11
This commit is contained in:
parent
032678593b
commit
c049c52d46
49
2015/11/11.md
Normal file
49
2015/11/11.md
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
## \-\-- Day 11: Corporate Policy \-\--
|
||||||
|
|
||||||
|
Santa\'s previous password expired, and he needs help choosing a new
|
||||||
|
one.
|
||||||
|
|
||||||
|
To help him remember his new password after the old one expires, Santa
|
||||||
|
has devised a method of coming up with a password based on the previous
|
||||||
|
one. Corporate policy dictates that passwords must be exactly eight
|
||||||
|
lowercase letters (for security reasons), so he finds his new password
|
||||||
|
by *incrementing* his old password string repeatedly until it is valid.
|
||||||
|
|
||||||
|
Incrementing is just like counting with numbers: `xx`, `xy`, `xz`, `ya`,
|
||||||
|
`yb`, and so on. Increase the rightmost letter one step; if it was `z`,
|
||||||
|
it wraps around to `a`, and repeat with the next letter to the left
|
||||||
|
until one doesn\'t wrap around.
|
||||||
|
|
||||||
|
Unfortunately for Santa, a new Security-Elf recently started, and he has
|
||||||
|
imposed some additional password requirements:
|
||||||
|
|
||||||
|
- Passwords must include one increasing straight of at least three
|
||||||
|
letters, like `abc`, `bcd`, `cde`, and so on, up to `xyz`. They
|
||||||
|
cannot skip letters; `abd` doesn\'t count.
|
||||||
|
- Passwords may not contain the letters `i`, `o`, or `l`, as these
|
||||||
|
letters can be mistaken for other characters and are therefore
|
||||||
|
confusing.
|
||||||
|
- Passwords must contain at least two different, non-overlapping pairs
|
||||||
|
of letters, like `aa`, `bb`, or `zz`.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
- `hijklmmn` meets the first requirement (because it contains the
|
||||||
|
straight `hij`) but fails the second requirement requirement
|
||||||
|
(because it contains `i` and `l`).
|
||||||
|
- `abbceffg` meets the third requirement (because it repeats `bb` and
|
||||||
|
`ff`) but fails the first requirement.
|
||||||
|
- `abbcegjk` fails the third requirement, because it only has one
|
||||||
|
double letter (`bb`).
|
||||||
|
- The next password after `abcdefgh` is `abcdffaa`.
|
||||||
|
- The next password after `ghijklmn` is `ghjaabcc`, because you
|
||||||
|
eventually skip all the passwords that start with `ghi...`, since
|
||||||
|
`i` is not allowed.
|
||||||
|
|
||||||
|
Given Santa\'s current password (your puzzle input), what should his
|
||||||
|
*next password* be?
|
||||||
|
|
||||||
|
Your puzzle input is `hxbxwxba`{.puzzle-input}.
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
|
67
2015/11/solution.py
Normal file
67
2015/11/solution.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#!/bin/python3
|
||||||
|
import sys,time,re
|
||||||
|
from pprint import pprint
|
||||||
|
sys.path.insert(0, '../../')
|
||||||
|
from fred import list2int,get_re,nprint,lprint,loadFile
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
input_f = 'test'
|
||||||
|
|
||||||
|
part = 1
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 1 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
|
||||||
|
if part == 1:
|
||||||
|
letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
|
||||||
|
|
||||||
|
def findpairs(text):
|
||||||
|
pattern = r"([a-zA-Z])\1"
|
||||||
|
matches = []
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while i < len(text):
|
||||||
|
match = re.search(pattern, text[i:])
|
||||||
|
if match:
|
||||||
|
matches.append(match.group(0))
|
||||||
|
i += match.end(0)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return matches
|
||||||
|
|
||||||
|
def validate(password:list):
|
||||||
|
valid = False
|
||||||
|
for i in ['l','i','o']:
|
||||||
|
if i in password:
|
||||||
|
return False
|
||||||
|
for i in range(0,len(password)-2):
|
||||||
|
idx = letters.index(password[i])
|
||||||
|
if password[i+1] == letters[idx+1] and password[i+2] == letters[idx+2]:
|
||||||
|
valid = True
|
||||||
|
#print(password[i],password[i+1],password[i+2])
|
||||||
|
if len(findpairs(''.join(password))) <= 1:
|
||||||
|
return False
|
||||||
|
return valid
|
||||||
|
|
||||||
|
instructions = loadFile(input_f)
|
||||||
|
for i in instructions:
|
||||||
|
i = list(i)
|
||||||
|
print(i)
|
||||||
|
if validate(i):
|
||||||
|
|
||||||
|
input()
|
||||||
|
#print(instructions)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 2 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
if part == 2:
|
||||||
|
exit()
|
||||||
|
|
||||||
|
print("--- %s seconds ---" % (time.time() - start_time))
|
Loading…
Reference in New Issue
Block a user