parent
794e3db35a
commit
5d7fc5b308
@ -1,14 +1,30 @@
|
|||||||
|
require "prime"
|
||||||
|
|
||||||
instructions, network = ARGF.read.split("\n\n")
|
instructions, network = ARGF.read.split("\n\n")
|
||||||
|
|
||||||
network = network
|
network = network
|
||||||
.scan(/(\w+) = \((\w+), (\w+)\)/)
|
.scan(/(\w+) = \((\w+), (\w+)\)/)
|
||||||
.to_h { [_1, { L: _2, R: _3 }] }
|
.to_h { [_1, { L: _2, R: _3 }] }
|
||||||
|
|
||||||
node = "AAA"
|
steps = ->(start, finish) {
|
||||||
p instructions.chars.map(&:to_sym)
|
node = start
|
||||||
.cycle.with_index.lazy
|
instructions.chars.map(&:to_sym)
|
||||||
.filter_map {|dir, i|
|
.cycle.with_index.lazy
|
||||||
node = network.fetch(node).fetch(dir)
|
.filter_map {|dir, i|
|
||||||
(node == "ZZZ") ? i+1 : nil
|
node = network.fetch(node).fetch(dir)
|
||||||
}
|
(node =~ finish) ? i+1 : nil
|
||||||
.first
|
}
|
||||||
|
.first
|
||||||
|
}
|
||||||
|
|
||||||
|
# part one
|
||||||
|
p steps.("AAA", /ZZZ/)
|
||||||
|
|
||||||
|
# part two
|
||||||
|
nodes = network.keys.select { _1.end_with?(?A) }
|
||||||
|
node_steps = nodes.map { steps.(_1, /Z$/) }
|
||||||
|
p node_steps
|
||||||
|
.map { Prime.prime_division(_1).to_h }
|
||||||
|
.inject {|acc, fac| acc.merge(fac) {|_,old,new| [old, new].max}}
|
||||||
|
.map { _1**_2 }
|
||||||
|
.inject(:*)
|
||||||
|
Loading…
Reference in new issue