aboutsummaryrefslogtreecommitdiff
path: root/day12/part1
blob: ac3f08798bf066f0f6bb2e73b3b05024e63e4054 (plain)
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
#!/usr/bin/env ruby

input = $stdin.readlines.map(&:chomp)

pipes = []

input.each do |line|
  me, them = line.split(' <-> ')
  me = me.to_i
  them = them.split(', ').map(&:to_i)
  
  pipes[me] = [] if pipes[me].nil?
  them.each do |p|
    pipes[me] << p
  end
end

group = [0]
visited = []
queue = [0]

until queue.empty? do
  here = queue.shift
  visited << here
  pipes[here].each do |p|
    group << p
    queue << p unless visited.include?(p)
  end
end

p group.uniq.length