-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.rb
151 lines (135 loc) · 9.79 KB
/
11.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
LAYOUT = DATA.each_line.map { |row| row.strip.split '' }
SLOPES = [-1, 0, 1].repeated_permutation(2).to_a - [[0, 0]]
ROWS = LAYOUT.size
COLS = LAYOUT[0].size
SEATS = (0...ROWS).to_a.product((0...COLS).to_a).reject { |r, c| LAYOUT[r][c] == ?. }
def in_layout? r, c
(0...ROWS) === r && (0...COLS) === c
end
def count_occupied min_no_of_occupied, &look_for_seats
layout = LAYOUT.clone.map &:clone
next_layout = nil
loop do
next_layout = layout.clone.map &:clone
SEATS.each do |r, c|
seat = layout[r][c]
no_of_occupied = look_for_seats.call(layout, r, c).count ?#
if seat == ?L && no_of_occupied.zero?
next_layout[r][c] = ?#
elsif seat == ?# && no_of_occupied >= min_no_of_occupied
next_layout[r][c] = ?L
end
end
break if layout == next_layout
layout.replace next_layout
end
layout.flatten.count ?#
end
occupied = count_occupied 4 do |layout, r, c|
SLOPES
.map { |dr, dc| [r + dr, c + dc] }
.filter_map { |r, c| layout[r][c] if in_layout? r, c }
end
puts occupied # 2211
occupied = count_occupied 5 do |layout, r, c|
SLOPES.filter_map do |dr, dc|
(1..)
.lazy
.map { |n| [r + n * dr, c + n * dc] }
.take_while { |r, c| in_layout? r, c }
.map { |r, c| layout[r][c] }
.find { |s| s =~ /L|#/ }
end
end
puts occupied # 1995
__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