-
Notifications
You must be signed in to change notification settings - Fork 0
/
25.jl
82 lines (73 loc) · 2.14 KB
/
25.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
74
75
76
77
78
79
80
81
using Revise
using Combinatorics
includet("IntComputer.jl"); using .IntComputer
function init_game(file)
code = intcode_from_file(file)
input = Channel{Int}(Inf)
output = Channel{Int}(Inf)
computer = @async run!(copy(code), input=input, output=output)
return (input=input, output=output)
end
function run_TUI(game)
@async while true
print.(Char.(take!(game.output)))
end
history = String[]
try
while true
read_input = readline()
push!(history, read_input)
send_cmd(game,read_input)
end
catch e
@show e
finally
return history
end
end
function send_cmd(game, cmd, retrieve_output=false)
command = [Int(c) for c in cmd]
push!(command,Int('\n'))
push!.(Ref(game.input),command)
if retrieve_output
output = Int[]
wait(game.output)
while isready(game.output)
push!(output, take!(game.output))
end
return join(Char.(output))
end
end
game = init_game("25.input")
history = run_TUI(game)
collect_all_items = [
"north", "take mug", "north", "take food ration", "south", "east", "north", "east", "take semiconductor", "west", "south", "west", "south", "east", "take ornament", "north",
"take coin", "east", "take mutex", "west", "south", "east", "take candy cane", "west", "west", "south", "east", "take mouse", "south", "west",
]
send_cmd.(Ref(game),collect_all_items, Ref(true))
function bruteforce_detector(game)
all_stuff = map(filter(x->startswith(x,"-"),split(send_cmd(game,"inv",true),"\n"))) do inv
inv[3:end]
end
function drop_all()
for item in all_stuff
send_cmd(game,"drop $item",true)
end
end
function take(items)
for item in items
send_cmd(game,"take $item",true)
end
end
for n in 1:length(all_stuff)
for comb in combinations(all_stuff,n)
drop_all(); take(comb)
res = send_cmd(game,"west",true)
if !(occursin("Alert!",res))
println(res)
return nothing
end
end
end
end
bruteforce_detector(game)