-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday04.rb
25 lines (23 loc) · 885 Bytes
/
day04.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
nums, *cards = File.read('day04_input.txt').split("\n\n")
nums = nums.split(',').map(&:to_i)
cards = cards.map {|c| (c.lines.map {|l| l.scan(/\d+/).map(&:to_i) }) }
cards = cards.map {|c| [c, c.transpose] }
cp1 = Marshal.load(Marshal.dump(cards))
nums.each do |n|
break if cp1.any? {|c| c.any? {|t| t.any? {|l| l.empty? } } }
cp1.each {|c| c.each {|t| t.each {|l| l.delete(n) } } }
$n = n
end
p $n*cp1.filter {|c| c.any? {|t| t.any? {|l| l.empty? } } }[0][0].flatten.sum
nums.each do |n|
break if cards.count {|c| c.all? {|t| t.all? {|l| !l.empty? } } } <= 1
cards.each {|c| c.each {|t| t.each {|l| l.delete(n) } } }
$n = n
end
c = cards.filter {|c| c.all? {|t| t.all? {|l| !l.empty? } } }[0]
nums.each do |n|
break if c.any? {|t| t.any? {|l| l.empty? } }
cards.each {|c| c.each {|t| t.each {|l| l.delete(n) } } }
$n = n
end
p $n*c[0].flatten.sum