parent
453521de55
commit
c96b0c667e
@ -0,0 +1,61 @@ |
||||
require "./common.cr" |
||||
require "big" |
||||
|
||||
lines = File.read("day12_input").split "\n" |
||||
lines.pop |
||||
|
||||
initial = lines[0].split(": ")[1].chars |
||||
original = initial.join("") |
||||
rules = lines[2..-1].map do |it| |
||||
split = it.split " => " |
||||
line = split[0] |
||||
into = split[1][0] |
||||
{ line.chars, into } |
||||
end |
||||
|
||||
class Array |
||||
def has_plant(i) |
||||
return false if i < 0 || i >= size |
||||
return self[i] == '#' |
||||
end |
||||
end |
||||
|
||||
def step(initial, rules) |
||||
array = [] of Char |
||||
(-2..initial.size + 2).each do |i| |
||||
rule = [] of Char |
||||
(i-2..i+2).each do |ind| |
||||
rule << (initial.has_plant(ind) ? '#' : '.') |
||||
end |
||||
|
||||
new_char = rules.find(&.[0].==(rule)).try(&.[1]) || '.' |
||||
array << new_char |
||||
end |
||||
return array |
||||
end |
||||
|
||||
def get_total(initial, offset) |
||||
total = 0 |
||||
initial.each_with_index do |char, i| |
||||
total += (char == '#') ? (i + offset) : 0 |
||||
end |
||||
return total |
||||
end |
||||
|
||||
offset = 0 |
||||
|
||||
20.times do |it| |
||||
initial = step(initial, rules) |
||||
offset -= 2 |
||||
end |
||||
puts get_total(initial, offset) |
||||
|
||||
total = BigInt.new("0") |
||||
fivebil = BigInt.new("50000000000") |
||||
first_index = (fivebil - 11) |
||||
|
||||
total += first_index * 57 |
||||
total += get_total("###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#".chars, 0) |
||||
puts total |
||||
|
||||
# 2850000002508 |
Loading…
Reference in new issue