r/dailyprogrammer • u/fvandepitte 0 0 • Nov 24 '16
[2016-11-24] Challenge #293 [Intermediate] Defusing the second bomb
Description
The bomb defusing becomes a little more complicated, but the upside is, we only have 5 wires now: white, black, red, orange and green.
The rules for defusing a bomb are as following now:
You have to start with either with a white or a red wire.
If you picked white wire you can either pick another white wire again or you can take an orange one.
If you picked a red wire you have the choice between a black and red wire.
When a second red wire is picked, you can start from rule one again.
Back to the second rule, if you picked another white one you will have to pick a black or red one now
When the red wire is picked, you again go to rule one.
On the other hand if you then picked an orange wire, you can choose between green, orange and black.
When you are at the point where you can choose between green, orange and black and you pick either green or orange you have to choose the other one and then the bomb is defused.
If you ever pick a black wire you will be at the point where you have to choose between green, orange and blackTry to draw this out if it is confusing, it is a part of the challenge. My drawing is available in the notes.
The bomb is defused when you reach the end, so by either cutting a green or orange cable. If you can't do that, bomb will explode
Formal Inputs & Outputs
Input description
You will be givin a sequence of wires
Input 1
white
white
red
white
orange
black
black
green
orangeInput 2
white
white
green
orange
greenOutput description
Output 1
defusedOutput 2
BooomChallenge Inputs
1
white
white
red
red
red
white
white
black
green
orange2
white
black
black
black
black
green
orange3
black
green
green4
red
red
white
orange
black
greenNotes/Hints
For those who had a hard time following the rules, I've mapped it out for you with this image
Bonus
You will be a number of wires and need to state if it is possible to defuse the bomb
Bonus input 1
white 4
red 3
black 4
green 1
orange 1Bonus output 1
defusableBonus input 2
white 4
red 3
black 4
green 0
orange 1Bonus output 2
not defusableBonus challenge input 1
white 3
red 1
black 48
green 1
orange 2Bonus challenge input 2
white 3
red 1
black 48
green 1
orange 1Bonus Note
You do have to use all wires, you can't leave some uncut
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
Edit
/u/cheers pointed out a logical error.
5
u/cheers- Nov 24 '16 edited Nov 24 '16
Javascript
Edit: fixed regex
Edit: Bonus solved with the power of Math
3
u/cheers- Nov 24 '16
2nd rule:
and then
According to the rules if you pick 2 white wires you have to pick Black:
Input 1 should cause an explosion because the starting sequence is
it should be
3
u/Boom_Rang Nov 24 '16 edited Nov 24 '16
Haskell, with bonus (further down)!
I didn't look at OP's answer before attempting it, but I did make full use of the state diagram, thanks for that! :-)
And here is my solution for the bonus:
2
u/fvandepitte 0 0 Nov 24 '16
Haskell, no bonus yet
2
u/ASpueW Nov 24 '16 edited Nov 24 '16
Rust, without bonus
Test output:
Challenge output:
10
u/skeeto -9 8 Nov 24 '16
C with bonus. The state machine is expressed using an adjacency list,
graph. Each 16-bit integer is really a 5-element array of 3-bit integers (i.e. 15 bits). Each of the 5 elements represents a color (in alphabetical order), and the 3-bit value is the next state index. I reserve 0 as the "null" state, which means there's no edge. Trying to take a non-existent edge blows up the bomb. State index 7 is the defuse state. TheNEXTmacro extracts the next state index from a 16-bit state value.To check if an input is defusable, it does a recursive depth-first search of the graph, cutting wires along the way, until either it hits the defuse state (defusable) or it runs out of options (not defusable)
7
u/KeinBaum Nov 24 '16 edited Nov 24 '16
Prolog
Output:
I feel like the bonus should be pretty easy but I guess I don't understand Prolog well enough for it. This just hangs for the bonus inputs:
4
u/seabombs Nov 25 '16
Python3
For the first challenge I assumed it was okay to leave some wires uncut, as long as you finished on the orange/green wire. For bonus challenge, all wires must be cut.
Challenge:
Bonus:
1
u/alexmccoding Dec 06 '16
Was looking for something to do for a plane journey. Not sure this is the best thing...
3
u/AdmissibleHeuristic 0 1 Nov 24 '16 edited Nov 24 '16
MATLAB
No bonus. Input sequence as a cell array:
3
u/oddolatry Nov 25 '16
Clojure
Regular solution also picks up the case where we have correct steps up until we run out of wires to cut (not quite in the spirit of the challenge, but, hey, bomb squad dude, just don't cut that last dead end wire and start running). I'm confident in the regular solution, but my bonus solution is a mess. It seems to yield correct results, but it's not very sexy or efficient. Also included an alternate solution for non-bonus.
Output by case:
3
u/Mr_Persons Nov 26 '16 edited Nov 28 '16
Python 2.7
With bonusNo more bonus. I am following a course at my local university dealing with graph traversal amongst other things. Posting my solution for BFS may thus get me in trouble as it might get misconstrued as enabling my other students to plagiarize it. Sorry if it comes over as a bit extreme, but it's not a risk I'm willing to take. If you still want to see my implementation for bfs in this domain, shoot me a pm...Using a state machine, Template Method Pattern
andBFSfor the bonus.2
u/gju_ Nov 25 '16
Clojure
2
u/infosecguy66 Nov 26 '16
Python 3 - No Bonus
The rules for making cuts is a bit unclear on a couple things, so I implemented the rules as I understood them. Notably, the first demonstrated input of
white, white, red, whitewould fail due to a second red not being selected prior to choosing a white.2
u/LeShirley Nov 26 '16 edited Nov 26 '16
Python without bonus
tried a more hacky approach using dictionaries for compact code, which seems to work just fine
+/u/CompileBot python
edit: made small corrections
2
u/hufterkruk Nov 28 '16 edited Nov 28 '16
Haskell, with bonus now:
Bonus is by no means efficient, but it's fast enough for the challenge inputs.
2
u/Ge_O Nov 28 '16
C#
2
u/Specter_Terrasbane Nov 28 '16
Python 2, with Bonus*
2
u/demreddit Nov 30 '16 edited Nov 30 '16
Python 3, no boni yet. My goal was to do this as simply as possible with just the Python basics, if that makes sense (I'm kinda new at this). Not sure I accomplished that or not, but my code feels pretty terse to me, excluding my gratuitous comments and function calls. Of course, I'm biased... Comments/criticisms are welcome!
Output:
1
u/Verochio Nov 24 '16 edited Nov 25 '16
VBA, no bonus
Edit: Refactored
Output
1
u/pie__flavor Nov 25 '16 edited Nov 25 '16
+/u/CompileBot Scala
Input:
edit: rip compilebot. apparently the compiler can't find scala-library.jar
1
u/Stan-It Nov 25 '16
Python
1
u/MoX46 Nov 27 '16
PHP 5.6
Without Bonus
1
u/justin-4 Nov 27 '16 edited Nov 27 '16
Java
I'm late, vaca was nutty.
No Bonus. User friendly. Prints rules, takes input.
1
u/Tetsumi- 1 0 Nov 27 '16
Racket with bonus
1
u/Minolwa Nov 28 '16
Scala
1
u/K2ray Nov 29 '16
Can anyone produce a Random Test Case (javascript/python/C#/Java) for this challenge (including for the Bonus)? Thanks.
1
u/draegtun Nov 29 '16 edited Nov 29 '16
Rebol
Following on from my earlier easy challenge solution...
Test program:
Output:
NB. Tested in Rebol 3
1
u/[deleted] Nov 30 '16 edited Nov 30 '16
C++ with bonus. I decided to forgo graphing it and instead brute forced the bonus with math and used recursion for the basic challenge. It turned out really long and way uglier than I would've liked.
challenge:
bonus:
1
u/[deleted] Nov 30 '16 edited Nov 30 '16
First submission! This was fun - feedback very welcome.
Implemented in Go
EDIT now with bonus! tests passing
1
u/[deleted] Dec 01 '16 edited Dec 01 '16
Ruby No bonus
Kind of saw a pattern with the stages, particularly stages 0, 1, 2 are very similar and stages 4 and 5 are similar. So I imagine can somehow encapsulate them into just three methods instead of five methods like I did for the stages.
+/u/CompilerBot Ruby
1
u/erfvbtgfdctyhnmujhgb Dec 01 '16
Ruby
It seems I've managed to do this.
+/u/CompileBot Ruby
1
u/Nhowka Dec 01 '16
F# Without bonus
1
u/TinkersWithtruth Dec 02 '16
Python 3 (no bonus)
Critique away!
1
u/kittensupernova Dec 03 '16
Go (no bonus, may add later) A bit verbose, but works. Feedback welcome!
Output:
ex1: defused ex2: Booom
input1: defused input2: Booom input3: Booom input4: Booom
1
u/AuslaenderLerner Dec 03 '16 edited Dec 03 '16
Java
(Without bonus, may do them later).
Main
Bomb
Output:
I suppose input #4 it's supposed to have next an orange one? If it's alright like that, it'll explode. If it's missing, it'll be solved.
1
u/crompyyy Dec 07 '16
Java First ever submission, implemented the state machine as an enum, its a little ugly because I put it all in the one class. Feedback very welcome.
1
u/hy3bk Dec 08 '16 edited Dec 08 '16
Python 3. No bonus. Newby and first time posting... Any feedback welcome :)
Output:
1
u/tecnofauno Dec 21 '16
C++ Challenge + Bonus1 online compiler
1
u/lepatrick714 Jan 04 '17
Here is my solution in C++. It was fun! I will do the bonus soon! Cheers!
1
u/zatoichi49 Mar 16 '17 edited Mar 20 '17
Method:
Concatenate the first letter of each colour of wire into a list, and use regular expressions to parse out potential 'paths' and account for any loops (one, two, three, four are strings of each of the separate challenge inputs). For the bonus, each set has only one red wire. For a 'one red wire' set to be defused, it must pass the regex 'rb+(go|og)' or 'wwrwob*(go|og)', and the totals for the wires must match the regex totals. Also, if there are three white wires and only one red, then there has to be two orange wires. The first bonus challenge passes these tests, while the second doesn't (as w=3, but o!=2).
Python 3 (with Bonus):
Output: