aboutsummaryrefslogtreecommitdiff
path: root/day06/part2
blob: f7fdfb1af2287d1f7e89bd96dbf76ace83ce8647 (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
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env ruby

def get_depth_to(hsh, elm, to)
  n = 0
  loop do
    break if elm == to
    n += 1
    elm = hsh[elm]
  end
  n
end

def get_hier(hsh, elm)
  hier = [elm]
  loop do
    break if elm == "COM"
    elm = hsh[elm]
    hier << elm
  end
  hier
end

input = $stdin.readlines.map(&:strip).map{|x|x.split(")")}

objects = {}

input.each do |line|
  objects[line[1]] = line[0]
end

me_hier = get_hier(objects, "YOU")
santa_hier = get_hier(objects, "SAN")

lca = nil
me_hier.each do |obj|
  if santa_hier.include?(obj) then
    lca = obj
    break
  end
end

puts get_depth_to(objects, "YOU", lca) + get_depth_to(objects, "SAN", lca) - 2