Code for the Advent of Code event
aoc
advent-of-code
1#!/usr/bin/env ruby
2# frozen_string_literal: true
3
4input = ARGF.readlines
5
6map = {}
7
8input.each do |line|
9 next unless line =~ /(\d+)[^\d]+([\d, ]+)/
10 reachable = map[$1.to_i] ||= Set.new
11 $2.split(',').map(&:to_i).each { |t| reachable.add t }
12end
13
14visited = Set.new
15tovisit = [0]
16while tovisit.any?
17 current = tovisit.pop
18 visited.add current
19 reachable = map[current].reject { |n| visited.include? n }
20 tovisit += reachable.to_a
21end
22
23puts visited.size
24
25puts 1 + (map.keys.count { |start|
26 if visited.include? start
27 false
28 else
29 tovisit = [start]
30 while tovisit.any?
31 current = tovisit.pop
32 visited.add current
33 reachable = map[current].reject { |n| visited.include? n }
34 tovisit += reachable.to_a
35 end
36
37 true
38 end
39})