Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
Last active December 21, 2015 09:39
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 EvanHahn/6286274 to your computer and use it in GitHub Desktop.
Save EvanHahn/6286274 to your computer and use it in GitHub Desktop.
Solution to Reddit's r/dailyprogrammer challenge #132. http://redd.it/1kqxz9
INSTRUCTIONS = {
'and' => {
0x00 => [:address, :address],
0x01 => [:address, :literal]
},
'or' => {
0x02 => [:address, :address],
0x03 => [:address, :literal]
},
'xor' => {
0x04 => [:address, :address],
0x05 => [:address, :literal]
},
'not' => {
0x06 => [:address],
},
'mov' => {
0x07 => [:address, :address],
0x08 => [:address, :literal]
},
'random' => {
0x09 => [:address],
},
'add' => {
0x0a => [:address, :address],
0x0b => [:address, :literal]
},
'sub' => {
0x0c => [:address, :address],
0x0d => [:address, :literal]
},
'jmp' => {
0x0e => [:address],
0x0f => [:literal]
},
'jz' => {
0x10 => [:address, :address],
0x11 => [:address, :literal],
0x12 => [:literal, :address],
0x13 => [:literal, :literal],
},
'jeq' => {
0x14 => [:address, :address, :address],
0x15 => [:literal, :address, :address],
0x16 => [:address, :address, :literal],
0x17 => [:literal, :address, :literal],
},
'jls' => {
0x18 => [:address, :address, :address],
0x19 => [:literal, :address, :address],
0x1a => [:address, :address, :literal],
0x1b => [:literal, :address, :literal],
},
'jgt' => {
0x1c => [:address, :address, :address],
0x1d => [:literal, :address, :address],
0x1e => [:address, :address, :literal],
0x1f => [:literal, :address, :literal],
},
'aprint' => {
0x20 => [:address],
0x21 => [:literal],
},
'dprint' => {
0x22 => [:address],
0x23 => [:literal],
},
'halt' => {
0xff => []
}
}
def hexify(v)
"0x#{v.to_i.to_s(16).upcase.rjust(2, '0')}"
end
def parse(line)
split = line.split
instruction_string = split.shift.downcase!
instruction = INSTRUCTIONS[instruction_string].find do |i, value|
value == split.map do |argument|
if argument.start_with? '['
:address
else
:literal
end
end
end.first
arguments = split.map { |s| s.delete('[').to_i }
([instruction] + arguments).map { |v| hexify v }.join(' ')
end
File.open(ARGV.first, 'r') do |file|
while (line = file.gets)
puts parse line
end
end
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment