-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.rb
68 lines (54 loc) · 1.75 KB
/
23.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
#############################################################################
# 1
#############################################################################
connections = File.readlines('input23.txt').map { |line| [line[0..1], line[3..4]] }
connection_map = Hash.new { |h, k| h[k] = Set.new }
connections.each do |c1, c2|
connection_map[c1] << c2
connection_map[c2] << c1
end
parties = []
connections.each do |comp1, comp2|
found = false
parties.each do |party|
party_ok = party.all? do |item|
(item == comp1 || connection_map[item].include?(comp1)) && (item == comp2 || connection_map[item].include?(comp2))
end
if party_ok
party << comp1 << comp2
found = true
end
end
parties << Set.new([comp1, comp2]) unless found
end
sets_by_3 = Set.new
parties.each do |party|
party.to_a.combination(3).each do |c|
sets_by_3 << Set.new(c) if c.any? { |comp| comp.start_with?('t') }
end
end
puts sets_by_3.count
#############################################################################
# 2
#############################################################################
connections = File.readlines('input23.txt').map { |line| [line[0..1], line[3..4]] }
connection_map = Hash.new { |h, k| h[k] = Set.new }
connections.each do |c1, c2|
connection_map[c1] << c2
connection_map[c2] << c1
end
parties = []
connections.each do |comp1, comp2|
found = false
parties.each do |party|
party_ok = party.all? do |item|
(item == comp1 || connection_map[item].include?(comp1)) && (item == comp2 || connection_map[item].include?(comp2))
end
if party_ok
party << comp1 << comp2
found = true
end
end
parties << Set.new([comp1, comp2]) unless found
end
puts parties.max_by(&:count).to_a.sort.join(',')