Skip to content

Instantly share code, notes, and snippets.

@cesarvr
Created June 13, 2020 07:20
Show Gist options
  • Save cesarvr/715d7412b4a4d5b842c437a73a7c9081 to your computer and use it in GitHub Desktop.
Save cesarvr/715d7412b4a4d5b842c437a73a7c9081 to your computer and use it in GitHub Desktop.
def puzzleSolutionPartOne(puzzle):
lhs = ""
units = []
react = lambda a,b: a.lower() == b.lower() and a != b
for candidate in puzzle:
if lhs == "":
lhs = candidate
continue
if not react(lhs, candidate):
units.append(lhs)
lhs = candidate
else:
if units:
lhs = units.pop()
else:
lhs = ""
units.append(lhs)
return len(units)
def puzzleSolutionPartTwo(puzzle):
removeUnits = lambda puzzle, chr: filter(lambda x: x != chr.lower() and x != chr.upper(), puzzle )
removedUnits = {}
for candidate in puzzle:
candidate = candidate.lower()
if not candidate in removedUnits:
removedUnits[candidate] = puzzleSolutionPartOne(removeUnits(puzzle, candidate))
return min( removedUnits.values() )
puzz = "dabAcCaCBAcCcaDA"
puzzle = open("puzzle.txt", "r").read()
print "Solution 1: " + str( puzzleSolutionPartOne(puzzle) )
print "Solution 2: " + str( puzzleSolutionPartTwo(puzzle) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment