Skip to content

Instantly share code, notes, and snippets.

@tckmn
Created December 8, 2020 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tckmn/ba6c243bfde8d73c4e92033f7f0cabef to your computer and use it in GitHub Desktop.
Save tckmn/ba6c243bfde8d73c4e92033f7f0cabef to your computer and use it in GitHub Desktop.
aoc 2020 day 8
def f idx
code = read.lines.map{|x|
a,b = x.split
[a,b.to_i]
}
return if code[idx][0] == 'acc'
code[idx][0] = code[idx][0] == 'jmp' ? 'nop' : 'jmp'
executed = code.map{false}
i = 0
acc = 0
loop {
if i == code.length
puts acc
exit
end
if executed[i]
return
end
executed[i] = true
case code[i][0]
when 'acc' then acc += code[i][1]; i += 1
when 'jmp' then i += code[i][1]
when 'nop' then i += 1
end
}
end
read.lines.map.with_index{|_,i| f i}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment