Created
December 16, 2019 02:40
-
-
Save mgates/5a506d6b3c81883e26e32b07369589ee to your computer and use it in GitHub Desktop.
advent 2019 day 11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "set" | |
class Thing | |
def paint_it(nums) | |
@painted = {} | |
@blarf = Set.new | |
@dir = :up | |
@loc = [0,0] | |
@first_panel = true | |
@last_out = :direction | |
do_it([], nums, [], "foo") | |
end | |
def par_mode(par, is_addr, nums, pos, offset) | |
if is_addr | |
nums[pos + offset] | |
else | |
nums[nums[pos + offset]] | |
end | |
end | |
def imm_mode(imm,nums, pos, offset) | |
nums[pos + offset] | |
end | |
def rel_mode(mode, rel, is_addr, nums, pos, offset) | |
if is_addr | |
nums[pos + offset] + rel | |
else | |
nums[nums[pos + offset] + rel] | |
end | |
end | |
def value(rel, mode, is_addr, *args) | |
if mode == :par | |
par_mode(:par, is_addr, *args) || 0 | |
elsif mode == :imm | |
raise "nope" if is_addr | |
imm_mode(:imm, *args) || 0 | |
elsif mode == :rel | |
rel_mode(:rel, rel, is_addr, *args) || 0 | |
end | |
end | |
def mode_for(d) | |
return :par if d == "0" | |
return :par if d == nil | |
return :imm if d == "1" | |
return :rel if d == "2" | |
end | |
def do_it(inq, code, output, name) | |
rel = 0 | |
nums = code.dup | |
pos = 0 | |
while true | |
if nums[pos] < 100 | |
opcode = nums[pos] | |
p1mode = :par | |
p2mode = :par | |
p3mode = :par | |
else | |
op = nums[pos].to_s | |
opcode = op.slice(-2..-1).to_i | |
p1mode = mode_for op[-3] | |
p2mode = mode_for op[-4] | |
p3mode = mode_for op[-5] | |
end | |
case opcode | |
when 99 | |
xmin = @painted.keys.map(&:first).min | |
xmax = @painted.keys.map(&:first).max | |
ymin = @painted.keys.map(&:last).min | |
ymax = @painted.keys.map(&:last).max | |
puts [xmin, xmax, ymin, ymax].inspect | |
(ymax.downto(ymin)).each do |y| | |
(xmin..xmax).each do |x| | |
print (@painted[[x,y]] == 0 || @painted[[x,y]] == nil) ? "⬛" : "⬜" | |
end | |
puts | |
end | |
puts "#{name}: DONE! #{@painted.count}\n" | |
#output.push out | |
#return out | |
return | |
when 1 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
p2 = value(rel, p2mode, false, nums, pos, 2) | |
p3 = value(rel, p3mode, true, nums, pos, 3) | |
(nums[p3] = p1 + p2) | |
pos += 4 | |
when 2 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
p2 = value(rel, p2mode, false ,nums, pos, 2) | |
p3 = value(rel, p3mode, true, nums, pos, 3) | |
(nums[p3] = p1 * p2) | |
pos += 4 | |
when 3 | |
p1 = value(rel, p1mode, true, nums, pos, 1) | |
popped = @painted[@loc] || (@first_panel ? 1 : 0) | |
@first_panel = false | |
nums[p1] = popped | |
pos += 2 | |
when 4 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
if @last_out == :direction | |
@last_out = :painted | |
@painted[@loc.dup] = p1 | |
@blarf << @loc | |
else | |
@last_out = :direction | |
turn = p1 == 1 ? :right : :left | |
@dir = case @dir | |
when :up | |
turn == :right ? :right : :left | |
when :right | |
turn == :right ? :down : :up | |
when :down | |
turn == :right ? :left : :right | |
when :left | |
turn == :right ? :up : :down | |
end | |
case @dir | |
when :up | |
@loc[1] +=1 | |
when :down | |
@loc[1] -=1 | |
when :right | |
@loc[0] +=1 | |
when :left | |
@loc[0] -=1 | |
end | |
end | |
pos += 2 | |
when 5 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
p2 = value(rel, p2mode, false, nums, pos, 2) | |
pos = p1 == 0 ? pos + 3 : p2 | |
when 6 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
p2 = value(rel, p2mode, false, nums, pos, 2) | |
pos = p1 == 0 ? p2 : pos + 3 | |
when 7 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
p2 = value(rel, p2mode, false, nums, pos, 2) | |
p3 = value(rel, p3mode, true, nums, pos, 3) | |
nums[p3] = p1 < p2 ? 1 : 0 | |
pos = pos + 4 | |
when 8 | |
p1 = value(rel, p1mode, false, nums, pos, 1) | |
p2 = value(rel, p2mode, false, nums, pos, 2) | |
p3 = value(rel, p3mode, true, nums, pos, 3) | |
nums[p3] = p1 == p2 ? 1 : 0 | |
pos = pos + 4 | |
when 9 | |
rel += value(rel, p1mode, false, nums, pos, 1) | |
pos += 2 | |
else | |
raise "wtf: #{pos}: #{nums[pos]}\n#{nums.inspect}" | |
end | |
end | |
end | |
end | |
nums = Array.new {0} | |
prog = ARGF.read.chomp.split(",").map(&:to_i) | |
nums[0..prog.length] = *prog | |
Thing.new.paint_it(nums) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3,8,1005,8,345,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,28,1006,0,94,2,106,5,10,1,1109,12,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,62,1,103,6,10,1,108,12,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,92,2,104,18,10,2,1109,2,10,2,1007,5,10,1,7,4,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,129,2,1004,15,10,2,1103,15,10,2,1009,6,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,164,2,1109,14,10,1,1107,18,10,1,1109,13,10,1,1107,11,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,201,2,104,20,10,1,107,8,10,1,1007,5,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,236,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,257,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,279,1,107,0,10,1,107,16,10,1006,0,24,1,101,3,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,316,2,1108,15,10,2,4,11,10,101,1,9,9,1007,9,934,10,1005,10,15,99,109,667,104,0,104,1,21101,0,936995730328,1,21102,362,1,0,1105,1,466,21102,1,838210728716,1,21101,373,0,0,1105,1,466,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,235350789351,1,21101,0,420,0,1105,1,466,21102,29195603035,1,1,21102,1,431,0,1105,1,466,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,825016079204,1,21101,0,454,0,1105,1,466,21101,837896786700,0,1,21102,1,465,0,1106,0,466,99,109,2,21201,-1,0,1,21101,0,40,2,21102,1,497,3,21101,0,487,0,1105,1,530,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,492,493,508,4,0,1001,492,1,492,108,4,492,10,1006,10,524,1101,0,0,492,109,-2,2105,1,0,0,109,4,2102,1,-1,529,1207,-3,0,10,1006,10,547,21102,1,0,-3,21201,-3,0,1,22102,1,-2,2,21101,1,0,3,21102,1,566,0,1105,1,571,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,594,2207,-4,-2,10,1006,10,594,21201,-4,0,-4,1106,0,662,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21101,613,0,0,1105,1,571,22101,0,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,632,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,654,22101,0,-1,1,21102,654,1,0,105,1,529,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment