Part 1


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]:
parse_programs (generic function with 1 method)

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]:
7-element Array{String,1}:
 "0 <-> 2"      
 "1 <-> 1"      
 "2 <-> 0, 3, 4"
 "3 <-> 2, 4"   
 "4 <-> 2, 3, 6"
 "5 <-> 6"      
 "6 <-> 4, 5"   

In [6]:
r = parse_programs(example)


Out[6]:
Dict{Any,Any} with 7 entries:
  "4" => Set(SubString{String}["2", "6", "3"])
  "1" => Set(SubString{String}["1"])
  "5" => Set(SubString{String}["6"])
  "0" => Set(SubString{String}["2"])
  "2" => Set(SubString{String}["4", "0", "3"])
  "6" => Set(SubString{String}["4", "5"])
  "3" => Set(SubString{String}["4", "2"])

In [14]:
union(Set(["1", "2"]), Set(["3", "4", "2"]))


Out[14]:
Set(String["4", "1", "2", "3"])

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]:
comunicate_to (generic function with 1 method)

In [27]:
length(comunicate_to("0", Set(), r))


Out[27]:
6

In [39]:
open("inputs/day12.txt") do fd
    lines = readlines(fd)
    all = parse_programs(lines)
    length(comunicate_to("0", Set(), all))
end


Out[39]:
380

Part 2


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]:
discover_groups (generic function with 1 method)

In [38]:
discover_groups(parse_programs(example))


Out[38]:
2

In [40]:
open("inputs/day12.txt") do fd
    lines = readlines(fd)
    discover_groups(parse_programs(lines))
end


Out[40]:
181
ERROR (unhandled task failure): InterruptException:
Stacktrace:
 [1] process_events at ./libuv.jl:82 [inlined]
 [2] wait() at ./event.jl:216
 [3] wait(::Condition) at ./event.jl:27
 [4] stream_wait(::Timer, ::Condition, ::Vararg{Condition,N} where N) at ./stream.jl:42
 [5] wait(::Timer) at ./event.jl:357
 [6] (::Base.##300#301{IJulia.#send_stderr,Timer})() at ./event.jl:430

In [ ]: