# DieHard 3
# Collect exactly 4 liters
import time
from machine import Pin
# --------------------------------------------------------------------------
# Functions
# --------------------------------------------------------------------------
# Fill entirely
def fillBoard(diodes):
for d in diodes:
d.value(1)
if len(diodes) == 5:
status = [1,1,1,1,1]
if len(diodes) == 3:
status = [1,1,1]
return status
# Empty entirely
def emptyBoard(diodes):
for d in diodes:
d.value(0)
if len(diodes) == 5:
status = [0,0,0,0,0]
if len(diodes) == 3:
status = [0,0,0]
return status
# Update specific state
def updateDiodes(diodes, status):
for i in range(len(diodes)):
diodes[i].value(status[i])
# Transfer from one side to the other
def transferContent(diodesSrc, diodesDst):
global status5, status3
# Identify source & destination
if len(diodesSrc) == 5:
statusSrc = status5
statusDst = status3
else:
statusSrc = status3
statusDst = status5
# Identify available pools
availableToSend = sum(statusSrc)
availableToReceive = len(statusDst)-sum(statusDst)
# Transfer content theoretically
while (availableToSend > 0) and (availableToReceive > 0):
statusDst = statusDst[1:] + [1]
statusSrc = [0] + statusSrc[:-1]
availableToSend -= 1
availableToReceive -= 1
# Transfer content practically
updateDiodes(diodesSrc, statusSrc)
updateDiodes(diodesDst, statusDst)
# Memorize new states
if len(diodesSrc) == 5:
status5 = statusSrc
status3 = statusDst
else:
status3 = statusSrc
status5 = statusDst
# --------------------------------------------------------------------------
# Correct solution
# --------------------------------------------------------------------------
S = [0,1,1,1,1]
# --------------------------------------------------------------------------
# Items description
# --------------------------------------------------------------------------
# LED boards
D5 = [Pin(6, Pin.OUT), Pin(5, Pin.OUT), Pin(4, Pin.OUT), Pin(3, Pin.OUT), Pin(2, Pin.OUT)]
status5 = [0,0,0,0,0] # idx 0 is top level
D3 = [Pin(19, Pin.OUT), Pin(20, Pin.OUT), Pin(21, Pin.OUT)]
status3 = [0,0,0] # idx 0 is top level
# Pushbuttons
push_button_left_fill = Pin(28, Pin.IN, Pin.PULL_DOWN)
push_button_left_empty = Pin(27, Pin.IN, Pin.PULL_DOWN)
push_button_transfer_left = Pin(22, Pin.IN, Pin.PULL_DOWN)
push_button_transfer_right = Pin(26, Pin.IN, Pin.PULL_DOWN)
push_button_right_fill = Pin(17, Pin.IN, Pin.PULL_DOWN)
push_button_right_empty = Pin(16, Pin.IN, Pin.PULL_DOWN)
# Check status diodes
diode_OK = Pin(15, Pin.OUT)
diode_KO = Pin(12, Pin.OUT)
# --------------------------------------------------------------------------
# Main loop
# --------------------------------------------------------------------------
while True:
# Callbacks
if push_button_left_fill.value():
status5 = fillBoard(D5)
if push_button_left_empty.value():
status5 = emptyBoard(D5)
if push_button_right_fill.value():
status3 = fillBoard(D3)
if push_button_right_empty.value():
status3 = emptyBoard(D3)
if push_button_transfer_left.value():
transferContent(D3, D5)
if push_button_transfer_right.value():
transferContent(D5, D3)
# Check result
if status5 == S:
diode_OK.value(1)
diode_KO.value(0)
break
else:
diode_OK.value(0)
diode_KO.value(1)
# --------------------------------------------------------------------------
# Finish puzzle
# --------------------------------------------------------------------------
print('***Congrats on solving the puzzle!***')