-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22_part2.rb
56 lines (49 loc) · 1.36 KB
/
day22_part2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#1419 - #1428 = 9 minutes = +- #60
class Node
attr_accessor :name, :size, :used, :avail, :use, :x, :y
def initialize(name, size, used, avail, use)
@name = name
name.match(/node-x(\d+)-y(\d+)/)
@x = $1.to_i
@y = $2.to_i
@size = size
@used = used
@avail = avail
@use = use
end
def self.parse(line)
puts line.strip
name, size, used, avail, use = line.strip.split(/\s+/)
Node.new(name, size.gsub("T", "").to_i, used.gsub("T","").to_i, avail.gsub("T","").to_i, use.gsub("%","").to_i)
end
end
def process(lines)
nodes = lines.map{|l|Node.parse(l)}
# nodes.each {|n|puts [n.x,n.y].join(",")}
moveable = nodes.permutation(2).select{|(a,b)| a.used != 0 && a.used <= b.avail}.flatten.select{|n|n.used != 0}
free = nodes.find{|n|n.used == 0}
barrier = nodes.select{|n|n.used != 0 && !moveable.include?(n)}
max_x = nodes.sort_by{|n|n.x}.last.x
max_y= nodes.sort_by{|n|n.y}.last.y
grid = Array.new(max_y+1){Array.new(max_x+1, nil)}
puts max_x
puts max_y
nodes.each do |node|
grid[node.y][node.x] = node
end
grid.map!{|row| row.map do |node|
if moveable.include?(node)
"."
elsif barrier.include?(node)
"#"
elsif free == node
"_"
else
"?"
end
end
}
puts grid.map{|row|row.join}.join("\n")
#manual solve
end
process(File.new("day22_input.txt").readlines.drop(2))