-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.jl
51 lines (45 loc) · 1.04 KB
/
day21.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
include("IntCode.jl")
using .IntCode
using OffsetArrays
function load_code(filename)
open(filename, "r") do file
ret = Vector{Int}()
while !eof(file)
pushfirst!(ret, Int(read(file, Char)))
end
return ret
end
end
function run!(srccode::Vector{Int}, springscript_filename::String)
in = load_code(springscript_filename)
out = Vector{Int}()
program = Program(copy(srccode), in, out)
run_program!(program)
while length(out) > 1
print_out(out)
end
if !isempty(out)
return pop!(out)
else
return -1
end
end
function print_out(out::Vector{Int})
io = IOBuffer()
prevc = ' '
while !isempty(out)
c = Char(pop!(out))
if c == '\n' && prevc == '\n'
break
end
prevc = c
print(io, c)
end
println(String(take!(io)))
end
function main()
srccode = read_src("input21.txt")
println("PART 1: ", run!(srccode, "day21code.txt"))
println("PART 2: ", run!(srccode, "day21code2.txt"))
end
main()