[2023][ruby][16.2] shameless green

main
Alpha Chen 11 months ago
parent b7a02fda85
commit efda4ecb16
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -3,45 +3,59 @@ input = ARGF.readlines(chomp: true)
row.chars.map.with_index {|elem, x| [[y,x], elem] } row.chars.map.with_index {|elem, x| [[y,x], elem] }
}.to_h }.to_h
current = [[[0,0], [0,1]]] count_energized = ->(start, dir) {
seen = Set.new current = [[start, dir]]
seen = Set.new
until current.empty?
coord, delta = current.shift until current.empty?
seen << [coord, delta] coord, delta = current.shift
seen << [coord, delta]
case [elem = input[coord], delta]
in [nil, _] case [elem = input[coord], delta]
# the beam has escaped the contraption in [nil, _]
in [?., _] | [?|, [_,0]] | [?-, [0,_]] # keep going # the beam has escaped the contraption
current << [coord.zip(delta).map { _1 + _2 }, delta] in [?., _] | [?|, [_,0]] | [?-, [0,_]] # keep going
in [?|, [0,_]] # split up and down current << [coord.zip(delta).map { _1 + _2 }, delta]
current << [coord.zip([-1,0]).map { _1 + _2 }, [-1,0]] in [?|, [0,_]] # split up and down
current << [coord.zip([1,0]).map { _1 + _2 }, [1,0]] current << [coord.zip([-1,0]).map { _1 + _2 }, [-1,0]]
in [?-, [_,0]] # split left and right current << [coord.zip([1,0]).map { _1 + _2 }, [1,0]]
current << [coord.zip([0,-1]).map { _1 + _2 }, [0,-1]] in [?-, [_,0]] # split left and right
current << [coord.zip([0,1]).map { _1 + _2 }, [0,1]] current << [coord.zip([0,-1]).map { _1 + _2 }, [0,-1]]
in [?/, [0,1]] # going right, goes up current << [coord.zip([0,1]).map { _1 + _2 }, [0,1]]
current << [coord.zip([-1,0]).map { _1 + _2 }, [-1,0]] in [?/, [0,1]] # going right, goes up
in [?/, [0,-1]] # going left, goes down current << [coord.zip([-1,0]).map { _1 + _2 }, [-1,0]]
current << [coord.zip([1,0]).map { _1 + _2 }, [1,0]] in [?/, [0,-1]] # going left, goes down
in [?/, [1,0]] # going down, goes left current << [coord.zip([1,0]).map { _1 + _2 }, [1,0]]
current << [coord.zip([0,-1]).map { _1 + _2 }, [0,-1]] in [?/, [1,0]] # going down, goes left
in [?/, [-1,0]] # going up, goes right current << [coord.zip([0,-1]).map { _1 + _2 }, [0,-1]]
current << [coord.zip([0,1]).map { _1 + _2 }, [0,1]] in [?/, [-1,0]] # going up, goes right
in [?\\, [0,1]] # going right, goes down current << [coord.zip([0,1]).map { _1 + _2 }, [0,1]]
current << [coord.zip([1,0]).map { _1 + _2 }, [1,0]] in [?\\, [0,1]] # going right, goes down
in [?\\, [0,-1]] # going left, goes up current << [coord.zip([1,0]).map { _1 + _2 }, [1,0]]
current << [coord.zip([-1,0]).map { _1 + _2 }, [-1,0]] in [?\\, [0,-1]] # going left, goes up
in [?\\, [1,0]] # going down, goes right current << [coord.zip([-1,0]).map { _1 + _2 }, [-1,0]]
current << [coord.zip([0,1]).map { _1 + _2 }, [0,1]] in [?\\, [1,0]] # going down, goes right
in [?\\, [-1,0]] # going up, goes left current << [coord.zip([0,1]).map { _1 + _2 }, [0,1]]
current << [coord.zip([0,-1]).map { _1 + _2 }, [0,-1]] in [?\\, [-1,0]] # going up, goes left
else current << [coord.zip([0,-1]).map { _1 + _2 }, [0,-1]]
fail "unexpected element: #{elem.inspect}" else
fail "unexpected element: #{elem.inspect}"
end
current = current.reject { seen.include?(_1) }
end end
current = current.reject { seen.include?(_1) } seen.map(&:first).select { input.has_key?(_1) }.uniq.size
end }
max_y = input.keys.map(&:first).max
max_x = input.keys.map(&:last).max
candidates = [
(0..max_x).map { [[0,_1], [1,0]] }, # down
(0..max_x).map { [[max_y,_1], [-1,0]] }, # up
(0..max_y).map { [[_1,0], [0,1]] }, # right
(0..max_y).map { [[_1,max_x], [0,-1]] }, # left
].inject(:+)
p seen.map(&:first).select { input.has_key?(_1) }.uniq.size p candidates.map { count_energized[*_1] }.max

Loading…
Cancel
Save