repl.it
Crystal

No description

fork
loading
Files
  • main.cr
  • input.txt
main.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
input = File.read "input.txt"
v = input.split(",").map &.to_i

def gr(v,i,m);m==1?v[i]: v[v[i]];end
def sr(v,i,t,m);m==1?(v[i]=t):(v[v[i]]=t);end
def gm(m,i);m[i]?||0;end

i = 0
loop do
	i=0 if i > v.size

	m = (v[i]-v[i]%100).to_s.split("").reverse.map &.to_i
	m.shift
	!m.empty? && m.shift
	case v[i]%100
		when 1 then sr(v,i+3,gr(v,i+1,gm m,2) + gr(v,i+2,gm m,1),gm m,0); i+=4
		when 2 then sr(v,i+3,gr(v,i+1,gm m,2) * gr(v,i+2,gm m,1),gm m,0); i+=4
		when 3 then sr(v,i+1,gets.not_nil!.chomp.to_i,gm m,0);            i+=2
		when 4 then puts gr(v,i+1,gm m,0);                                i+=2
		when 99 then break
		else raise "wat #{v[i]%100}"
	end
end
?