Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created December 2, 2020 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/7e97c17b46a2779e50b9005c27c26c95 to your computer and use it in GitHub Desktop.
Save JeffersGlass/7e97c17b46a2779e50b9005c27c26c95 to your computer and use it in GitHub Desktop.
def isValid_Sled(low, high, letter, password):
return low <= password.count(letter) <= high
#The 'first', 'second' inputs are given as 1-indexed values per the problem statement
def isValid_Toboggan(first, second, letter, password):
return (password[first-1] == letter) ^ (password[second-1] == letter)
def isValidLine(line, func):
contents = line.split(' ')
lowbound, highbound = [int(x) for x in contents[0].split('-')]
letter = contents[1].strip(":")
password = contents[2].strip("\n")
return func(lowbound, highbound, letter, password)
with open('input.txt', 'r') as infile:
data = [line for line in infile]
print ("The solution to part 1 is: " + str(len([line for line in data if isValidLine(line, isValid_Sled)])))
print ("The solution to part 2 is: " + str(len([line for line in data if isValidLine(line, isValid_Toboggan)])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment