Repository navigation
Expand file tree
/
Copy pathpuzzle23.rb
More file actions
88 lines (81 loc) · 1.4 KB
/
Copy pathpuzzle23.rb
File metadata and controls
88 lines (81 loc) · 1.4 KB
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
lines = File.readlines("day23input.txt");
#lines = File.readlines("day23input_test.txt");
pg = 0
reg = Array.new(4) {0}
rt = {"a"=>0,"b"=>1,"c"=>2,"d"=>3}
reg[0] = 7
count = 0
while(true)
if (pg > lines.size())
break
end
# for i in 0..reg.size-1
# print(i.to_s + "," + reg[i].to_s + " ")
# end
# puts("pg," + pg.to_s + " " + lines[pg])
line = lines[pg]
if line =~ /cpy (.+) (.+)/
if rt.has_key?($2)
if rt.has_key?($1)
reg[rt[$2]] = reg[rt[$1]]
else
reg[rt[$2]] = $1.to_i
end
end
pg+=1
elsif line =~ /inc (.+)/
if rt.has_key?($1)
reg[rt[$1]] += 1
end
pg+=1
elsif line =~ /dec (.+)/
if rt.has_key?($1)
reg[rt[$1]] -= 1
end
pg+=1
elsif line =~ /jnz (.+) (.+)/
i=0
if rt.has_key?($1)
i=reg[rt[$1]]
else
i=$1.to_i
end
if i != 0
if rt.has_key?($2)
pg += reg[rt[$2]]
else
pg += $2.to_i
end
else
pg += 1
end
elsif line =~ /tgl (.*)/
if rt.has_key?($1)
i = pg + reg[rt[$1]]
else
i = pg + $1.to_i
end
pg+=1
if i>=0 and i<lines.size()
tline = lines[i]
if tline =~ /inc(.*)/
lines[i] = "dec" + $1
elsif tline =~ /^\S+(\s+\S+\s*)$/
lines[i] = "inc" + $1
elsif tline =~ /^jnz(\s+\S+\s+\S+\s*)$/
lines[i] = "cpy" + $1
elsif tline =~ /^\S+(\s+\S+\s+\S+\s*)$/
lines[i] = "jnz" + $1
end
end
#puts(lines)
#puts(reg)
else
break
end
count +=1
# if count > 20
# break
# end
end
puts(reg[0])