5aliases = ["docs/learn/hoon/workbook/bomb"]
7In this exercise, we will build a generator that takes a list of wires to be cut
8in order to defuse a bomb. The bomb is defused according to the following rules.
10There are 6 possible colors of wire: white, black, purple, red, green, and
13If you cut a white cable, you cannot cut white or black cable.
15If you cut a red cable, you must cut a green cable.
17If you cut a black cable, you cannot cut a white, green, or orange cable.
19If you cut an orange cable, you must cut a red or cable.
21If you cut a green cable, you must cut an orange or white cable.
23If you cut a purple cable, you cannot cut a purple, green, orange, or white
26If the list of cuts does not result in the bomb exploding, the output should be
27a tape `bomb defused`. If any of the rules were broken, it should output the
34|= [* [cuts=(list tape) ~] ~]
47 $(bad (~(got by rules) cut), cuts t.cuts)
49 $(bad (~(got by rules) cut), cuts t.cuts)
52 %- ~(gas by *(map tape (list tape)))
91The start of our generator is simple enough. The `!:` in the first line enables
92a full stack trace in the event of an error. The following three lines create a
93`%say` generator that takes a list of tape as an argument.
95We next use the `=<` rune to compose two hoons, inverted. Remember that it's
96good Hoon style is to have the heaviest parts to the bottom -- that's what we're
99We call `defuse` and pass it `cuts`, the list of tapes we were provided with
100when the generator started.
102Next is the core that has two arms, `defuse` and `rules`. Let's look at `rules`
105We can see that it is a `map` of `tape` to `list` of `tape`. For each of the
106rules above we have specified when you cut a wire, which of the other wires you
107cannot cut. This required some reformulating of the rules given to use at the
108start, but it is much simpler to work with.
119 $(bad (~(got by rules) cut), cuts t.cuts)
120 ?~ (find [cut ~] bad)
121 $(bad (~(got by rules) cut), cuts t.cuts)
125The `defuse` arm is the meat of our program. It is a `gate` that takes a `list`
126of `tape`. We combine a defaulted `list` of `tape` with the face `bad` with the
129If `cuts` is empty, the bomb has been successfully defused.
131Otherwise, we give `i.cuts`, the first item in the list, a face called `cut`.
133If `bad` is empty, we recurse. This recursion sets `bad` to be the list of
134wires we can not cut from the `rules` map and make `cuts` `t.cuts` or the tail
135of `cuts`, removing the first element of the list.
137If `bad` is not empty, we want to search inside of it for the current `cut`. If
138it's not listed, then we are safe to continue and recurse to process the rest of
139the cuts. However, if it is found in the bad list, the user has failed and the
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list