-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.jl
73 lines (68 loc) · 1.96 KB
/
23.jl
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Revise
includet("IntComputer.jl"); using .IntComputer
function init_computers(file)
code = intcode_from_file(file)
input = [Channel{Int}(Inf) for _ in 1:50]
output = [Channel{Int}(Inf) for _ in 1:50]
computers = [ @async(run!(copy(code), input=input[i], output=output[i])) for i in 1:length(input)]
return input, output, computers
end
function twice_in_row(v)
for i in 1:length(v)-1
v[i] == v[i+1] && return v[i]
end
return nothing
end
function NWcontroller(input,output, computers, use_nat=true)
NAT_tracking = Int[]
NAT = (X=-1, Y=-1)
not_sending = 0
while true
push!.(input,Ref(-1))
not_sending += 1
for i in 1:length(output)
out = output[i]
c = computers[i]
istaskdone(c) && continue
isready(out) || continue
not_sending = 0
dst = take!(out)
X = take!(out)
Y = take!(out)
if dst == 255
NAT = (X=X, Y=Y)
if !use_nat
@info "Ending ..." NAT
return
end
elseif dst <= 49
push!(input[dst+1],X)
push!(input[dst+1],Y)
end
end
sleep(0.01)
if not_sending > 10
not_sending = 0
@info "Network is idle"
@info "Sending to 0" NAT.X NAT.Y
push!(input[1], NAT.X)
push!(input[1], NAT.Y)
push!(NAT_tracking, NAT.Y)
twice = twice_in_row(NAT_tracking)
if twice != nothing
@info "Found twice in a row" twice
return
end
end
end
end
# Part 01
input, output, computers = init_computers("23.input")
# Start them
push!.(input,0:49)
NWcontroller(input,output,computers, false)
# Part 02
input, output, computers = init_computers("23.input")
# Start them
push!.(input,0:49)
NWcontroller(input,output,computers)