r/dailyprogrammer • u/fvandepitte 0 0 • Nov 21 '16
[2016-11-21] Challenge #293 [Easy] Defusing the bomb
Description
To disarm the bomb you have to cut some wires. These wires are either white, black, purple, red, green or orange.
The rules for disarming are simple:
If you cut a white cable you can't cut white or black cable.
If you cut a red cable you have to cut a green one
If you cut a black cable it is not allowed to cut a white, green or orange one
If you cut a orange cable you should cut a red or black one
If you cut a green one you have to cut a orange or white one
If you cut a purple cable you can't cut a purple, green, orange or white cableIf you have anything wrong in the wrong order, the bomb will explode.
There can be multiple wires with the same colour and these instructions are for one wire at a time. Once you cut a wire you can forget about the previous ones.
Formal Inputs & Outputs
Input description
You will recieve a sequence of wires that where cut in that order and you have to determine if the person was succesfull in disarming the bomb or that it blew up.
Input 1
white
red
green
whiteInput 2
white
orange
green
whiteOutput description
Wheter or not the bomb exploded
Output 1
"Bomb defused"Output 2
"Boom"Notes/Hints
A state machine will help this make easy
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
6
u/fvandepitte 0 0 Nov 21 '16
Haskell
3
u/Jesus_Harold_Christ Nov 21 '16
Actually, does someone want to take on the intermediate challenge of writing the rules correctly?
80
u/Specter_Terrasbane Nov 21 '16 edited Nov 22 '16
Python 2
Edit: Thanks to Dustypigjut for pointing out my error in the purple rule!
21
u/skeeto -9 8 Nov 21 '16
C, using a state machine. The state machine is packed into a 64-bit integer (
rules = 0x2a140b3bcULL), which is used as an array of six 6-bit integers. Each 6-bit integer has a bit set for each edge. An input that doesn't match an edge is a failure. That is, I've encoded the challenge description into a single integer.To map a color name to the range 0–5, I'm using a dumb trick with strstr() so that I don't have to write the loop. It scans the string of color names to find its offset, each divisible by 6 (maximum string length). It's actually less efficient than have separate strings since it tries to match between strings. But, hey, it makes my program a little shorter!
10
u/itsme86 Nov 21 '16 edited Nov 21 '16
C#
10
u/casualfrog Nov 21 '16
JavaScript (trying out some ES6 features)
10
u/AdmissibleHeuristic 0 1 Nov 23 '16
Python 2
8
u/msJacksonxx Nov 22 '16
C# Meant to post this yesterday but ended up leaving before I pressed "save" lol
7
u/franza73 Nov 23 '16
6
u/RVerite Nov 21 '16
My first submission in this sub. Cheers!
4
u/free2use Nov 21 '16 edited Nov 21 '16
With just a map instead of state machine, but result is same
+/u/CompileBot Clojure
Input:
4
u/CleverError Nov 21 '16
Swift 3
4
u/TinkersWithtruth Nov 23 '16
Python 3
Feedback welcomed!
1
u/[deleted] Nov 22 '16
What is the output of "Red, Green"?
Is the result is Boom? as you have to cut Orange or White after Green?
1
u/maxiewawa Nov 28 '16
Do you mean to say that if you cut a white wire, you can't cut another white wire? I don't understand the statement 'If you cut a white cable you can't cut white or black cable.'
1
u/Anndrei98 Nov 30 '16
Those many codes here and I don't even know how to make a matrix in VB so well ... :( Anyone can recommend me a site/book to learn programming ? I'd like to start with VB/C# since is easy and I'm doing it at school too. I have to mention that in school's lessons we aren't doing anything else than 'copy-paste'-ing the code from a web without explanations. Thank you, Reddit, great community !
1
u/iDownvoteBlink182 Jan 19 '17
I don't understand, it clearly says that if you cut a white cable you can't cut a white cable, so how is input 1 a successful defusal? You cut the first white wire which means that cutting the second white wire would blow up the bomb.
3
u/Minolwa Nov 21 '16 edited Nov 21 '16
Scala
I'm accumulating all of my solutions over on my Github! Go check it out!
This was generated by a script.
3
u/mafilikescats Nov 23 '16
JavaScript
2
u/batanete Nov 21 '16 edited Nov 21 '16
C#
Edit: typo;
2
u/[deleted] Nov 21 '16
Crystal:
Play: https://play.crystal-lang.org/#/r/1eia
2
u/narcodis Nov 21 '16
Go, using bitmasks
+/u/CompileBot Go
Input
2
u/nevec71 Nov 21 '16
C#
2
u/Scroph 0 0 Nov 21 '16
C++ :
There's really no need to store the wires combinations before handling them, it can be done lazily in the main loop.
2
u/jm2dev Nov 21 '16
Scala, using recursion.
2
u/KRoosevelt Nov 22 '16
R
2
u/Mr_Persons Nov 22 '16
Python 2.7 First time really making use of Python's class system. I tried to make use a Template Method Pattern and a state machine.
Feedback is very much welcome, especially about comparing instances of objects that have no attributes. I had a lot of trouble figuring that part out...
2
u/gravitationalBS Nov 22 '16
Python 2
I'm sure there is a better way of processing input, but this will work just piping from the command line.
2
u/[deleted] Nov 23 '16 edited Nov 23 '16
Python my first time using python and first time here
2
u/demreddit Nov 23 '16 edited Nov 23 '16
Python 3. I have no idea what a 'state machine' is, but apparently I made one...? Anyway, it's my hasty solution. (This looks like a fun one to go back and try to streamline later.)
2
u/altorelievo Nov 23 '16
C++
2
u/hufterkruk Nov 23 '16
Haskell:
Not the "nicest" solution, but it works.
2
u/[deleted] Nov 23 '16 edited Nov 24 '16
oforth
I'm really having a lot of fun with oforth, I don't write the most terse or elegant code, but it really is fun, finally a forth that I manage to use ;)
EDIT: Franck, the creator of oforth was looking into it as well, and giving this amazing solution, I just wanted to add it so that people can see that oforth doesn't have to be as clunky as I made it seem. so here goes an enhanced version, that I find really beautiful where he made the code shorter, more readable and generally more awesome:
2
u/Sharparam Nov 23 '16
Ruby:
2
u/gju_ Nov 23 '16
Clojure
2
u/Stan-It Nov 23 '16
Python
2
u/merlin-_- Nov 24 '16
C#
First submission. Quick and dirty:
2
u/Verochio Nov 24 '16
VBA
2
u/pie__flavor Nov 25 '16
+/u/CompileBot Scala
Input:
2
u/Saytahri Nov 26 '16
Haskell
2
u/sebposselt Nov 28 '16
F#
2
u/ponytoaster Nov 28 '16 edited Nov 28 '16
C# , Note that i take my list in as a comma list (red,purple,green, etc) , rather than line by line just to make it quicker for me to test, otherwise i'd loop over lines in a file, rather than items in an array
Although looking at everyone elses solutions it just feels messy and cumbersome :( Probably because I used LINQ a lot. Happy for someone to explain how the hell state machines work and how i could make this nicer!
2
u/nachkonvention Dec 01 '16
Prolog
2
u/ChemiCalChems Jan 19 '17
C++:
Solution(gist)
2
u/Autryy Feb 18 '17 edited Feb 18 '17
Java
2
u/[deleted] Apr 04 '17
Haskell
1
u/cheers- Nov 21 '16 edited Nov 21 '16
Javascript
Started Js 2 days ago, I'm trying to figure out how to do stuff with it.
run with:
1
u/Godspiral 3 3 Nov 21 '16
in J, by handish rather than sat
T ' purple black black'
defused
T ' white red green'
defused
T ' white red green white'
Boom
T ' white orange green'
Boom
1
u/ASpueW Nov 21 '16
Rust
1
u/KoncealedCSGO Nov 21 '16 edited Nov 21 '16
Java I couldn't get green to work properly with negations. So I just wrote a lot of || (or's).
1
u/Anb623 Nov 21 '16
JAVA
1
u/purloin_a_coin Nov 21 '16
Javascript I'm a noob, so feedback welcome
1
u/[deleted] Nov 21 '16
Mathematica
The idea is to use the optional second argument to Split the list of wire colors anywhere where the second color is not allowed to follow the first, then check to see if any splits occurred.
1
u/Ge_O Nov 21 '16
C#
1
u/[deleted] Nov 22 '16 edited Nov 22 '16
Haskell:
Result:
[Edit]: Assuming cuts are ordered (defuseE293).
1
u/primaryobjects Nov 22 '16
Javascript
Demo | Gist
I solved this using a bitmask for the rules. I assigned each wire color to a bit in the order: white 0, black 1, purple 2, red 3, green 4, orange 5. Then I created the rules by doing a bitmask and assigning a positive or negative rule (bitmask value) to each wire.
After that, it's easy to simply loop over the problem set and check which rule to apply (positive or negative), examine the next wire and make sure the rule holds valid, and return the result. For positive cuts (i.e., must cut this color wire) if the bit isn't set or no more wires, then boom. For negative cuts (i.e., do not cut this color wire) if the bit is set, then boom, otherwise ok.
One cool thing, if the bomb explodes, I return a true/false AND the color of the offending wire. :)
1
u/[deleted] Nov 22 '16 edited Nov 22 '16
Pascal I'm starting to learn Pascal and I learned java by myself, so still a beginner;
1
u/[deleted] Nov 22 '16
+/u/CompileBot R
1
u/oddolatry Nov 22 '16
Clojure
Something's nettling me about this, but it does function as required.
1
u/justin-4 Nov 22 '16 edited Nov 22 '16
Java
Beginner. First dailyP post. Obligatory CS Easter egg included.
I've modified this code 10 or 12 times.
1
u/free_elvis Nov 22 '16
Bash - Regex
1
u/vzaardan Nov 22 '16
Elixir
1
u/IWieldTheFlameOfAnor Nov 22 '16
Python 3
1
u/[deleted] Nov 22 '16 edited Nov 22 '16
Java
1
u/rizzeal Nov 22 '16
Python 3
1
u/[deleted] Nov 22 '16
This is my first attempt at one of these. I used Python as I've only used it once before years ago and wanted experience. I generally code in C# and I think that's kind of apparent in my code here.
1
u/DesperatelyCurious Nov 22 '16
Python 3, not sure it works for all cases
1
u/Regara_Hontha Nov 22 '16
Powershell
I found that the switch statement in powershell really helps these things. I'm not completely sure that the extra set of parenthesis surrounding the entire conditional for the switch is necessary, but it's there.
1
u/Nymall Nov 22 '16
PHP 5.2
Output:
1
u/themajesticpark Nov 22 '16
Java:
Edit: Formatting failure D:
1
u/sickapps420 Nov 22 '16
Bash feedback welcome.
1
u/SirDinkleburry Nov 26 '16 edited Nov 26 '16
Python 2, no fancy regex though
1
u/LeShirley Nov 26 '16
Python 3
could've been solved more elegantly, but I guess this works as a solution
1
u/Tetsumi- 1 0 Nov 26 '16
Racket
1
u/[deleted] Nov 26 '16
I am learning C#, so it is a bigger code that it could be.
1
u/tealfan Nov 26 '16 edited Nov 26 '16
Java
Output
1
u/MoX46 Nov 27 '16
PHP 5.6
In "index.php"
1
u/joelhed Nov 27 '16
Python 3, using sets!
1
u/El_Dumfuco Nov 27 '16
Matlab. Very unoptimized but it works. Shoutouts to set theory.
1
u/kaiserzep Nov 27 '16
my python solution
def disarm(i):
print disarm(['white', 'orange', 'green', 'white'])
1
u/lbtouda Nov 27 '16 edited Nov 27 '16
R
1
u/4kpics Nov 28 '16
Python 3 with regex
1
u/Pretentious_Username Nov 28 '16
Julia
I've only just started learning Julia but loving it so far. Any advice would be gratefully received.
Which when run prints:
1
u/draegtun Nov 29 '16
Rebol
Example usage in Rebol console:
NB. Tested in Rebol 3.
1
u/fleekonpoint Nov 29 '16
1
u/ShroudedEUW Nov 30 '16
C#
At first I hardcoded all 36 state switches. Then my challenge became to only define the rules once. I got that part working.
1
u/Yopu Nov 30 '16
Rust
Longer than others here. Yet likely more maintainable than a clever solution.
Fun project.
1
u/_Absolut_ Nov 30 '16
Python 3.5
1
u/[deleted] Dec 01 '16 edited Dec 01 '16
Very brute force way, wonder if there is a better way.
Ruby
+/u/compilerbot Ruby
1
u/SuperSmurfen Dec 01 '16 edited Dec 01 '16
Java
Takes input via program arguments.
1
u/smapti Dec 01 '16
C++. Sloppy return usage but prevents checking unnecessary wires after BOOM.
1
u/dariosilva88 Dec 02 '16 edited Dec 02 '16
Haskell
OUTPUT
1
u/robsie Dec 02 '16
Trying to learn Erlang so this may not be idiomatic:
1
u/AuslaenderLerner Dec 03 '16
Java
Main
Bomb
1
u/net_nomad Dec 04 '16
Python 2 (Only tested given inputs, so might have bugs -- also haven't written python in a while)
1
u/mochancrimthann Dec 05 '16
Ruby Unfortunately this turned out a lot like the solution from Specter_Terrasbane's but I didn't see it until finishing my solution. Oh well! Let me know if there's anyway to improve.
1
u/kbake_dev Dec 05 '16 edited Dec 05 '16
Python 3
1
u/[deleted] Dec 06 '16 edited Dec 06 '16
This is my first submission in this sub, looking forward to any feedback. Edit: I used Java
1
u/revampedassassin Dec 07 '16
An orange*
1
u/_ninjajack Dec 12 '16 edited Dec 12 '16
JavaScript
First submission here. Bit sloppy as it's late but feel free to poke me with advice ~
Edit: Just noticed that the input is a bit ambiguous. If input is supplied in plaintext (not an array) you'd need to add a
var wires = input.split(" ");Edit 2: Removed JSON double quotes from rules obj to make it tidier... Been doing too much JSON work lately.
1
u/SpunkyBoiFresh Dec 16 '16 edited Dec 16 '16
Python
So this is the third piece of code I've ever written, and I'm looking for feedback. My idea was this:
And I did it! It's just massive... at least compared to everything else here, and much of the shorter stuff I don't even begin to understand. So. With the outline above, what can I do to cut it down?
1
u/murphs33 Dec 16 '16 edited Dec 16 '16
Old thread, but feck it.
Python 2:
1
u/benpva16 Dec 16 '16
C++. I used a lot of if/else instead of a switch and enum.
1
u/adam_AU Dec 24 '16
Python 3
1
u/alabomb Dec 27 '16
C++ - it doesn't feel very pretty but it works?
1
u/karrash76 Dec 28 '16
Java
1
u/dandyfap Dec 30 '16
Python 3
1
u/fmpundit Dec 30 '16
Horrifically bad code, especially when I see u/Specter_Terrasbane solution. I might spend some time reducing the amount of lines... But the logic seems sound.
python3.5
1
u/F1nches Jan 09 '17
PHP New to programming, so this might not be the best solution out there, but it works I think.
1
u/neptunDK Jan 12 '17
Python 3
While trying to get back to making Python code I thought I better check out some of the puzzles here. I started with adding some unittest rules, and hopefully ended up covering all of them correctly. So this could be considered TestDrivenDevelopment ;).
1
u/umop-apisdn-wa Jan 15 '17
JAVA
Hello, this is my first post here. I am new to Java/Programming and would like feedback on my code.
1
u/Balthazar582 Jan 19 '17
Javascript
1
u/cheerios_are_for_me Jan 24 '17
Golang
1
u/zer0d4yz Feb 05 '17 edited Feb 05 '17
C
#include <stdio.h>
#define white 0x20
#define black 0x10
#define red 0x8
#define orange 0x4
#define green 0x2
#define purple 0x1
could be done so much better. btw why the bloody hell aren't my includes/defines in the box? Ive got them spaced 4 spaces ?
1
u/carlogarro Feb 15 '17
JAVA, I tried to create a game. I also would like to know how to do it with a state machine. I hope you like it!
1
u/frrarf Apr 20 '17
Javascript
0
u/[deleted] Nov 21 '16
[deleted] — view deleted comment
0
u/[deleted] Nov 25 '16
[deleted] — view deleted comment