AnonymousNamefag

Advent of Code Day 2 Part 2

Dec 9th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.96 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # This program is a filter.
  4. # Input file should be fed
  5. # into standard input.
  6.  
  7. # Remove character at index from string
  8. def puncture( string, index )
  9.   substring1 = string[0,index]
  10.   substring2 = string[index+1,string.length-index-1]
  11.   return substring1 + substring2
  12. end
  13.  
  14. # Read stdin file into array
  15. codes = Array.new
  16. i = 0
  17. while codes[i] = gets
  18.   i = i + 1
  19. end
  20. # Next line removes ending
  21. # blank line if there is one
  22. # because it tends to cause
  23. # problems later on.
  24. codes.pop if codes[-1].nil?
  25. codes.each{ |code| code.strip! }
  26.  
  27. # Create hash table for each position
  28. hashes = Array.new(26)
  29. 0.upto(25) do |i|
  30.   hashes[i] = Hash.new
  31.   codes.each do |code|
  32.     p = puncture( code, i )
  33.     hashes[i][p] = Array.new unless hashes[i].key?(p)
  34.     hashes[i][p].push( code )
  35.   end
  36. end
  37.  
  38. # Find keys with multiple values
  39. 0.upto(25) do |i|
  40.   hashes[i].each do |p|
  41.     if p[1][1]
  42.       puts puncture( p[1][0], i );
  43.     end
  44.   end
  45. end
Add Comment
Please, Sign In to add comment