-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.jl
131 lines (120 loc) · 3.7 KB
/
day25.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module Day25
using AdventOfCode2019
using Combinatorics
function day25(input::String = readInput(joinpath(@__DIR__, "..", "data", "day25.txt")))
program = parse.(Int, split(input, ","))
return solve(program)
end
# This last puzzle was an interactive puzzle.
# It has been solved by manually exploring the map and collecting all the
# items which do not lead the game to end.
# After having collected all the items it uses simple brute-force to enter
# the "Pressure-Sensitive Floor" and collecting the password.
#
# This process has been hardcoded in that `solve` method, so it will only
# work for my particular input!
function solve(program::Array{Int,1})
inp = Channel{Int}(100)
out = Channel{Int}(1000)
waiting = Channel{Bool}(1)
@async AdventOfCode2019.IntCode.run_program!(copy(program), inp, out, nothing; waitingForInput = waiting)
items = ["mutex", "whirled peas", "cake", "space law space brochure", "loom", "hologram", "manifold", "easter egg"]
command("north", inp, out)
command("take mutex", inp, out)
command("east", inp, out)
command("east", inp, out)
command("east", inp, out)
command("take whirled peas", inp, out)
command("west", inp, out)
command("west", inp, out)
command("west", inp, out)
command("south", inp, out)
command("south", inp, out)
command("take cake", inp, out)
command("north", inp, out)
command("west", inp, out)
command("take space law space brochure", inp, out)
command("north", inp, out)
command("take loom", inp, out)
command("south", inp, out)
command("south", inp, out)
command("take hologram", inp, out)
command("west", inp, out)
command("take manifold", inp, out)
command("east", inp, out)
command("north", inp, out)
command("east", inp, out)
command("south", inp, out)
command("west", inp, out)
command("south", inp, out)
command("take easter egg", inp, out)
command("south", inp, out)
for combi in Combinatorics.combinations(items)
drop_all(items, inp, out)
take_items(combi, inp, out)
command("south", inp, out; clear_output = false)
while !isready(out)
yield()
end
os = get_output(out)
if occursin("heavier", os) || occursin("lighter", os)
continue
end
# println(os)
m = match(r"\d+", os)
return parse(Int, m.match)
break
end
# while true
# print("> "); n = readline()
# if n == "read"
# o = Int[]
# while isready(out)
# push!(o, take!(out))
# end
# print(join(Char.(o)))
# continue
# end
# if n == "exit" || n == "quit" || n == "q"
# return
# end
# input = push!([Int(x) for x in n], 10)
# for x in input
# put!(inp, x)
# end
# end
end
function get_output(out::Channel{Int})
o = Int[]
while isready(out)
push!(o, take!(out))
end
return join(Char.(o))
end
function command(c::String, inp::Channel{Int}, out::Channel{Int}; clear_output = true)
commandInts = push!([Int(x) for x in c], 10)
for x in commandInts
put!(inp, x)
end
if clear_output
while !isready(out)
yield()
end
while isready(out)
take!(out)
end
end
end
function drop_all(items::Array{String,1}, inp::Channel{Int}, out::Channel{Int})
for item in items
c = "drop " * item
command(c, inp, out)
end
end
function take_items(items::Array{String,1}, inp::Channel{Int}, out::Channel{Int})
for item in items
c = "take " * item
command(c, inp, out)
end
end
end # module