-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.rb
More file actions
90 lines (76 loc) · 1.5 KB
/
3.rb
File metadata and controls
90 lines (76 loc) · 1.5 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'strscan'
require 'pry'
class Cell
def initialize
claims = []
end
def add_claim(claim)
claims << claim
end
def claim_count
claims.length
end
def to_s
claim_count
end
private
attr_reader :claims
end
class Sheet
def initialize(x, y, initial = 0)
@matrix = Array.new(y) { (Array.new(x) { Cell.new }) }
end
def to_s
s = ''
@matrix.each do |row|
row.each do |cell|
s << cell.to_s + " "
end
s << "\n"
end
s
end
def apply_claim(claim)
rows = @matrix[claim.y_offset, claim.height]
rows.each do |row|
row.fill(claim.x_offset, claim.width) {|i| row[i].add_claim }
end
end
def count
count = 0
@matrix.each do |row|
row.each do |cell|
if yield(cell)
count += 1
end
end
end
count
end
end
class Claim
attr_reader :order, :x_offset, :y_offset, :width, :height
def initialize(claim)
ss = StringScanner.new(claim)
ss.skip(/#/)
@order = ss.scan(/\d+/).to_i
ss.skip(/ @ /)
@x_offset = ss.scan(/\d+/).to_i
ss.skip(/,/)
@y_offset = ss.scan(/\d+/).to_i
ss.skip(/: /)
@width = ss.scan(/\d+/).to_i
ss.skip(/x/)
@height = ss.scan(/\d+/).to_i
end
def to_s
"##{order} @ #{x_offset},#{y_offset}: #{width}x#{height}"
end
end
input = File.read("./3.input")
fabric = Sheet.new(1000,1000)
input.each_line do |line|
claim = Claim.new(line)
fabric.apply_claim(claim)
end
puts fabric.count {|value| value > 1 }