Skip to content

Commit 03b2dbf

Browse files
committed
Day 23: LAN Party
1 parent 3963b2e commit 03b2dbf

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

lib/2024/23.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
NETWORK = INPUT.split("\n").each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |line, network|
2+
computer, connected = line.split("-")
3+
4+
network[computer] << connected
5+
network[connected] << computer
6+
end
7+
8+
def cliques(computer, network)
9+
network[computer].to_a.combination(2).select do |a, b|
10+
network[a].include?(b)
11+
end
12+
end
13+
14+
parties = NETWORK.keys.each_with_object(Set.new) do |computer, lans|
15+
cliques(computer, NETWORK).each { |peers| lans << Set.new([computer, *peers]) }
16+
end
17+
matches = parties.count { |party| party.any? { |cpu| cpu.start_with?("t") } }
18+
19+
solve!("The number of groups containing a 't' computer is:", matches)
20+
21+
def largest_clique(network, clique = Set.new, ignore = Set.new)
22+
return clique if network.empty? && ignore.empty?
23+
24+
network.keys.map do |computer|
25+
largest = largest_clique(
26+
network.slice(*network[computer]),
27+
clique + [computer],
28+
ignore.intersection(network[computer])
29+
)
30+
31+
network.delete(computer)
32+
ignore << computer
33+
34+
largest
35+
end.reject(&:nil?).max_by(&:size)
36+
end
37+
38+
max_party = largest_clique(NETWORK.dup)
39+
solve!("The LAN party password is:", max_party.sort.join(","))

spec/2024/23_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "spec_helper"
2+
3+
RSpec.describe "Day 23: LAN Party" do
4+
let(:runner) { Runner.new("2024/23") }
5+
let(:input) do
6+
<<~TXT
7+
kh-tc
8+
qp-kh
9+
de-cg
10+
ka-co
11+
yn-aq
12+
qp-ub
13+
cg-tb
14+
vc-aq
15+
tb-ka
16+
wh-tc
17+
yn-cg
18+
kh-ub
19+
ta-co
20+
de-co
21+
tc-td
22+
tb-wq
23+
wh-td
24+
ta-ka
25+
td-qp
26+
aq-cg
27+
wq-ub
28+
ub-vc
29+
de-ta
30+
wq-aq
31+
wq-vc
32+
wh-yn
33+
ka-de
34+
kh-ta
35+
co-tc
36+
wh-qp
37+
tb-vc
38+
td-yn
39+
TXT
40+
end
41+
42+
describe "Part One" do
43+
let(:solution) { runner.execute!(input, part: 1) }
44+
45+
it "counts the sets of potential computers" do
46+
expect(solution).to eq(7)
47+
end
48+
end
49+
50+
describe "Part Two" do
51+
let(:solution) { runner.execute!(input, part: 2) }
52+
53+
it "finds the largest group of mutual peers" do
54+
expect(solution).to eq("co,de,ka,ta")
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)