Skip to content

Instantly share code, notes, and snippets.

@blackknight36
Created April 8, 2016 21:22
Show Gist options
  • Save blackknight36/7cf9daa7ae6e5ccfd28536356eae5ce4 to your computer and use it in GitHub Desktop.
Save blackknight36/7cf9daa7ae6e5ccfd28536356eae5ce4 to your computer and use it in GitHub Desktop.
How many beers can you drink?
#!/usr/bin/python
def drink_beer():
global caps
global empties
global full
caps += 1
empties += 1
full -= 1
print "Drank full bottle.\n"
def trade_empties():
global caps
global empties
global full
global total
empties -= 2
full += 1
total += 1
print "Traded in two empty bottles.\n"
def trade_caps():
global caps
global empties
global full
global total
caps -= 4
full +=1
total += 1
print "Traded in four bottle caps.\n"
# start with 5 full, unopened bottles (10/2)
empties = 0
full = 5
caps = 0
total = 5
while (1):
while (empties >= 2):
trade_empties()
while (caps >= 4):
trade_caps()
drink_beer()
print "empty bottles:", empties
print "caps held:", caps
print "Total bottles:", total, "\n"
if (empties < 2 and caps < 4 and full <= 0):
print "Nothing to trade or drink. Passing out.\n\n"
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment