Compare commits
2 Commits
28cacbd5d6
...
69243552de
Author | SHA1 | Date |
---|---|---|
Guido Schweizer | 69243552de | |
Guido Schweizer | 886800afcb |
|
@ -0,0 +1,100 @@
|
|||
Game 1: 1 blue; 4 green, 5 blue; 11 red, 3 blue, 11 green; 1 red, 10 green, 4 blue; 17 red, 12 green, 7 blue; 3 blue, 19 green, 15 red
|
||||
Game 2: 17 red, 10 green; 3 blue, 17 red, 7 green; 10 green, 1 blue, 10 red; 7 green, 15 red, 1 blue; 7 green, 8 blue, 16 red; 18 red, 5 green, 3 blue
|
||||
Game 3: 10 blue, 3 green, 8 red; 15 green, 14 blue, 1 red; 8 blue, 11 red, 2 green; 5 red, 9 green, 6 blue
|
||||
Game 4: 1 red, 3 blue; 3 blue, 3 green, 1 red; 11 blue, 2 green; 2 green, 14 blue; 1 green, 7 blue; 11 blue, 5 green
|
||||
Game 5: 9 green, 5 red, 10 blue; 9 red, 4 blue, 12 green; 9 green, 6 blue, 14 red; 16 green, 8 red, 6 blue; 11 blue, 13 red, 1 green
|
||||
Game 6: 1 blue, 2 green, 16 red; 1 green, 19 red; 1 blue; 3 blue, 2 red, 1 green; 18 red, 2 blue, 1 green
|
||||
Game 7: 2 blue, 9 red, 5 green; 11 blue, 6 green, 4 red; 7 red, 3 green, 5 blue; 5 green, 11 blue, 7 red; 17 blue, 4 red, 3 green; 20 blue, 1 green, 2 red
|
||||
Game 8: 1 green, 6 red, 4 blue; 8 green, 4 blue, 2 red; 2 blue, 5 green
|
||||
Game 9: 1 green, 5 blue; 4 blue; 2 red, 1 blue
|
||||
Game 10: 1 green, 12 blue; 6 red, 4 green; 5 green, 14 blue, 9 red; 6 red, 13 blue, 2 green; 6 red, 17 blue, 1 green
|
||||
Game 11: 19 green, 1 red; 15 red, 7 green; 1 blue, 8 red, 14 green; 1 blue, 11 green, 1 red
|
||||
Game 12: 4 red, 3 green, 12 blue; 4 green, 13 red; 2 green, 15 blue, 5 red; 5 red, 10 blue, 3 green; 3 green, 17 blue, 17 red; 1 red, 4 green, 15 blue
|
||||
Game 13: 5 red, 7 blue; 6 red, 1 green, 11 blue; 17 blue, 11 green, 4 red; 6 red, 9 green, 5 blue; 4 green, 14 blue
|
||||
Game 14: 6 red, 15 blue, 3 green; 4 green, 4 blue, 3 red; 3 blue, 1 green, 5 red
|
||||
Game 15: 4 green, 3 blue, 6 red; 3 blue, 2 red, 4 green; 15 red, 3 green, 4 blue; 11 red, 2 blue, 1 green
|
||||
Game 16: 4 green, 1 blue, 12 red; 10 green, 6 blue, 10 red; 1 blue, 2 green, 15 red; 1 green, 3 red, 4 blue
|
||||
Game 17: 10 green, 11 blue; 13 green, 10 blue, 3 red; 8 red, 3 green, 10 blue
|
||||
Game 18: 3 red; 4 red, 1 blue; 3 green, 3 red; 10 green, 1 blue; 4 red, 6 green, 1 blue; 3 green
|
||||
Game 19: 4 red, 6 green; 11 red, 4 blue, 2 green; 4 green, 8 red; 9 red, 7 green, 2 blue; 13 red, 4 blue; 9 red
|
||||
Game 20: 2 blue, 1 green, 8 red; 3 green, 11 blue, 1 red; 5 blue, 4 red, 6 green; 6 green, 7 red, 5 blue; 7 red, 2 green, 1 blue
|
||||
Game 21: 7 blue, 3 green; 2 green, 2 red, 7 blue; 6 blue, 3 red, 1 green; 9 blue, 1 green, 3 red
|
||||
Game 22: 7 red, 5 blue, 9 green; 6 red, 1 blue, 5 green; 18 red, 7 green, 5 blue; 13 red, 11 green, 1 blue; 13 red, 1 blue, 11 green
|
||||
Game 23: 10 green, 1 blue, 5 red; 2 red, 4 green; 9 green, 2 red; 10 green, 1 blue, 5 red
|
||||
Game 24: 2 red, 6 green, 16 blue; 3 blue, 12 red, 3 green; 6 blue, 4 red, 12 green; 12 green, 14 blue, 3 red
|
||||
Game 25: 5 red, 2 blue, 1 green; 4 blue, 14 red, 2 green; 16 blue, 4 red; 5 red; 12 blue, 16 red; 2 red, 6 blue, 1 green
|
||||
Game 26: 10 blue, 6 green, 5 red; 2 red, 2 green; 8 blue, 5 red; 7 blue, 11 green; 8 green, 9 blue
|
||||
Game 27: 1 red, 2 green; 6 green, 10 blue; 1 red, 6 green, 11 blue; 4 blue, 1 green, 1 red; 1 red, 7 blue; 5 green, 2 blue, 1 red
|
||||
Game 28: 6 blue, 5 red, 3 green; 5 blue, 1 green; 1 green, 8 red, 1 blue; 2 blue, 4 green; 4 red, 5 blue
|
||||
Game 29: 2 red, 4 green, 6 blue; 6 blue, 2 green, 1 red; 10 green, 13 blue, 2 red; 11 green, 11 blue, 2 red; 5 red, 10 green
|
||||
Game 30: 4 green, 4 blue, 5 red; 6 red, 18 green, 7 blue; 1 red, 11 green, 2 blue; 11 red, 2 blue, 1 green
|
||||
Game 31: 1 red, 7 green, 2 blue; 5 red, 6 green, 9 blue; 3 green, 1 red, 9 blue; 3 red; 8 blue, 6 green, 1 red; 14 blue, 4 green, 7 red
|
||||
Game 32: 11 red, 2 blue, 2 green; 18 blue, 1 green; 6 blue, 1 green, 15 red; 4 red, 2 green; 13 red, 6 green, 5 blue; 7 blue, 5 green
|
||||
Game 33: 12 blue, 2 red, 2 green; 5 blue, 3 green; 2 green, 6 red, 4 blue; 12 blue, 13 red; 10 blue, 12 red, 1 green; 4 blue, 4 red
|
||||
Game 34: 2 blue, 1 red, 6 green; 2 blue, 1 green; 1 red, 2 green, 2 blue; 17 red, 1 blue, 2 green; 4 green, 2 red
|
||||
Game 35: 2 blue, 12 red, 2 green; 2 green, 8 red, 11 blue; 17 red, 16 blue; 8 blue, 8 red; 1 green, 6 red, 9 blue
|
||||
Game 36: 6 red, 14 green, 7 blue; 1 green, 1 red, 6 blue; 9 red, 10 blue, 9 green; 11 blue, 14 green, 8 red; 10 red, 6 green, 3 blue
|
||||
Game 37: 6 green; 2 red, 5 green; 4 blue, 4 red, 5 green; 1 red, 7 green, 4 blue
|
||||
Game 38: 1 green, 8 red, 10 blue; 3 green, 5 red, 12 blue; 13 blue, 5 red, 1 green; 3 green, 1 red; 3 red, 15 blue, 6 green; 3 green, 6 red, 4 blue
|
||||
Game 39: 15 green, 3 blue; 3 green; 3 blue, 3 red, 8 green; 4 blue, 10 green, 4 red; 2 green, 5 blue, 4 red; 3 green, 3 blue, 1 red
|
||||
Game 40: 2 red, 3 green, 15 blue; 5 blue, 13 green, 2 red; 11 blue, 1 red; 5 blue, 20 green, 5 red
|
||||
Game 41: 1 red, 2 green, 1 blue; 10 red; 11 green, 1 blue, 5 red; 10 red, 13 green
|
||||
Game 42: 10 red, 8 green, 2 blue; 5 green, 4 red; 6 red; 5 red, 9 green, 2 blue; 2 blue, 2 red, 3 green; 1 blue, 7 red, 2 green
|
||||
Game 43: 8 red, 6 blue, 12 green; 11 green, 2 red, 2 blue; 4 blue, 3 red, 8 green; 14 red, 3 blue; 9 green, 1 blue, 5 red
|
||||
Game 44: 4 red, 5 blue, 4 green; 9 blue, 1 green; 10 green, 2 blue, 4 red; 5 red, 15 green, 12 blue
|
||||
Game 45: 1 red, 2 green; 5 blue; 3 blue, 1 red; 1 blue; 4 green, 1 red
|
||||
Game 46: 3 green, 8 red, 5 blue; 1 blue, 10 red, 5 green; 2 green, 5 red, 3 blue; 5 green, 4 red, 13 blue
|
||||
Game 47: 2 green, 1 blue; 2 red, 2 green, 6 blue; 1 green, 1 red, 1 blue; 2 green, 4 blue, 4 red; 2 green, 6 blue, 3 red
|
||||
Game 48: 4 blue, 1 green, 2 red; 6 blue, 1 red; 4 blue
|
||||
Game 49: 16 blue, 18 green, 13 red; 7 red, 13 blue; 14 green, 12 red; 20 green, 14 red, 12 blue
|
||||
Game 50: 8 blue, 2 red, 1 green; 4 red, 2 green, 5 blue; 6 blue, 2 green; 8 blue, 3 green, 5 red; 1 green, 4 blue
|
||||
Game 51: 2 blue, 8 red; 2 green, 3 red, 2 blue; 12 green; 3 blue, 10 green, 1 red; 13 green, 3 blue, 5 red
|
||||
Game 52: 12 blue, 5 red, 16 green; 4 blue, 13 green; 2 red, 5 blue, 5 green; 4 green, 8 blue, 3 red
|
||||
Game 53: 7 blue, 5 green; 8 green, 8 blue; 4 red, 1 green; 8 blue, 5 red, 11 green
|
||||
Game 54: 4 blue, 3 green, 1 red; 10 green, 4 red, 7 blue; 1 red, 6 blue, 2 green
|
||||
Game 55: 2 red, 10 blue; 13 red, 10 blue; 4 red, 5 blue, 4 green; 3 green, 8 blue; 5 blue, 7 red, 2 green
|
||||
Game 56: 6 green, 7 blue, 9 red; 3 blue, 4 green; 15 red, 3 blue, 3 green; 5 red, 8 green, 7 blue
|
||||
Game 57: 9 red, 13 blue, 2 green; 3 red, 2 green, 3 blue; 1 blue, 5 red, 3 green; 9 blue, 2 green, 2 red; 3 red, 12 blue, 3 green; 2 green, 7 red
|
||||
Game 58: 4 blue, 7 red; 2 blue, 5 green, 1 red; 17 blue, 3 red; 5 green, 1 blue, 4 red; 4 green, 14 blue, 3 red
|
||||
Game 59: 5 blue, 4 red; 7 red; 4 blue, 4 red, 1 green; 1 green, 1 blue, 5 red
|
||||
Game 60: 4 red, 2 green; 1 green, 2 blue; 2 red, 2 green; 4 red, 2 green; 3 red
|
||||
Game 61: 6 red, 14 green; 12 green, 3 blue; 3 blue, 16 green, 11 red
|
||||
Game 62: 12 green, 2 red, 1 blue; 14 red, 2 blue, 4 green; 7 red, 5 green, 1 blue; 4 green, 14 red, 1 blue; 2 blue, 7 green, 3 red; 14 red, 9 green, 2 blue
|
||||
Game 63: 1 red, 8 blue, 12 green; 3 green, 10 blue, 4 red; 2 red, 1 blue, 2 green; 2 red, 8 green; 4 red, 3 blue, 9 green; 13 green, 4 blue
|
||||
Game 64: 4 blue, 9 green, 4 red; 10 green, 6 blue, 7 red; 10 green, 8 red, 1 blue; 13 blue, 8 green, 8 red; 4 green, 1 red, 8 blue
|
||||
Game 65: 16 blue, 5 red, 13 green; 5 red, 9 green, 10 blue; 2 green, 14 red; 6 red, 5 green, 5 blue; 19 blue, 4 green, 14 red; 7 red, 1 blue, 4 green
|
||||
Game 66: 7 blue, 11 green, 5 red; 7 green, 8 red, 10 blue; 3 red, 1 green
|
||||
Game 67: 9 green, 2 blue, 13 red; 11 red, 10 blue; 14 red, 1 green, 1 blue; 1 red, 13 green, 6 blue; 7 blue, 3 green, 5 red; 3 green, 2 blue
|
||||
Game 68: 6 green, 2 blue; 1 blue, 3 red, 5 green; 1 blue, 10 green
|
||||
Game 69: 4 red, 6 blue, 1 green; 6 blue, 2 red, 4 green; 9 green, 7 blue, 2 red
|
||||
Game 70: 11 blue, 1 green, 4 red; 7 blue, 1 green; 6 red, 9 blue; 1 green, 3 red, 7 blue; 1 green, 9 blue, 2 red
|
||||
Game 71: 2 green, 6 red; 1 blue, 2 green, 15 red; 6 red, 1 blue; 2 green, 5 red
|
||||
Game 72: 5 green, 6 red; 8 red, 1 blue, 12 green; 1 blue, 9 red, 15 green; 11 green, 1 blue, 6 red; 14 green, 5 red
|
||||
Game 73: 9 green, 13 red, 1 blue; 14 green, 5 blue, 13 red; 13 green, 10 red, 16 blue; 3 blue, 7 red, 1 green; 7 red, 6 green, 11 blue
|
||||
Game 74: 1 blue, 9 red, 15 green; 3 blue, 7 green; 9 green, 1 blue, 7 red
|
||||
Game 75: 5 blue, 12 red, 2 green; 2 blue, 2 green; 4 green, 2 red, 7 blue; 4 green, 7 blue, 5 red; 1 green, 7 blue, 3 red
|
||||
Game 76: 1 blue; 8 red, 13 blue; 4 green, 7 blue, 7 red; 3 red, 12 blue, 2 green; 2 green, 2 blue, 1 red
|
||||
Game 77: 4 blue; 4 blue; 1 blue, 1 red; 1 red, 5 blue; 10 blue, 1 green
|
||||
Game 78: 3 blue, 10 green, 6 red; 12 red, 7 blue, 8 green; 2 green, 18 red, 5 blue; 2 red, 15 blue, 14 green; 4 green, 6 blue, 13 red
|
||||
Game 79: 7 red, 1 green; 1 blue, 6 red, 2 green; 1 blue, 12 red
|
||||
Game 80: 4 red, 6 blue, 2 green; 5 blue, 2 red; 6 blue, 7 red, 2 green
|
||||
Game 81: 2 green, 9 red; 2 green, 2 blue, 7 red; 12 red, 7 green; 8 green, 3 red, 3 blue
|
||||
Game 82: 4 green, 5 blue; 2 red, 16 blue; 2 red, 2 green, 18 blue
|
||||
Game 83: 14 red, 2 green; 3 blue, 16 red, 2 green; 4 green, 13 red, 1 blue
|
||||
Game 84: 10 green, 6 blue, 2 red; 5 red, 6 blue; 7 green, 6 red, 9 blue
|
||||
Game 85: 1 red; 12 red, 1 blue, 2 green; 6 red, 1 green; 12 red, 2 green
|
||||
Game 86: 14 red, 1 green, 3 blue; 3 blue; 4 green, 8 red, 2 blue; 10 red, 2 green
|
||||
Game 87: 4 red, 9 green, 8 blue; 3 green, 6 blue, 7 red; 4 blue, 1 red; 2 red, 7 blue, 11 green; 8 green, 2 blue, 5 red; 6 blue, 10 green, 8 red
|
||||
Game 88: 1 green, 1 red; 2 green, 1 blue; 3 green, 1 red, 1 blue; 4 green; 1 blue, 3 green, 1 red
|
||||
Game 89: 11 green, 7 blue, 8 red; 7 blue, 3 green, 2 red; 7 green, 6 red, 4 blue; 1 blue, 2 green, 10 red; 3 red, 2 blue, 1 green
|
||||
Game 90: 10 green, 12 red, 2 blue; 7 red, 7 blue, 8 green; 2 blue, 11 red, 7 green; 6 green, 5 red, 2 blue; 7 red, 10 green
|
||||
Game 91: 12 red, 8 green; 8 red, 6 green, 3 blue; 12 red, 4 blue, 2 green
|
||||
Game 92: 10 blue, 3 green; 4 red, 13 blue, 8 green; 7 green, 8 blue, 7 red
|
||||
Game 93: 7 red, 4 green, 1 blue; 15 green, 4 red; 2 blue, 15 red
|
||||
Game 94: 10 red, 10 green, 11 blue; 3 red, 1 green, 7 blue; 9 red, 4 green, 9 blue; 7 red, 9 green, 13 blue; 9 blue, 2 green, 10 red
|
||||
Game 95: 4 blue, 12 green; 7 green, 1 red, 5 blue; 2 blue, 8 green, 8 red
|
||||
Game 96: 12 green, 2 blue; 11 green, 3 blue; 3 red, 2 green, 5 blue; 12 green, 2 blue, 4 red; 2 blue, 1 green, 1 red; 3 red, 11 green, 3 blue
|
||||
Game 97: 6 red, 3 blue, 1 green; 1 blue, 2 red, 14 green; 4 blue, 14 green
|
||||
Game 98: 13 green, 1 red, 5 blue; 2 red, 5 green, 7 blue; 19 green, 5 blue; 4 blue, 13 green; 5 green, 8 blue
|
||||
Game 99: 11 red, 8 green; 16 red, 10 green; 9 red, 6 green; 3 blue, 2 red, 4 green
|
||||
Game 100: 4 red, 2 blue, 4 green; 2 green, 1 red, 1 blue; 3 green, 4 blue, 5 red; 18 red, 2 blue; 9 red, 5 green, 4 blue
|
|
@ -0,0 +1,44 @@
|
|||
MAX = {
|
||||
red: 12,
|
||||
green: 13,
|
||||
blue: 14
|
||||
}
|
||||
|
||||
|
||||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
input.each_line do |line|
|
||||
#Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
game_num = line.split(":").first.split(" ").last.to_i
|
||||
draws = line.chomp.split(":").last.split(";").map {|e| e.split(",") }
|
||||
h = {}
|
||||
h[game_num] = draws
|
||||
data << h
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
result = 0
|
||||
data.each do |d|
|
||||
game_num = d.keys.first
|
||||
draws = d.values.first
|
||||
fits = draws_fits?(draws)
|
||||
result += game_num if fits
|
||||
puts "#{game_num} is #{fits ? "in": "out"}"
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def draws_fits?(draws)
|
||||
draws.each do |set|
|
||||
set.each do |draw|
|
||||
num, color = draw.strip.split(" ")
|
||||
return false if num.to_i > MAX[color.to_sym]
|
||||
end
|
||||
end
|
||||
|
||||
true
|
||||
end
|
|
@ -0,0 +1,34 @@
|
|||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
input.each_line do |line|
|
||||
#Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
game_num = line.split(":").first.split(" ").last.to_i
|
||||
draws = line.chomp.split(":").last.split(";").map {|e| e.split(",") }
|
||||
h = {}
|
||||
h[game_num] = draws
|
||||
data << h
|
||||
end
|
||||
data
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
result = 0
|
||||
data.each do |d|
|
||||
draws = d.values.first
|
||||
mins = {
|
||||
red: 0,
|
||||
blue: 0,
|
||||
green: 0
|
||||
}
|
||||
draws.each do |set|
|
||||
set.each do |draw|
|
||||
num, color = draw.strip.split(" ")
|
||||
mins[color.to_sym] = num.to_i if num.to_i > mins[color.to_sym]
|
||||
end
|
||||
end
|
||||
result += mins[:red] * mins[:blue] * mins[:green]
|
||||
end
|
||||
result
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
if ARGV[0] == "2"
|
||||
require_relative "impl_2"
|
||||
else
|
||||
require_relative "impl"
|
||||
end
|
||||
|
||||
|
||||
def print(result)
|
||||
if result.is_a? Array
|
||||
result.map! {|l| "#{l}\n" }
|
||||
end
|
||||
|
||||
puts result
|
||||
end
|
||||
|
||||
def run
|
||||
puts "running your implementation"
|
||||
input = File.new("day_2/data").read
|
||||
data = parse(input)
|
||||
result = calculate(data)
|
||||
print(result)
|
||||
end
|
||||
|
||||
run
|
|
@ -0,0 +1,31 @@
|
|||
if ARGV[0] == "2"
|
||||
require_relative "impl_2"
|
||||
RESULT = 2286
|
||||
else
|
||||
require_relative "impl"
|
||||
RESULT = 8
|
||||
end
|
||||
|
||||
|
||||
INPUT = <<~IN
|
||||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||
IN
|
||||
|
||||
def test_example
|
||||
parsed = parse(INPUT)
|
||||
result = calculate(parsed)
|
||||
|
||||
if result == RESULT
|
||||
puts "Test successful. Now run with real input"
|
||||
puts "ruby day_2/run.rb #{ARGV[0]}"
|
||||
else
|
||||
puts "Test failed"
|
||||
puts "expected \"#{RESULT}\" got \"#{result}\""
|
||||
end
|
||||
end
|
||||
|
||||
test_example
|
|
@ -0,0 +1,140 @@
|
|||
...............776..............552........968..................589...26...........484..............958......186....546.........484.........
|
||||
.........*.........778....................*....124...................................*...............*........%..26.........................
|
||||
......194..380....@....900..........639....467........478*..............582...........798.............326...........894.........#...........
|
||||
904...........*2.......#......259.....*..........801......464................597.569.............794+................$..218....502..........
|
||||
...*.....................-...$.....%..431.........*...810.....840+.668..........*.......144=.............................../...........%627.
|
||||
...890...497.........829.643....504..........465..502..............*........488...................787.184...601....215........-450..........
|
||||
.......................*............684.....*.............466...970....646..%...........399...........*.............=....88...........=415..
|
||||
....................139........415...+......854.....683....*.........+.=.......107.676.....*451......83..416....856.....%....765............
|
||||
..............163..............*...................*......796.....992.............*...............................#.798.....*......819......
|
||||
.../....678..*...........429..351..........937......629.................*59.677..........652$.292....................*..431..184..*......688
|
||||
.79......*...694.......-...........492.247......26.......399/.=....................113.........*...................789....*.......469.......
|
||||
.......94............667.............*..*...933...*684........861...687...567*927.*.....869..509.......................423..................
|
||||
.596............................561.271.349....-.........139.........@............278......*............@...942*....36......................
|
||||
.....475...........*268.........*...................695.................850..#.........233.876......@..768......425......85*734.......*349..
|
||||
.......*..119.96........287.....177.............940*......................*..445.........@.........577..........................39..30......
|
||||
.....26......*.......................1=.685..........3...120..81.......533...................................718................*...........
|
||||
.....................306.........335.................*......+...*..................563..240..................*.......349*708...244...319....
|
||||
..........30..197...........639......................7.../.....471.......-.......@..*..%........895*680.....635.......................*.....
|
||||
.....*116...*..*..............*...........445.............643.........75.29.....42.586..........................41.....@.....323.....860....
|
||||
.511.......105.71.......765...670.....489*.........426.........758.....................139............94.793......*....63.....*.............
|
||||
...*.....................$..................470......*..........*.....69.......590.730*...........553...........814..........604............
|
||||
.680..............................443..............598...-.....318.....=........*........845..215..*.............................-..........
|
||||
............237.......180...........#.691...276...........909...............$..527...881.*.....*...585....224....762.....389..193...........
|
||||
.....@.....=.........@....789.................*.......857........&.450...716.............149..885............*............*............504..
|
||||
993...906................*.....685..688........385...*........925..*................................700*...811..=......295.....$311.....-...
|
||||
...@.................956..809.......*..............................577....965.......%846.....$...@.............839..........................
|
||||
.........704.393%....*............1..562..............716.................$..................387.645........................571.342./.......
|
||||
......@..*.........226...........*.........526*914...*......172....-..........32............................211.......990......*....695.....
|
||||
.....272..481.................288.....................370.....*.....740...........281..............430..13..$...............................
|
||||
397............#............................65................677....................*.586..758....*......*.................49.#973....409..
|
||||
.............818.........103...........524.*.........150...........781...236.../891.....*.........456.416.591.691-...849....+...........&...
|
||||
..................457@......*...........&..813...979*.....453*181....*.....-..........286.....565.....................&.......624...........
|
||||
......#....................221..464..................................604.....978.........................................%412.*.............
|
||||
619%..176.........=...280......*......*......................493...............*.......................570+....................961..233@....
|
||||
..............52..50....*.....26......518.207....%.465.........*.............871...817.............384........792.....@754..................
|
||||
............3*.......703............%..........554..............20.........................879.......-....646*...............415...814......
|
||||
........944......946..............766.477.492......835......806...........214.........*......*..............................*......%....633.
|
||||
....266*......*.....-........................#.783*........*..................900.....852....271..+567.452..249..........850................
|
||||
...........938.661....770...87.841..................@......739../.....230.....*...916....................*..*...636..49.........18..*20..4..
|
||||
....110................*......*......#341.-.......*.75..........660..........117.....$.803@.780........172.......*....*.160/.....+..........
|
||||
....+..................92.693.............952..988..................670.....................@...972...........760..194...............24.....
|
||||
........479*...880..=........&..475&..677..........%283.....642....*............359...154................755.............690...........&....
|
||||
...../.........*.....590..=..........&.......&....................765......./...*....................419........214-.......*...324..........
|
||||
......934...470...........10.......*....-....412............750...........496..651...+701...........*....................698......*217......
|
||||
............................./228...484.854...........627....%......795.......................*.............450...66........................
|
||||
............978$...274.......................822.........*.....-760............700.........235.346................*.............+.....885...
|
||||
...................%...582..........338.@.....*...274..252.....................*...720....................700.18.41...........503.......+...
|
||||
........4..960....................../....722..897.&..........987..........510..104....+..416.....549.......*...*...................407......
|
||||
.......*......*211............=725...................$574.......*.........................+......*.......788..350...432..............*......
|
||||
.244...921.........601....36*..................222..............918...344..733.....109.........610....66..............*...............937...
|
||||
...*.........464...*.../.....991................*....................+....*....285*......538..........*.......914......526...465.722........
|
||||
............*.....362.460........49............66.121...=...............373.............=.........530..337....=.................*........606
|
||||
154...713...611............$725....................*..29...425..74..............920.................@.................389...........715.....
|
||||
......*........../.....143.......................652.........*..*..711...69.26........406.....................37*175........122.....#.......
|
||||
.....588........39....*......224.........*................868..800...&../...*....407.......46..387.728......................*...............
|
||||
............-........39.........*.541.595.516...............................484.*.................*...........206.........636..807..........
|
||||
.............858.............716....*..........810*290..372........@949.........921...................428........*..+..........*............
|
||||
........486........301*307.........135.................*................264.178......838.....224..-......*......499..816.......227..........
|
||||
.......*........................................802...608............24*.......*.........940......98....960............................685..
|
||||
.......425.76.........+94..556...........699.......*.......................%....857.266.*.....................8................624*....=....
|
||||
............*.................*735......&....$..345....361%.803..856....732.........*....520....*..............%.......269...=.....286......
|
||||
...........73.901.../..............384.....520......#.........-./...........447......930......279.......634.............#..207.754......473.
|
||||
..............@...538.936*484.....*....665...........630...............236...@.....%.....................*...985..406............-..........
|
||||
..............................328..531...%.616...........................*........64..497..105........844.....*......*......207.............
|
||||
.....*730......................*.......................478..701........+..567........*...........110.......175.......6.737.......+....192...
|
||||
..363......119....6....942......892....171..358.981..........*........731.........455....#..........*993.......383......*.........505..@....
|
||||
..............*....%.%....*............*......*..../..%.....................995........850...264..........82%.....=......735.../............
|
||||
..207...413/.349.@...628.685.........930......231....921.......................................$..142...........-............713....260.....
|
||||
.................384.........527..........373.............@946....&...............948...949......*......349...838......................*....
|
||||
.......................210....*.....................*241.........333.......................*..723........*..........632....916-...164..63...
|
||||
....789....../..859......@....770............286.881.................867.........218......346.........691..368......*.......................
|
||||
......$...411.....@.781............857................*912.135.&27...........885....*.942.........%............436..264.449.577..92#........
|
||||
...............9....................*..............391.....*................*......54..=.........108.......................*................
|
||||
..............$..106........767..290...751................977............990................499...................466.........545...........
|
||||
...............................=..........*..................................345............./....=.../...266.847....*605....*....219.......
|
||||
..................984.....735..........761......727.........814.481.....737...*......936...........76.599.*....=...........247...=..........
|
||||
.....................*697...*.595.................=...........*........*.......480.........................822.....287..................941.
|
||||
.....182*875...477#..............*...108...................390.......266...........678.593.......-..............$....&.649...../..../...*...
|
||||
....................279..............*...136........@..........................513.=...*.........332...510.....337............534..236..101.
|
||||
.......................*538........453......=.......390....235...................*......741............*.................792@...............
|
||||
..492......639.650.........................................@.....................21........./.......170............................+..@.....
|
||||
...*..942..=................794.........@..........456............185.395....437...........721..........206..+231...............638...424...
|
||||
479......*....................*.........251....243*....387........=..................520.............$...../................................
|
||||
..........971.................995.........................................116.......-.....*950...*....731.....543.......+..............748..
|
||||
......522................852=..............380..848......932........482......*644......514........40.........#..........869........710..-...
|
||||
.........*......................#955......./...*....../.....*46.......*.160#.................652..................218..........478*.........
|
||||
.........870...254.........309.......106......214....840..#.........29.......%........111.....$.........20*865..........&.............694...
|
||||
..................*316........*323......*................734.881.......33...84.97$..4*..........*135...............772.993............*.....
|
||||
.......................208...............793.....157............*597....*....................818.........-...260.....*........933....384....
|
||||
..........106......34......358*263.................+...457*...$.......866.583....................439=..677......@................=..........
|
||||
......-...*........#.......................................87.68...............#.......186%..................&.....=626.....................
|
||||
...191...136..453*.............#../537...............%.612........89..417......50.........................429..................859..........
|
||||
..................432....*89.20...................387..../....../.......*.........121...108...................................&.............
|
||||
...........768........................134...793................339....206.........*......$.......394.......@.........974..........132*863...
|
||||
......./...*................252.355.....#......................................335...169...332...*.......896....894..*......................
|
||||
....629....191..........369*.................109.............326..312*....&...........*.....*....478...........*......432...&......104......
|
||||
670............................&....967*........*.....................516..92..855...685.712..............936...............88....$.........
|
||||
.....583......974....26*24......782.....806...160...............669...............@..........606*........%......89..........................
|
||||
786.............*................................................&..................=............742...........+...................%........
|
||||
......#....@.939......375..............332-..........575...546*........*....196.628.581....@...............288.........645...467.246..125...
|
||||
....46...872.....414.....*..=......+........677................342..883.301..*...@.........699.261...796......*....809....-..*.......*......
|
||||
................$.....828....940..400.........$.40............................39.........*..............=.......-....*........780....925....
|
||||
.....*375.................39.............418....*................................../..279.367../676..........982.....216....=...............
|
||||
..703......$....../............703*.......*..904.....465.........*182..92.........173...............610...65................685.....55$.....
|
||||
.........904...650.................274...6.....................&.........*635...............787.306*.......*.............*..................
|
||||
..136........................................933......783.724.320...344..................../...........+....51...108..597.41.....809........
|
||||
...+........715.663............$.....821.....*...........*............*.....@657..354..411..........811..........*..........................
|
||||
......633.....*............*...925.........991...852........=...=......184..........*.........589.................788...191.................
|
||||
.........*....574........368.....................@.......373..657.701........411..575.954&......*.......$.....748.........*....758..........
|
||||
........787........770.............................879............*......88....*..............119.....817.....*.........914....*......751...
|
||||
.14.173...............*........................189*................509..%......29.............................87................683..*......
|
||||
...*........../.....563..270*876...123................................................../64.....216........................977......316.....
|
||||
.......612..516.50...................*................762..23/.........26.........608.............$..466*214.......455...#..#..352......928.
|
||||
........*.........*......296..@.......970..513.........*.........584..................233.....945................./.....846...+.............
|
||||
......918.......640..585.....600..550.....*.......=....723.......*.............-.......-..418.....15.180...969.......................703....
|
||||
...@.......-63...........=........*.......839...769............76........671...268........+..........*.........965....#.....#.568...*.......
|
||||
.194.....................453......530...................*675......487.=....+.........309....224..507.209.........@.138....300..*..$.........
|
||||
.......@..@....846.............%.........714...468...150......88......510..............*...*.......*...........................9.816........
|
||||
.....234.140....*..............903..789.....*.*..............*..............631........702.80....33.........513........276..................
|
||||
.............381.......................*.803...474......267...296.436.......*........................679....*............*........939...784.
|
||||
.....-..........................33&.158....................*......*.......569........487...871/............510......274.542.........*.......
|
||||
......883.....*163...782..-.............379.764@.....912=..796...91.....................&.......979....907.....*......*........167....+.+873
|
||||
...........110........*..577........&....*............................*778...................$............@.241.428..949...680*.....513.....
|
||||
....................924........471.470.268..486......../....#......567...........470..469.794....%...............................+..........
|
||||
..725*532......$........@.......*..........*........926......213............405.....*...*......355...615...#....73........151.....896.......
|
||||
............980......969....812..28.936.....882..............................&...107.....377...........*.611.......+.......&..737...........
|
||||
....$594...................*............156.....210....585...........234.821.........................403........837..........*....652.......
|
||||
...........................247.@881..7.....*....*.....................*.....*.......*........140.351..........................510....*......
|
||||
...........790*776...................*...270...778......707.110.....487..486...460.70........*.....#...585.702..430..141...........34.......
|
||||
...52.......................&216..788......................*...................*............199...........*....=......................989...
|
||||
....$..445...498.254.508*.............*860..711.48......./.............596....305.....618..........337.............198....287...............
|
||||
.........#....*....@.....352....*..645......*.............328......50.*..................=...-........*584..........*..........*275..986*527
|
||||
.............560..............815.........931......400............*...592......824.........807...591*................311.....82.............
|
||||
.........%.......678.926........................19...*......545..649...........*...............*.....393....=896............................
|
||||
......834...........*............................/....386.....*......=290...772.............545.969...................679.820......704......
|
||||
..........122...........202....693.................*..............................555*826..............458......691......*.........*........
|
||||
....#.......&..%692........+......#..333............41...............=204..................868............/.164..................316........
|
||||
...346..................+.......$...*......396..315....119..@755...............942..918........................*........631.651.............
|
||||
.................=....836......402.755............=.......*.......*....181.62...*.....%........................596......*............#591...
|
||||
...............764.....................................488.....880.209........416..................183...156..........917...................
|
|
@ -0,0 +1,82 @@
|
|||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
row = 0
|
||||
symbols = {}
|
||||
numbers = []
|
||||
input.each_line do |line|
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
|
||||
line.chomp.chars.each_with_index do |c,col|
|
||||
if c == "."
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
elsif /\d/.match?(c)
|
||||
# a digit
|
||||
start_num = true
|
||||
num += c
|
||||
num_cols << col
|
||||
else
|
||||
# symbol
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
# fill adjacents
|
||||
symbols[row] = {} if symbols[row].nil?
|
||||
symbols[row][col-1] = true
|
||||
symbols[row][col+1] = true
|
||||
symbols[row-1] = {} if symbols[row-1].nil?
|
||||
symbols[row-1][col] = true
|
||||
symbols[row-1][col-1] = true
|
||||
symbols[row-1][col+1] = true
|
||||
symbols[row+1] = {}if symbols[row+1].nil?
|
||||
symbols[row+1][col] = true
|
||||
symbols[row+1][col-1] = true
|
||||
symbols[row+1][col+1] = true
|
||||
end
|
||||
end
|
||||
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
row += 1
|
||||
end
|
||||
data = [symbols,numbers]
|
||||
data
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
result = ""
|
||||
symbols, numbers = data
|
||||
selected_numbers = []
|
||||
numbers.each do |number_set|
|
||||
number, row, cols = number_set
|
||||
cols.each do |col|
|
||||
if symbols.dig(row,col)
|
||||
selected_numbers << number
|
||||
puts "selected: #{selected_numbers}"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result = selected_numbers.reduce(&:+)
|
||||
|
||||
result
|
||||
end
|
|
@ -0,0 +1,105 @@
|
|||
#prepare the input which is a string containing new lines
|
||||
def parse(input)
|
||||
data = []
|
||||
row = 0
|
||||
symbols = []
|
||||
numbers = []
|
||||
input.each_line do |line|
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
|
||||
line.chomp.chars.each_with_index do |c,col|
|
||||
if c == "."
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
elsif /\d/.match?(c)
|
||||
# a digit
|
||||
start_num = true
|
||||
num += c
|
||||
num_cols << col
|
||||
elsif c == "*"
|
||||
# symbol
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
# fill adjacents
|
||||
|
||||
symbols << [row,col]
|
||||
else
|
||||
# other symbol
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if start_num
|
||||
# finish a number
|
||||
numbers << [num.to_i, row, num_cols]
|
||||
start_num = false
|
||||
num = ""
|
||||
num_cols = []
|
||||
end
|
||||
row += 1
|
||||
end
|
||||
data = [symbols,numbers]
|
||||
data
|
||||
end
|
||||
|
||||
def find_adjacent_numbers(row,col,numbers)
|
||||
adjacents = []
|
||||
matches = [
|
||||
[row-1,col-1],
|
||||
[row-1,col,],
|
||||
[row-1,col+1],
|
||||
[row,col-1],
|
||||
[row,col+1],
|
||||
[row+1,col-1],
|
||||
[row+1,col],
|
||||
[row+1,col+1]
|
||||
]
|
||||
numbers.each do |number|
|
||||
num, row, cols = number
|
||||
cols.each do |col|
|
||||
if matches.include? [row,col]
|
||||
adjacents << num
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
adjacents
|
||||
end
|
||||
|
||||
# result should a single string or integer
|
||||
def calculate(data)
|
||||
result = ""
|
||||
symbols, numbers = data
|
||||
selected_numbers = []
|
||||
symbols.each do |symbol|
|
||||
row,col = symbol
|
||||
adj = find_adjacent_numbers(row,col,numbers)
|
||||
if adj.length == 2
|
||||
selected_numbers << adj.first * adj.last
|
||||
end
|
||||
end
|
||||
|
||||
result = selected_numbers.reduce(&:+)
|
||||
|
||||
result
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
if ARGV[0] == "2"
|
||||
require_relative "impl_2"
|
||||
else
|
||||
require_relative "impl"
|
||||
end
|
||||
|
||||
|
||||
def print(result)
|
||||
if result.is_a? Array
|
||||
result.map! {|l| "#{l}\n" }
|
||||
end
|
||||
|
||||
puts result
|
||||
end
|
||||
|
||||
def run
|
||||
puts "running your implementation"
|
||||
input = File.new("day_3/data").read
|
||||
data = parse(input)
|
||||
result = calculate(data)
|
||||
print(result)
|
||||
end
|
||||
|
||||
run
|
|
@ -0,0 +1,36 @@
|
|||
if ARGV[0] == "2"
|
||||
require_relative "impl_2"
|
||||
RESULT = 467835
|
||||
else
|
||||
require_relative "impl"
|
||||
RESULT = 4361
|
||||
end
|
||||
|
||||
|
||||
INPUT = <<~IN
|
||||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
IN
|
||||
|
||||
def test_example
|
||||
parsed = parse(INPUT)
|
||||
result = calculate(parsed)
|
||||
|
||||
if result == RESULT
|
||||
puts "Test successful. Now run with real input"
|
||||
puts "ruby day_3/run.rb #{ARGV[0]}"
|
||||
else
|
||||
puts "Test failed"
|
||||
puts "expected \"#{RESULT}\" got \"#{result}\""
|
||||
end
|
||||
end
|
||||
|
||||
test_example
|
Loading…
Reference in New Issue