SpinalHDL
1 / 1
View past submissionsLeaderboard

Description

Exact Change, No Exceptions

Someone stole the night bus's farebox controller during the last depot inspection, but the route still runs tonight. Rebuild the controller to accept three kinds of coin, issue tickets without waking the driver, and return exact change before the next passenger starts paying.

The farebox accepts coins worth 1, 2, or 5 credits. A ticket costs exactly 7 credits.

After each coin, add its value to the stored balance. If the balance reaches or exceeds 7, emit a ticket with the excess as change and reset the balance to zero; otherwise emit no ticket and zero change.

Hardware I/O Encoding

  • coins is a stream of unsigned 3-bit values, each equal to 1, 2, or 5
  • the stream contains at most 64 coins
  • each output element is (ticket, change)
  • ticket is 1 when that coin completes a fare
  • change is a 3-bit unsigned value

Example

Input [5, 1, 2, 5, 5] produces [(0, 0), (0, 0), (1, 1), (0, 0), (1, 3)].

Input

coins

Stream of 3-bit Unsigned Integer (up to 64 elements)
Coin values, each equal to 1, 2, or 5 credits.

Output

Stream of (1-bit Unsigned Integer, 3-bit Unsigned Integer) Pair
Ticket pulse and returned change after each coin.