fork download
  1. def bomb_check(input)
  2. check=""
  3. input.each { |wire|
  4. check.insert(-1, wire[0])
  5. }
  6. pat = /(((wwr)*(rr)*)*(wwb|rb|wo)b*(og|go))/
  7. unless pat.match(check).nil?
  8. if pat.match(check)[0] == check
  9. return "Bomb defused"
  10. else
  11. return "Explodes!"
  12. end
  13. else
  14. return "Explodes!"
  15. end
  16. end
  17.  
  18. def defusable?(input)
  19. c = Hash.new(0)
  20. input.each { |wire|
  21. x = wire.split(" ")
  22. c[x[0][0]] = x[1].to_i
  23. }
  24.  
  25. s1, s2, s3 = Hash.new(0), Hash.new(0), Hash.new(0)
  26. s1["w"], s1["b"], s1["r"], s1["o"], s1["g"] = c["w"]-2, c["b"]-1, c["r"], c["o"]-1, c["g"]-1
  27. s2["w"], s2["b"], s2["r"], s2["o"], s2["g"] = c["w"], c["b"]-1, c["r"]-1, c["o"]-1, c["g"]-1
  28. s3["w"], s3["b"], s3["r"], s3["o"], s3["g"] = c["w"]-1, c["b"], c["r"], c["o"]-2, c["g"]-1
  29.  
  30. def zeroedHash?(hash)
  31. zeroed = true
  32. hash.each_value { |val|
  33. unless val == 0
  34. zeroed = false
  35. end
  36. }
  37. return zeroed
  38. end
  39. def sn_check(sn)
  40. checks = false
  41. unless sn["b"] < 0
  42. an = sn["w"] / 2
  43. cn = sn["r"] / 2
  44. sn["b"] = 0
  45. test_a = []
  46. (0..an).each { |i|
  47. (0..cn).each { |j|
  48. test_a.push([i,j])
  49. }
  50. }
  51. test_a.each { |iteration|
  52. temp = sn
  53. temp["w"] -= iteration[0]*2
  54. temp["r"] -= iteration[0] + iteration[1]*2
  55. if zeroedHash?(temp)
  56. checks = true
  57. end
  58. }
  59. else
  60. checks = false
  61. end
  62. return checks
  63. end
  64. sn_check(s1) || sn_check(s2) || sn_check(s3) ? (return "Defusable") : (return "Not defuseable")
  65.  
  66. end
  67.  
  68. #I'm using shorthand, but this will also work for white instead of w, red instead of r and e.t.c
  69. puts "Challenge:"
  70. print 'Example1: ', bomb_check(["w", "w", "r", "w", "o", "b", "b", "g", "o"]), "\n"
  71. print 'Example2: ', bomb_check(["w", "w", "g", "o", "g"]), "\n"
  72. print 'Challenge1: ', bomb_check(["w", "w", "r", "r", "r", "w", "w", "b", "g", "o"]), "\n"
  73. print 'Challenge2: ', bomb_check(["w", "b", "b", "b", "b", "g", "o"]), "\n"
  74. print 'Challenge3: ', bomb_check(["b", "g", "g"]), "\n"
  75. print 'Challenge4: ', bomb_check(["r", "r", "w", "o", "b", "g"]), "\n"
  76. puts ""
  77. puts "Bonus:"
  78. print 'Example1: ', defusable?(["white 4", "red 3", "black 4", "green 1", "orange 1"]), "\n"
  79. print 'Example2: ', defusable?(["white 4", "red 3", "black 4", "green 0", "orange 1"]), "\n"
  80. print 'Input1: ', defusable?(["white 3", "red 1", "black 48", "green 1", "orange 2"]), "\n"
  81. print 'Input2: ', defusable?(["white 3", "red 1", "black 48", "green 1", "orange 1"]), "\n"
Success #stdin #stdout 0.02s 9792KB
stdin
Standard input is empty
stdout
Challenge:
Example1: Bomb defused
Example2: Explodes!
Challenge1: Bomb defused
Challenge2: Explodes!
Challenge3: Explodes!
Challenge4: Explodes!

Bonus:
Example1: Defusable
Example2: Not defuseable
Input1: Defusable
Input2: Not defuseable