Skip to content

Instantly share code, notes, and snippets.

@redspider
Created December 2, 2020 07:58
Show Gist options
  • Save redspider/7b09be59fa950dd2abcc2dfe9653c821 to your computer and use it in GitHub Desktop.
Save redspider/7b09be59fa950dd2abcc2dfe9653c821 to your computer and use it in GitHub Desktop.
valid = 0
# Part 1
for line in INPUT.split("\n"):
m = re.match(r"(\d+)-(\d+) (\S): (\S+)", line)
(low, high, character, password) = m.groups()
if int(low) <= password.count(character) <= int(high):
valid += 1
print(valid)
# Part 2
valid = 0
for line in INPUT.split("\n"):
m = re.match(r"(\d+)-(\d+) (\S): (\S+)", line)
(first, second, character, password) = m.groups()
def is_character_at(position):
return password[int(position) - 1] == character
if is_character_at(first) ^ is_character_at(second):
valid += 1
print(valid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment