diff options
author | Nat Lasseter <user@4574.co.uk> | 2019-10-22 00:18:37 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2019-10-22 00:18:37 +0100 |
commit | ede81a630c8f67cad53ce17af86bdf4fc1ff9307 (patch) | |
tree | 61f23e98570e959c2fb593c74e4c955d1f9b28fd /day12/part2 | |
parent | 5534d4e4054f03ac74ff1ab0bd9811aebd5a9aea (diff) |
Day 12, part1 only
Diffstat (limited to 'day12/part2')
-rwxr-xr-x | day12/part2 | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/day12/part2 b/day12/part2 new file mode 100755 index 0000000..e1af0ed --- /dev/null +++ b/day12/part2 @@ -0,0 +1,69 @@ +#!/usr/bin/env ruby + +input = $stdin.readlines.map(&:strip) + +is = input[0].split(" ")[2] +rs = input[2..-1] + +class Plants + def initialize(initial_state, rules) + @state = Hash.new(false) + parse_state_string(initial_state) + @rules = [] + parse_rules_lines(rules) + end + + def step(t = 1) + t.times do + newstate = Hash.new(false) + + min = @state.keys.min + max = @state.keys.max + ((min-5)..(max+5)).each do |i| + res = false + @rules.each do |r| + ires = true + (-2..2).each do |o| + if @state[i + o] != r[o + 2] then + ires = false + end + end + if ires then + res = true + end + end + if res then + newstate[i] = true + end + end + + @state = newstate + end + end + + def sum + @state.select { |_, s| s }.keys.sum + end + + private + def parse_state_string(state) + state.chars.each_with_index do |s, i| + @state[i] = s == "#" + end + end + + def parse_rules_lines(rules) + rules.each do |rule| + c, r = rule.split(" => ") + if r == "#" then + @rules << c.chars.map { |p| p == "#" } + end + end + end +end + +m = Plants.new(is, rs) + +m.step(50000000000) + +puts m.sum |