From c96b0c667e7a9b6600570e1556141852a1d35a1a Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 11 Dec 2018 22:18:14 -0800 Subject: [PATCH] Add day 12 solution. --- day12.cr | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 day12.cr diff --git a/day12.cr b/day12.cr new file mode 100644 index 0000000..766e124 --- /dev/null +++ b/day12.cr @@ -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