In [2]:
function parse_programs(values)
r = Dict()
for line in values
from, to = match(r"([0-9]+) <-> ([0-9 ,]+)", strip(line)).captures
s_to = split(to, ", ")
r[from] = Set(s_to)
end
r
end
Out[2]:
In [3]:
example = [
"0 <-> 2",
"1 <-> 1",
"2 <-> 0, 3, 4",
"3 <-> 2, 4",
"4 <-> 2, 3, 6",
"5 <-> 6",
"6 <-> 4, 5"
]
Out[3]:
In [6]:
r = parse_programs(example)
Out[6]:
In [14]:
union(Set(["1", "2"]), Set(["3", "4", "2"]))
Out[14]:
In [22]:
function comunicate_to(dest, visited, all)
for to in all[dest]
if !in(to, visited)
push!(visited, to)
visited = union(visited, comunicate_to(to, visited, all))
end
end
visited
end
Out[22]:
In [27]:
length(comunicate_to("0", Set(), r))
Out[27]:
In [39]:
open("inputs/day12.txt") do fd
lines = readlines(fd)
all = parse_programs(lines)
length(comunicate_to("0", Set(), all))
end
Out[39]:
In [37]:
function discover_groups(data)
all = Set(keys(data))
done = Set()
groups = 0
for p in all
if !in(p, done)
done = union(done, comunicate_to(p, Set(), data))
groups += 1
end
end
groups
end
Out[37]:
In [38]:
discover_groups(parse_programs(example))
Out[38]:
In [40]:
open("inputs/day12.txt") do fd
lines = readlines(fd)
discover_groups(parse_programs(lines))
end
Out[40]:
In [ ]: