See https://www.youtube.com/watch?v=q6M_pco_5Vo or https://en.wikipedia.org/wiki/Water_pouring_puzzle
https://kotlinlang.org/docs/reference/
https://kotlinlang.org/api/latest/jvm/stdlib/index.html
-
Transform
HelloWorldto a Kotlin class -
Remove boilerplate
-
create a
Glassdata class withcapacity, andcurrentInt, with 0 as default value forcurrent -
create
emptymethod returning anotherGlassusingcopymethod intoGlass -
outside the class, create an extension method
fillreturning anotherGlassusingcopymethod -
create a
Statedata class with aListofGlass -
(bonus) create a secondary constructor for
Statewith avarargofGlass -
create a
Moveclass -
create a
Emptydata class with a glassindex(an Int) that extends theMoveclass overridetoStringusing String template -
create a
Filldata class with a glassindex(an Int) that extends theMoveclass overridetoStringusing String template -
create a
Pourdata class withfromandtoglass index that extends theMoveclass overridetoStringusing String template -
(bonus) seal the
Moveclass -
(bonus) create a computed property (custom getter) to get the missing part into a
Glass(capacity - current) -
define a
solvefunction with form and toStateand returning aListofMovedo not implement the function, useTODO()for the moment -
create a
mainmethod, into this method create initial state 0/8, 0/5, and 0/3Glasscreate final state 4/8, 0/5, and 0/3Glass -
call
solveto obtain result display all moves usingforEachwith a lambda -
create a
processmethod that taking aMovea returning a newStateinto theStateusemapIndexedon glasses, awhento match differentMoveNote:if-then-elseis a expression (i.e. can return a value) -
Into the
Glassclass (or with an extension method) create the method (operator) than allow the syntaxnewGlass = glass + 4create the method (operator) than allow the syntaxnewGlass = glass - 3use these operators inState#processmethod -
create method
availableMovesreturningListof availableMoveinto theStateClass Note: avoid empty an already emptied class Note: avoid fill an already filled class Note: you cannot pour a glass to itself -
(bonus) create helper function
isEmptyandisFillinGlass -
(bonus) transform helper function
isEmptyandisFillto lazy val inGlass -
create a private
solveAuxrecursive function with aListofPairofStateandListofMove, with a targetState, and with aSetof already visitedStatereturning theListofMoveskip implementation withTODO() -
implements
solvefunction with thesolveAuxmethod -
for the
sloveAuxreal implementation first implement the terminal case -
then create the next
ListofPair<State, List<Move>> -
create the new
Listof visitedState -
finish the recursion
-
(bonus) use a
typealiasforPair<State, List<Move>> -
(bonus) use a
tailrec -
(bonus) make
solveAuxan internal function intosolve, remove thetargetargument (closure) -
(bonus) safety check for data class using
initblock -
(bonus) safety check
solveandsolveAux -
drink non-empty glass ;-)
Solve 0/8, 0/5, 0/3 --> 4/8, 0/5, 0/3 in 8 moves:
Fill 1 (0/8, 5/5, 0/3)
Pour 1 in 0 (5/8, 0/5, 0/3)
Fill 1 (5/8, 5/5, 0/3)
Pour 0 in 2 (2/8, 5/5, 3/3)
Empty 2 (2/8, 5/5, 0/3)
Pour 1 in 0 (7/8, 0/5, 0/3)
Pour 0 in 2 (4/8, 0/5, 3/3)
Empty 2 (4/8, 0/5, 0/3)