mid-kid
5 years ago
12 changed files with 743 additions and 0 deletions
@ -0,0 +1,145 @@ |
|||
function get_param(state, arg) |
|||
local param_mode = state.mem[state.pos] // (10 ^ (arg + 1)) % 10 |
|||
if #state.mem < state.pos + arg then |
|||
print(string.format("Error: Opcode too short at end of memory: %d", state.pos)) |
|||
os.exit(false) |
|||
end |
|||
local arg = state.mem[state.pos + arg] |
|||
if param_mode == 0 then |
|||
arg = arg + 1 |
|||
elseif param_mode == 2 then |
|||
arg = arg + state.relbase |
|||
end |
|||
return arg |
|||
end |
|||
|
|||
function read_mem(state, arg) |
|||
local param_mode = state.mem[state.pos] // (10 ^ (arg + 1)) % 10 |
|||
local arg = get_param(state, arg) |
|||
if param_mode == 1 then |
|||
return arg |
|||
end |
|||
arg = state.mem[arg] |
|||
if not arg then |
|||
return 0 |
|||
end |
|||
return arg |
|||
end |
|||
|
|||
function write_mem(state, arg, val) |
|||
local param_mode = state.mem[state.pos] // (10 ^ (arg + 1)) % 10 |
|||
local arg = get_param(state, arg) |
|||
if param_mode == 1 then |
|||
print(string.format("Error: Invalid inmediate write: %d", arg)) |
|||
end |
|||
state.mem[arg] = val |
|||
end |
|||
|
|||
function interpret(state) |
|||
if not state.mem then |
|||
state.mem = state |
|||
end |
|||
if not state.pos then |
|||
state.pos = 1 |
|||
state.relbase = 1 |
|||
end |
|||
if not state.input then |
|||
state.input = input_stdin |
|||
end |
|||
if not state.output then |
|||
state.output = output_stdout |
|||
end |
|||
|
|||
while true do |
|||
if #state.mem < state.pos then |
|||
print(string.format("Instruction pointer ran off of memory")) |
|||
os.exit(false) |
|||
end |
|||
|
|||
local opcode = state.mem[state.pos] % 100 |
|||
local instr_size = 1 |
|||
|
|||
if opcode == 1 then |
|||
write_mem(state, 3, read_mem(state, 1) + read_mem(state, 2)) |
|||
instr_size = 4 |
|||
elseif opcode == 2 then |
|||
write_mem(state, 3, read_mem(state, 1) * read_mem(state, 2)) |
|||
instr_size = 4 |
|||
elseif opcode == 3 then |
|||
local val = state.input() |
|||
if not val then return true end |
|||
write_mem(state, 1, val) |
|||
instr_size = 2 |
|||
elseif opcode == 4 then |
|||
state.output(read_mem(state, 1)) |
|||
instr_size = 2 |
|||
elseif opcode == 5 then |
|||
instr_size = 3 |
|||
if read_mem(state, 1) ~= 0 then |
|||
state.pos = read_mem(state, 2) + 1 |
|||
instr_size = 0 |
|||
end |
|||
elseif opcode == 6 then |
|||
instr_size = 3 |
|||
if read_mem(state, 1) == 0 then |
|||
state.pos = read_mem(state, 2) + 1 |
|||
instr_size = 0 |
|||
end |
|||
elseif opcode == 7 then |
|||
write_mem(state, 3, read_mem(state, 1) < read_mem(state, 2) and 1 or 0) |
|||
instr_size = 4 |
|||
elseif opcode == 8 then |
|||
write_mem(state, 3, read_mem(state, 1) == read_mem(state, 2) and 1 or 0) |
|||
instr_size = 4 |
|||
elseif opcode == 9 then |
|||
state.relbase = state.relbase + read_mem(state, 1) |
|||
instr_size = 2 |
|||
elseif opcode == 99 then |
|||
return false |
|||
else |
|||
print(string.format("Error: Invalid opcode %d at %d", opcode, state.pos - 1)) |
|||
os.exit(false) |
|||
end |
|||
|
|||
state.pos = state.pos + instr_size |
|||
end |
|||
end |
|||
|
|||
function input_stdin() |
|||
io.stdout:write("> ") |
|||
return io.stdin:read("n") |
|||
end |
|||
|
|||
function output_stdout(val) |
|||
io.stdout:write(val) |
|||
io.stdout:write("\n") |
|||
end |
|||
|
|||
function input_array(array) |
|||
function inner() |
|||
return table.remove(array, 1) |
|||
end |
|||
return inner |
|||
end |
|||
|
|||
function output_array(array) |
|||
function inner(val) |
|||
table.insert(array, val) |
|||
end |
|||
return inner |
|||
end |
|||
|
|||
file = io.open(arg[1]) |
|||
program = {} |
|||
for num in string.gmatch(file:read(), "(-?%d+),?") do |
|||
table.insert(program, tonumber(num)) |
|||
end |
|||
file:close() |
|||
|
|||
--interpret(program) |
|||
|
|||
robot_input = {} |
|||
robot_output = {} |
|||
while interpret({mem=program, input=input_array(robot_input), output=output_array(robot_output)}) do |
|||
|
|||
end |
@ -0,0 +1,53 @@ |
|||
moons = {} |
|||
for line in io.lines(arg[1]) do |
|||
x, y, z = line:match("<x=(-?%d+), +y=(-?%d+), +z=(-?%d+)>") |
|||
table.insert(moons, {pos={x=tonumber(x), y=tonumber(y), z=tonumber(z)}, vel={x=0, y=0, z=0}}) |
|||
end |
|||
|
|||
function update_vel(moon) |
|||
for _, omoon in ipairs(moons) do |
|||
for _, axis in ipairs({"x", "y", "z"}) do |
|||
if omoon.pos[axis] > moon.pos[axis] then |
|||
moon.vel[axis] = moon.vel[axis] + 1 |
|||
end |
|||
if omoon.pos[axis] < moon.pos[axis] then |
|||
moon.vel[axis] = moon.vel[axis] - 1 |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
function update_pos(moon) |
|||
moon.pos.x = moon.pos.x + moon.vel.x |
|||
moon.pos.y = moon.pos.y + moon.vel.y |
|||
moon.pos.z = moon.pos.z + moon.vel.z |
|||
end |
|||
|
|||
print("After 0 steps:") |
|||
for _, moon in ipairs(moons) do |
|||
print(string.format("pos=<x=%d, y=%d, z=%d>, vel=<x=%d, y=%d, z=%d>", moon.pos.x, moon.pos.y, moon.pos.z, moon.vel.x, moon.vel.y, moon.vel.z)) |
|||
end |
|||
print() |
|||
for i = 1, 10 do |
|||
for _, moon in ipairs(moons) do |
|||
update_vel(moon) |
|||
end |
|||
|
|||
for _, moon in ipairs(moons) do |
|||
update_pos(moon) |
|||
end |
|||
|
|||
print(string.format("After %d steps:", i)) |
|||
for _, moon in ipairs(moons) do |
|||
print(string.format("pos=<x=%d, y=%d, z=%d>, vel=<x=%d, y=%d, z=%d>", moon.pos.x, moon.pos.y, moon.pos.z, moon.vel.x, moon.vel.y, moon.vel.z)) |
|||
end |
|||
print() |
|||
end |
|||
|
|||
energy = 0 |
|||
for _, moon in ipairs(moons) do |
|||
pot = math.abs(moon.pos.x) + math.abs(moon.pos.y) + math.abs(moon.pos.z) |
|||
kin = math.abs(moon.vel.x) + math.abs(moon.pos.y) + math.abs(moon.pos.z) |
|||
energy = energy + pot * kin |
|||
end |
|||
print("Energy:", energy) |
@ -0,0 +1,74 @@ |
|||
width = nil |
|||
map = {} |
|||
for line in io.lines(arg[1]) do |
|||
if not width then |
|||
width = #line |
|||
end |
|||
for char in line:gmatch(".") do |
|||
table.insert(map, char) |
|||
end |
|||
end |
|||
|
|||
function reachable_keys(map, pos_y, pos_x, keys, dist) |
|||
local cell = map[width * pos_y + pos_x] |
|||
if cell == "@" then |
|||
cell = "." |
|||
end |
|||
if cell == "." then |
|||
map[width * pos_y + pos_x] = "#" |
|||
reachable_keys(map, pos_y + 1, pos_x, keys, dist + 1) |
|||
reachable_keys(map, pos_y - 1, pos_x, keys, dist + 1) |
|||
reachable_keys(map, pos_y, pos_x + 1, keys, dist + 1) |
|||
reachable_keys(map, pos_y, pos_x - 1, keys, dist + 1) |
|||
elseif string.byte(cell) >= 97 and string.byte(cell) <= 122 then |
|||
table.insert(keys, {key=cell, y=pos_y, x=pos_x, dist=dist}) |
|||
end |
|||
return keys |
|||
end |
|||
|
|||
function find_paths(map, pos_y, pos_x) |
|||
local keys = reachable_keys({table.unpack(map)}, pos_y, pos_x, {}, 0) |
|||
local paths = {} |
|||
for _, key in ipairs(keys) do |
|||
--print(key.key, key.dist) |
|||
local open_map = {table.unpack(map)} |
|||
open_map[width * pos_y + pos_x] = "." |
|||
open_map[width * key.y + key.x] = "@" |
|||
for i, x in ipairs(open_map) do |
|||
if x:lower() == key.key then |
|||
open_map[i] = "." |
|||
break |
|||
end |
|||
end |
|||
local path = find_paths(open_map, key.y, key.x) |
|||
if #path == 0 then |
|||
table.insert(paths, {path={key.key}, dist=key.dist}) |
|||
else |
|||
for _, p in ipairs(path) do |
|||
table.insert(p.path, key.key) |
|||
p.dist = p.dist + key.dist |
|||
table.insert(paths, p) |
|||
end |
|||
end |
|||
end |
|||
return paths |
|||
end |
|||
|
|||
pos_y = nil |
|||
pos_x = nil |
|||
for i, x in ipairs(map) do |
|||
if x == "@" then |
|||
pos_y = i // width |
|||
pos_x = i % width |
|||
break |
|||
end |
|||
end |
|||
|
|||
paths = find_paths(map, pos_y, pos_x) |
|||
smallest = nil |
|||
for _, path in ipairs(paths) do |
|||
if not smallest or smallest.dist > path.dist then |
|||
smallest = path |
|||
end |
|||
end |
|||
print(table.concat(smallest.path, ","), smallest.dist) |
@ -0,0 +1,81 @@ |
|||
################################################################################# |
|||
#...#.........P...................#.....#.........#.......#.........#...........# |
|||
#.###.#############.#.###########.#.#.###.#########.###.###.#######.#.########### |
|||
#.#...#.#...V.....#.#..t..#.......#.#..a#...........#.#..c..#...#.#.#...........# |
|||
#.#.###.#.#######.#####.#.#####.#######.#.###########.#######.#.#.#.###########.# |
|||
#...#...#...#.#...#.J.#.#.....#.........#.......#.......#.....#.#.#.......#...#.# |
|||
#Z###.#####.#.#.###.#.###.###.#################.#.###.#.#.#####.#.#######.#.#.#.# |
|||
#.....#...#.#.......#...#...#.#...#.....#...#.#.#.#...#.#...#.....#.......#.#.#.# |
|||
###.###.#.#.###########.###.#.#.#.#.###.#.#.#.#.#.#.###.###.#######.#######.#.#.# |
|||
#.#.#...#..y#.........#...#.#...#...#...#.#...#.#.#.#.......#.......#.......#...# |
|||
#.#.#.#######.#######.###.#######.###.###.###.#.###.#######.#.#######.#########.# |
|||
#.#.#..q........#.....#.#...#.E.#...#...#.#...#...#.....#.#.#.#...........#...#.# |
|||
#.#.#############.#####.###.#.#.#######.#.###.###.#.###.#.#.#.#####.#####.#.#.#.# |
|||
#.#...F.......#...#...#.......#.........#...#.#...#.#...#.#.#.....#.#.....#.#...# |
|||
#.#########.###.###.#S###################.#I###.#####.###.#.#####.#.#.#########.# |
|||
#...#...#...#...#...#....u......#.#.....#.#...#.#...#.#...#.....#.#.#.#.......#.# |
|||
#.###.#.#.###.###.#############.#.#.###.#####.#.#.#.#.#.#######.#.#.#.#.#####.#.# |
|||
#.#...#.#.#.#.#s..#...#hG.#...#.#.#...#.#.....#...#...#.#.......#.#.#...#...#.#.# |
|||
#.#.###.#.#.#O#.###.#.#.#.#.#.#.#.#.###.#.###.#########.#.#######.###.###.###.#.# |
|||
#m..#.#...#.#.#.#...#...#...#g#.#.#.#...#.#.#.#.........#.#.#...#...#.....#...#.# |
|||
#.###.#####.#.#.#H###########.#.#.#.#.###.#.#.###.#.#####.#.#.#.#.#.#.#####.###.# |
|||
#.#.........#...#.....#...#...#.#...#...#.#...#...#.#...#.#.#.#.#.#.#.#..w#.#.#.# |
|||
#.#.#####.#.#############.#.###U###.###.#.#.###.###.#.#.#.#.###.###.#.#.#.#.#.#.# |
|||
#.#.....#.#.#.....#.......#.#.....#.#...#.#.....#...#.#.#.#...#...#.#.#.#...#...# |
|||
#.#######.#.###.#.#####.###.#####.###.#.#.#######.###.#.#.#.#####.#.###.#####.### |
|||
#.#.......#.....#.....#...#.#...#b#...#.#.#.#.....#...#.....#.....#...#.#.....#.# |
|||
#.#.#################.#.#.#.#.#.#.#.###.#.#.#.#####.#########.#######.#.#.#####.# |
|||
#.#.#.......#.......#.#.#.#.B.#...#.#.#.#.#.....#...#.....#...#.......#.#...#...# |
|||
#.#.#.#######.###.###.#.#.#########.#.#.#.#######.###.#.###.#####.#.###N###.#.### |
|||
#.#.#...#.....#...#...#.#...........#.#.#.......#.#...#.#...#...#.#.#...#...#...# |
|||
#.#.#.#.#.#####.###.###.###########.#.#.#.#####.#.#.###.#.###.#.###.#.###.###.#.# |
|||
#...#.#.#.#...#.....#.#.#...#.......#...#.#..r#...#.#...#.....#.....#.#.#...#.#.# |
|||
#######.#.###.#######.#.###.#.#######.###.###.#######.#.#############.#.###.#.#.# |
|||
#.......#...#.......#.#...#.#.#.....#.#.#.....#.......#.......#.......#...#.#.#.# |
|||
###.###.###.#.#####.#.###.#.#.###.#.#.#.#####.#.#.###########.###.#####.#.#.###.# |
|||
#...#...#...#...#.#.#.......#...#.#.#.#.#.#...#.#...#...#...#...#.#.....#.#...#.# |
|||
#D###.###.###.#.#.#.###########.#.###.#.#.#.###.###.#.#.#.#.###.#.#.###.#####.#.# |
|||
#...#...#.#...#.#.#...........#.#.#...#.#...#...#...#.#...#...#...#.#.#.......#.# |
|||
#.#.#####.#####.#.###########.#.#.#.###.#.#######.###.#######.#####.#.#########.# |
|||
#.#.............#...............#.................#.........#.................M.# |
|||
#######################################.@.####################################### |
|||
#.#...#.......#...............................#.........#.........#..d#...#.....# |
|||
#.#.#.#.#####.#.#######################.###.#.#.#######.#.###.###.###.#.#.#.#.#K# |
|||
#...#...#...#.#.#...#.................#.#...#.#.#.#.....#...#.#.#.....#.#...#.#.# |
|||
#.#########.#.#.#.#.#.###############.#.#.###.#.#.#.#########.#.#####.#.#####.### |
|||
#.....#...#.#.#.#.#.#.#...#.........#...#.#...#.#.#.......#...#.#.....#.....#...# |
|||
#####.#.#.#.#.#.#.#.#.#.#.#.#####.#####.#.#.###.#.#######.#.###.#.#########.###.# |
|||
#...#.#.#...#.....#.#.#.#.#...#...#...#.#.#.#...#...........#.....#.......#.#...# |
|||
#.#.#.#.###.#########.#.#.###.#####.#.#.#.#.###.###.###########.###.###.###.#.#.# |
|||
#.#.#...#.W...#.......#.#...#.......#.#.#.#...#...#.#...#.#...#.#...#l#.#...#.#.# |
|||
#.###.#########.#######.###.#.#######.#.#.###.###.#.#.#.#.#.#.###.###.#.#.###.#.# |
|||
#.....#.........#...#...#...#.......#...#.#...#...#...#...#.#.#.......#.#...#.#.# |
|||
#.#####.#######.#.#.#.###.###.#####.#####.###.#.#####.#####.#.#.#######.###.#.### |
|||
#.#...#.#.....#.#.#...#...#...#...#.....#...#.R.#...#.#.....#.....#...#...#x#...# |
|||
#.#.#.#.###.###.#.#####.#######.#.#######.#.#####.#.###.###########.#.#.###.###.# |
|||
#...#.#...#...#.#.#...#...#...#.#.#.....#.#.#.....#.#...#.#.........#.#.......#.# |
|||
#####.###.###.#.#.###.###.#.#.#.#.#.###.#.#.#.#####.#.###.#.#########.#.#######.# |
|||
#...#...#...#...#...#.......#...#.#...#.#.#...#.#...#.....#.#.......#.#.#.......# |
|||
###.###.###.###.###.#############.###.#.#.#####.#.#######.#.#.#.#####Y###.#####.# |
|||
#...#.....#...#...#.......#.#...#.....#.#.#..i..#.......#.#.#.#.......#...#.#...# |
|||
#.###.#######.###.#######.#.#.#.#######.#.#.###.#####.###.#Q###.#######.###.#.### |
|||
#...#.#.......#..n#.#.....#...#...#.....#.#.#...#...#.#...#.#.#.#...#...#.#...#.# |
|||
#.#.#.#.###.#####.#.#######.#####.#.#####.#.#.###.###.#.###.#.#.#.#.#.###.#.###.# |
|||
#.#...#.#...#...#.....#...#.#...#...#...#...#...#.#...#...#..o#...#.#.#.......#.# |
|||
#.#####.#####.#.#######.#.#.#.#.#####.#.#######.#.#.###.###########.#.#######.#.# |
|||
#.....#...#...#.#.......#.#.#.#.....#.#.#.....#.#.#.....#.........#.#.#...#.....# |
|||
#####L###.#.###.#.#######.#.###.###.###.###.#.#.#.#######.#.#####.#.#.#.#.####### |
|||
#...#.#...#.#...#...#...#.#...#.#.......#...#.#...#.....#.#.....#.#.#.#.#.......# |
|||
#.#.#.#.###.#.#.###.#.#.#.###.#.#######.#.#######.#.#.###.#####.###.#.#.#######.# |
|||
#.#.#.#.....#.#.#...#.#.....#.#.......#.#.......#...#.#...#..j..#..v#.........#.# |
|||
#.#.#.#######.#.#.###.#.#####.###.###.###.#####.###.###.###.###.#.###.#########.# |
|||
#.#.#.#...#...#.#...#.#.#...#...#...#...#...#.#.#...#...#.#.#...#.#.#.#.........# |
|||
###.#.#.#.#.#######.#.###.#.###.#####.#.###.#.#.#.###.###.#.#####.#.#.#.#######.# |
|||
#...#...#.#.........#.....#...#.....#f#.#.....#.....#e..#.#.......#.#.#.#.#...#.# |
|||
#.#######.#################.#.#####.###.#.#############A#.#########.#.#.#.#.#.#.# |
|||
#...#.....#.....#...#.....#.#.....#.....#...#...#.....#...#...#.......#.#.#.#..z# |
|||
#.#.#.#####.###.###.#.###C#.###########.###.#.#T#.#.#####.#.###.#######.#.#.##### |
|||
#.#.#.X.#k..#.#...#...#...#.....#.....#.#...#.#...#.......#.....#....p..#...#...# |
|||
#.#.###.###.#.###.#####.#.#####.#.#.###.#.###.###################.#######.###.#.# |
|||
#.#.........#...........#.....#...#.....#...#.....................#...........#.# |
|||
################################################################################# |
@ -0,0 +1,125 @@ |
|||
Q I B S J P J |
|||
I P B L M Q H |
|||
###################################.###########.#####.###.#####.#####.###.####################################### |
|||
#.#...#.........#.#...#.#...#.....#...#.#.#.#.#...#.....#.......#.....#.........#...#.........#.#.....#...#.....# |
|||
#.#.###.###.###.#.###.#.###.#.#.#.#.###.#.#.#.#.#######.#.#.#.#.#.#.###.#.#####.#.#.#.#########.#.#####.###.###.# |
|||
#.........#.#.................#.#...#.....#.#.#.#...#...#.#.#.#.#.#...#.#...#.....#.#.............#.....#...#.#.# |
|||
###############.###.#.###.###.###.###.#####.#.#.#.#####.#####.#.#.#########.#########.###.#####.#####.#.#.#.#.### |
|||
#.............#.#...#.#.....#.#.........#.#...#.....#.......#.#.#.......#.......#...#.#...#...#.#.#...#...#.#.#.# |
|||
#########.###.#######.#.#.#######.#####.#.#.#.#.#######.###.###.#####.#.#.###.###.###.#####.#####.#.###.#####.#.# |
|||
#.........#.........#.#.#.#...#...#.....#...#.#.#...#.#...#.#.#.#...#.#.#.#.#.#...#.#...#...#.......#.#.#...#.#.# |
|||
###############.###.#######.#########.#.#.###.#.###.#.#.###.#.#.#.#.###.#.#.#####.#.#.###.###.#######.#####.#.#.# |
|||
#.#...#.#.#.....#.......#.....#.#...#.#.#.#.......#.#.....#.#...#.#.....#...#.#.#.#...#.#.....#.#.#.......#.#.#.# |
|||
#.#.###.#.#.#########.#######.#.#.###.###.###.#.#.#.###.###.###.#.###.###.#.#.#.#.#.###.#.#.#.#.#.#.#######.#.#.# |
|||
#...........#.........#.#.#...........#.....#.#.#.#.......#.#...#.#.#.#...#...............#.#.#.........#.......# |
|||
#######.#########.###.#.#.#######.#######.###.#.#.###.#.#######.#.#.###.#######.#######.#######.#########.#####.# |
|||
#.#...#.#...#.#.#.#.........#...#.......#...#.#.#.#.#.#...#.#...#.....#...#.....#...#.#.#...#.#.#.#.#.#.......#.# |
|||
#.#.#######.#.#.#####.###.#.###.#.#####.#.#####.#.#.#.#.###.#.#.#.###.#####.###.#.#.#.###.###.#.#.#.#.###.#.##### |
|||
#.#.......#.#.........#...#.........#.#.#.#.#...#...#.#...#...#.#.#...#.....#.#...#.#.#...#...#.......#.#.#.#.#.# |
|||
#.#.#.#.###.###.#.#.#.#####.#.#.#####.#.#.#.###########.#####.#.#.#######.###.#.#.###.###.#.###.#######.#.###.#.# |
|||
#.#.#.#...#.#.#.#.#.#.#.#...#.#.#.......#.#.#.#.#.#.#...#.....#.#...#...#.....#.#.#.#.....#.#...........#...#...# |
|||
#.#####.###.#.#########.#.###.#######.#.#.#.#.#.#.#.###.###.###.###.#.#####.#####.#.#####.#.###.###.###.#.###.#.# |
|||
#.#.........#.#.#.#.#...#.#.#.....#.#.#.#...#...#.......#...#.....#...#.#.......#.#.#.#.....#.....#...#.#.....#.# |
|||
#.#####.#####.#.#.#.#.#####.#######.###.#.#.#.#.###.###.###.###.###.###.#.#########.#.###.#####.#.#######.####### |
|||
#.#...#.#.#.#...#.....#.#.#.#.#.#.#.#.#.#.#...#.#.....#.#.#.#.....#.....#...#.........#.#.#.#.#.#.#.#.#...#...#.# |
|||
#.#.###.#.#.#.#######.#.#.#.#.#.#.#.#.#.###.###.#.#####.#.#####.#######.###.#.#.#.#####.#.#.#.#.###.#.###.#.#.#.# |
|||
#.#...........#...#.#.....#.#.#.........#...#...#.....#.#...#.#...#.....#.....#.#.#.#...#...#.....#...#.....#...# |
|||
#.#.###.#########.#.#.#####.#.###.#.###.###.#.#####.#####.###.#.#.#.#.###.#####.###.###.#.###.#####.#####.####### |
|||
#...#.#.....#.#...#.#.#.....#.#.#.#.#.....#.#...#.......#.......#.#.#...#.....#.#.........#.#.#.#.....#.#...#.#.# |
|||
###.#.#######.#.###.#.#####.#.#.#######.#.###.###.#.#####.#########.###.#.#########.###.###.#.#.#.#####.#.###.#.# |
|||
#.#.#.......#.....#.#...#.#.....#.#.....#.#...#.#.#.#.......#...#.#...#.#.#.#.....#.#.#...#.#.......#.......#.#.# |
|||
#.#.#.#######.###.#.#.###.#.#####.###.#.#####.#.###.#.#####.#.#.#.#.#.###.#.###.#####.###.#.#.###.###.#.#####.#.# |
|||
#...#.#.#...#...#.#...#...#.#.#.......#...#.....#...#...#.....#...#.#.#.............#.#.#.#...#.#.#.#.#.....#...# |
|||
###.#.#.###.#####.#.###.###.#.#########.#######.#.#######.#####.###.#########.#######.#.#.#.#.#.###.###.#######.# |
|||
#.......#.....#.#.#...#.#...#.# J Q E P W J M #.....#...#.#.#...#.....#.....# |
|||
###.#######.#.#.#.#.###.###.#.# M I A Q C K Q ###.###.#####.#.#.###.#####.### |
|||
#...#.#.#...#.#.#.............# #...#.........#.#.............# |
|||
###.#.#.###.###.###.#######.#.# #.#.#.#####.#####.###.###.#.### |
|||
SF........#.......#.#...#.#...#.# YL..#...#...#.#...#...#.#.#.#.#..WC |
|||
#.###.#########.#.###.#.#.##### #.###.#.#.#.#.#########.#.#.#.# |
|||
#.#.......#...#.....#...#.....# #.#...#.#...#.#.#.#.#.....#...# |
|||
#.#####.###.#.#.###.#.#######.# #.#.#.#.#####.#.#.#.#.#######.# |
|||
#.#...#.....#...#.......#......SL #.#.#.#.................#.#.#.# |
|||
#####.######################### #.#######################.#.### |
|||
#...................#...#...#.# #.#...................#.......# |
|||
###.#.###.#####.###.#.#.###.#.# #####.###.###.#######.#.#.#.#.# |
|||
YY..#.#.#.......#.#.....#...#....OB VN..#.#.#.#.#.......#.....#.#.#.# |
|||
#.#.#######.###.#.#####.#####.# #.#.#.#.#####.###.###.#.#####.# |
|||
#.#...#.#...#.#.#.#.....#.#...# #.......#.....#.#.#.#.#.....#..HV |
|||
#.#.#.#.###.#.###.#####.#.###.# #####.#######.#.###.########### |
|||
#...#...#.#...#.....#.........# #...#.#.#.......#.....#.#...#.# |
|||
#####.###.###############.##### #.#.###.#####.#.###.#.#.#.###.# |
|||
#.#.#.#.#...#...#.#.#...#.#...# SF..#.#.......#.#.#.#.#.#...#...# |
|||
#.#.###.#.###.###.#.#.#.#####.# #.#.###.#########.#.#.#.#.#.#.# |
|||
#.#...........#.#.#...#........HI #.#.....#...#...#...#.#.#.#.#..EA |
|||
#.#.#####.#####.#.###.#####.#.# #######.#.###.#####.#.#.#.#.### |
|||
YL..#...#...#...#.#.#.....#.#.#.# #.#...#.............#...#...#.# |
|||
#.#.###.###.###.#.###.###.#.### #.#.#########.###.#.###.#.###.# |
|||
#.....#.................#.....# #.#.....#...#...#.#.#...#.#...# |
|||
#####################.#####.### #.#.#.#.#.###########.#.###.#.# |
|||
ZZ............#.......#...#.#...# #...#.#...#.#.#.#...#.#.#...#.# |
|||
#.#######.#.#.#####.#####.##### #.#.###.###.#.#.#.#########.#.# |
|||
#...#.#...#.....#.....#.....#.# EJ..#.#.....#.#...#.#.....#...#.# |
|||
#.###.#############.#####.###.# #########.#.###.#.###.###.#.#.# |
|||
#.......#.....#.#.#...#.....#..PD #.........................#.#..PD |
|||
#.###.###.#.###.#.###.#.#.###.# #######################.#.##### |
|||
MQ..#.#...#.#.#.#...#.....#.....# #...#.#.#.......#.....#.#.....# |
|||
###.#######.#.#.############### #.###.#.#.#####.#.###.######### |
|||
#.......#...#.......#.........# #.#.#...#...#.....#...#...#...# |
|||
#.#.###.#.#.#.###.#.#.#.###.#.# #.#.###.###.###.#.#.#####.###.# |
|||
OB..#.#.....#...#...#...#.#...#.# PT....#...#.....#.#.#.#...#...#..LH |
|||
#########.#.###.###.#########.# #.#.#.#.#.#####.###.#.#.#.###.# |
|||
#.....#.#.#.#...#.......#.#.#..GJ #.#...#.......#...#...#.......# |
|||
###.###.#################.#.### #.#####.#.#.################### |
|||
#...#.......#.#...#.#.#.......# #.#...#.#.#.#.#.........#.....# |
|||
#.#.#.#####.#.#.###.#.###.##### #.###.#######.#####.#.#.#.###.# |
|||
#.#.#.#...#.......#.#.#.#...#.# #...#...#.....#.#...#.#.#...#.# |
|||
#.#.#.#.#.###.#####.#.#.###.#.# #####.###.###.#.#.###.#.###.#.# |
|||
DU..#.#...#.#.....#.........#...# #.......#.#...#.#...#.#.#...#..BC |
|||
###.#####.#.#######.#.#####.### ###.#.#.#####.#.###.###.#.#.#.# |
|||
#.#.......#.........#..........LH YY....#.#...............#...#.#.# |
|||
#.#####.#.#.###.#####.#####.#.# #.#####.###.#.################# |
|||
#...#.#.#.#.#.#.#.#.......#.#.# #...#.....#.#.#...#.........#.# |
|||
#.#.#.#######.###.#.###.####### ###.#############.###.#####.#.# |
|||
GJ..#...#.#.......#...#.#.#...#.# HV..#.#.#.#...#.....#...#...#.#.# |
|||
###.###.#####.#.#####.#####.#.# #.###.#.###.#####.###.#.#.#.#.# |
|||
#...#.#...#...#.#.#.#...#.#...# #.......#.....#.#...#...#.#.#..EJ |
|||
#.###.###.#####.#.#.###.#.#.#.# #.###.#####.###.#.#.#####.#.#.# |
|||
#...........................#..BB #...#.............#.......#...# |
|||
###.###.#.#.#.#.#.#.###.#.###.# #.###.#######.#####.###.#.#.#.# |
|||
#...#...#.#.#.#.#.#...#.#...#.# #.#.#.#.#.#.....#.#...#.#.#.#.# |
|||
#.#.###.###########.#.#.###.### J B I D P F #.#.#.#.#.###.###.#.#####.#.### |
|||
#.#...#.....#.#.#...#.#.#.#.#.# H C P U A K #.#.....#.#...#.........#.#...# |
|||
#.#####.#.###.#.#####.#.#.###.#########.#####.#########.###.#######.#######.#############.#.#.#.#.###.###.#.##### |
|||
#...#...#.....#.......#...#.#.#.........#.....#.#.....#.#...#.....#.#...#...........#.....#...#.#...#.#...#.....# |
|||
#.#######.###.#########.###.#.#.###.###.###.###.#.###.#.#.###.#####.#.#.###.###.###.#####.###.#.#.###.########### |
|||
#.....#...#.#.....#.#.....#...#...#...#...#.....#...#...#.....#.#...#.#.#...#...#.#...#.....#.#.#.#.........#...# |
|||
#.#.###.###.###.###.#########.#.###.###.#####.###.#.#.###.###.#.###.#.#.#####.###.#.#####.#.#########.#####.#.### |
|||
#.#.#.......#...#.......#.......#.#...#.#.#.#.#.#.#.#.#...#.#.....#...#.#.#...#.#.#.....#.#.....#.#.......#.....# |
|||
###.#.###.#.#######.###.#.###.###.###.###.#.#.#.#.#.#.#.###.###.###.###.#.###.#.#.#.#.#.#.#.#####.#.#.###.#.#.### |
|||
#...#...#.#...#.....#.......#.#.....#...#.....#...#.#.#.#.........#.#.....#...#.#.#.#.#.#.#.....#...#...#.#.#.#.# |
|||
#.#.###########.###########.#####.#.###.#.#######.###.#.###.#######.#.#######.#.#.#.#.#######.#.###.###.#.#.#.#.# |
|||
#.#.........#...#.#.........#...#.#.....#.#...#.#...#.#.#.#.....#.#.#...#.#.......#.#...#.....#...#.#...#.#.#...# |
|||
###.#.#.###.#####.#.###.###.###.#.#.#####.#.#.#.#.#######.#.#.#.#.#####.#.#####.#####.#.###.#.#.###.#########.#.# |
|||
#...#.#.#.#.#.#.....#...#.#.#.....#.#.#.....#...#.....#.....#.#.....#...#...........#.#...#.#.#.#.#...#.......#.# |
|||
#.#.#####.###.#####.#.#.#.#######.###.###.#####.#.#############.#####.###.#.#.#.#####.#########.#.###.#########.# |
|||
#.#...#.#...........#.#.#.#.#...#.....#.....#.#.#.#.....#.#...#.#.......#.#.#.#.#...#.#...#.........#.......#...# |
|||
#.#.#.#.###########.#####.#.#.###.#.#########.#.#.#.#####.###.#.#.###.###.#######.#######.#.#.###.###.#####.###.# |
|||
#.#.#...#...#.........#.#.........#.....#.#.....#.#.#...#.#.#...#...#.#...................#.#.#...#...#.....#...# |
|||
###.###.#.#####.#.#.###.#####.###.#######.###.#.#.###.#.#.#.#.###.###.#.#.###.###.###.#######.###.###.###.#####.# |
|||
#.....#.#.......#.#.#...#.#...#.......#...#...#.#.....#.#...#.#...#.#.#.#...#.#.....#.....#.#.#.#...#.#.......#.# |
|||
#.#####.#####.#.#######.#.#.#.###.#####.#######.#######.#.#.#.###.#.#####.#######.###.#####.###.#####.###.#.#.#.# |
|||
#.#.#.#.#.#...#.#.......#...#...#.........#.#.....#.....#.#.....#...#...#...#.#.....#.#.#...#.....#.#.#...#.#.#.# |
|||
#.#.#.#.#.#.###.###.#.###.#############.###.###.###.#.###.#######.#####.#.###.#########.#.#####.###.#####.###.#.# |
|||
#.....#.#...#...#...#.......#.............#...#.#...#...#.#.....#.#.....#...#.#.#.#.#...#...#.#.#.#.#.#.#...#.#.# |
|||
#.###.#.#######.###.###.###.#####.###########.#.###.#.###.###.#.#.#####.#.#.#.#.#.#.###.#.###.#.#.#.#.#.#.###.### |
|||
#...#.#...#.#...#...#.#.#.#...#...#.......#.#.....#.#.#.......#.#.......#.#.#.#.#.....#.#...#.#.......#.....#...# |
|||
#.#########.#########.###.#.###.#.#.#.#.###.#.#.###.###.###.###.###.#.#####.#.#.#.#####.#.###.#.#######.#####.### |
|||
#.....#.#.....................#.#.#.#.#.#.....#.#.....#.#...#.....#.#...#.#.....#.#...#...#.#.......#.#...#.....# |
|||
#.#.###.#.###.###.###.#.#########.###.#.###.###.###.#.#######.#######.###.#####.#.#.###.###.#.###.###.#.###.##### |
|||
#.#.....#.#.....#.#.#.#.#.............#.#...#.....#.#...#...#.......#.#.........................#.....#.#.......# |
|||
#.#.#####.#.###.#.#.#.#.#######.#.#####.###.#.#.###.#.#.#.#.###.#####.#####.###.#.#.###.#####.###.#.#.#######.#.# |
|||
#.#.....#.#.#...#.#...#.#.......#.....#...#.#.#.#...#.#.#.#.......#.....#.....#.#.#.#.......#...#.#.#.....#...#.# |
|||
#######################################.#######.#.#.#####.###########.#######.################################### |
|||
P V P A J F H |
|||
A N T A K K I |
File diff suppressed because one or more lines are too long
@ -0,0 +1,71 @@ |
|||
bignum = require "_openssl.bignum" |
|||
|
|||
function find(card, deck, times) |
|||
local revert = false |
|||
local increment = bignum.new(0) |
|||
local multiply = bignum.new(1) |
|||
for line in io.lines(arg[1]) do |
|||
if line:find("^deal into new stack$") then |
|||
revert = not revert |
|||
elseif line:find("^cut %-?%d+$") then |
|||
local val = tonumber(line:match("^cut (%-?%d+)$")) |
|||
if not revert then |
|||
increment = (increment - val) % deck |
|||
else |
|||
increment = (increment + val) % deck |
|||
end |
|||
elseif line:find("^deal with increment %d+$") then |
|||
local val = tonumber(line:match("^deal with increment (%d+)$")) |
|||
multiply = (multiply * val) % deck |
|||
increment = (increment * val) % deck |
|||
if revert then |
|||
increment = (increment + (val - 1)) % deck |
|||
end |
|||
end |
|||
end |
|||
|
|||
print(card, multiply, increment, deck) |
|||
card = (card * multiply * times + increment * times) % deck |
|||
if revert then |
|||
card = deck - (card + 1) |
|||
end |
|||
return card |
|||
end |
|||
|
|||
function revfind(card, deck, times) |
|||
local revert = false |
|||
local increment = bignum.new(0) |
|||
local multiply = bignum.new(1) |
|||
for line in io.lines(arg[1]) do |
|||
if line:find("^deal into new stack$") then |
|||
revert = not revert |
|||
elseif line:find("^cut %-?%d+$") then |
|||
local val = tonumber(line:match("^cut (%-?%d+)$")) |
|||
if not revert then |
|||
increment = (increment - val) % deck |
|||
else |
|||
increment = (increment + val) % deck |
|||
end |
|||
elseif line:find("^deal with increment %d+$") then |
|||
local val = tonumber(line:match("^deal with increment (%d+)$")) |
|||
multiply = (multiply * val) % deck |
|||
increment = (increment * val) % deck |
|||
if revert then |
|||
increment = (increment + (val - 1)) % deck |
|||
end |
|||
end |
|||
end |
|||
|
|||
print(card, multiply, increment, deck) |
|||
card = (card * multiply * times + increment * times) % deck |
|||
if revert then |
|||
card = deck - (card + 1) |
|||
end |
|||
return card |
|||
end |
|||
|
|||
print("Part 1:", find(2019, 10007, 1)) |
|||
print("Part 2:", find(2020, 119315717514047, 101741582076661)) |
|||
|
|||
print("Part 1:", revfind(7744, 10007, 1)) |
|||
print("Part 2:", revfind(2020, 119315717514047, 101741582076661)) |
@ -0,0 +1 @@ |
|||
https://boards.4channel.org/g/thread/74086545#p74095430 |
@ -0,0 +1,100 @@ |
|||
deal with increment 24 |
|||
cut -9655 |
|||
deal with increment 20 |
|||
cut -3052 |
|||
deal with increment 14 |
|||
deal into new stack |
|||
deal with increment 12 |
|||
cut 2041 |
|||
deal into new stack |
|||
deal with increment 13 |
|||
cut -5574 |
|||
deal into new stack |
|||
deal with increment 52 |
|||
cut 2735 |
|||
deal with increment 14 |
|||
deal into new stack |
|||
deal with increment 72 |
|||
deal into new stack |
|||
deal with increment 11 |
|||
cut -7008 |
|||
deal with increment 7 |
|||
cut -3920 |
|||
deal into new stack |
|||
deal with increment 68 |
|||
cut -7497 |
|||
deal with increment 7 |
|||
cut 8878 |
|||
deal with increment 39 |
|||
cut -3407 |
|||
deal with increment 74 |
|||
cut -3728 |
|||
deal into new stack |
|||
cut 483 |
|||
deal with increment 55 |
|||
cut 8147 |
|||
deal with increment 48 |
|||
cut 5734 |
|||
deal with increment 35 |
|||
deal into new stack |
|||
deal with increment 53 |
|||
deal into new stack |
|||
cut 9833 |
|||
deal with increment 21 |
|||
cut -1328 |
|||
deal with increment 29 |
|||
cut 469 |
|||
deal with increment 34 |
|||
deal into new stack |
|||
deal with increment 50 |
|||
cut 8218 |
|||
deal with increment 8 |
|||
cut 1546 |
|||
deal with increment 27 |
|||
cut 3699 |
|||
deal with increment 44 |
|||
cut 1167 |
|||
deal into new stack |
|||
cut -9744 |
|||
deal with increment 71 |
|||
cut -6111 |
|||
deal with increment 19 |
|||
cut 2592 |
|||
deal with increment 17 |
|||
cut 3257 |
|||
deal with increment 11 |
|||
cut 4618 |
|||
deal with increment 64 |
|||
deal into new stack |
|||
cut -1513 |
|||
deal into new stack |
|||
cut -2976 |
|||
deal with increment 58 |
|||
cut 2744 |
|||
deal with increment 4 |
|||
cut 6408 |
|||
deal with increment 66 |
|||
cut 5182 |
|||
deal with increment 6 |
|||
cut -1767 |
|||
deal with increment 12 |
|||
cut -7805 |
|||
deal with increment 45 |
|||
cut -4126 |
|||
deal with increment 52 |
|||
cut 2112 |
|||
deal with increment 35 |
|||
cut 863 |
|||
deal with increment 55 |
|||
cut 3159 |
|||
deal with increment 67 |
|||
deal into new stack |
|||
deal with increment 65 |
|||
cut -3194 |
|||
deal with increment 69 |
|||
cut 5695 |
|||
deal with increment 7 |
|||
cut -5035 |
|||
deal with increment 30 |
|||
cut -2282 |
|||
deal with increment 70 |
@ -0,0 +1,82 @@ |
|||
field = {} |
|||
for line in io.lines(arg[1]) do |
|||
row = {} |
|||
for char in line:gmatch(".") do |
|||
table.insert(row, char) |
|||
end |
|||
table.insert(field, row) |
|||
end |
|||
|
|||
function hash(field) |
|||
local res = 0 |
|||
|
|||
for y, row in ipairs(field) do |
|||
for x, cell in ipairs(row) do |
|||
local data = 0 |
|||
if cell == "#" then |
|||
data = 2 ^ ((y - 1) * #row + (x - 1)) |
|||
end |
|||
res = res + data |
|||
end |
|||
end |
|||
|
|||
return res |
|||
end |
|||
|
|||
|
|||
function next_field(field) |
|||
local new = {} |
|||
for y, row in ipairs(field) do |
|||
local new_row = {} |
|||
for x, cell in ipairs(row) do |
|||
local adjacent = 0 |
|||
if field[y - 1] and field[y - 1][x] == "#" then |
|||
adjacent = adjacent + 1 |
|||
end |
|||
if field[y + 1] and field[y + 1][x] == "#" then |
|||
adjacent = adjacent + 1 |
|||
end |
|||
if field[y][x - 1] == "#" then |
|||
adjacent = adjacent + 1 |
|||
end |
|||
if field[y][x + 1] == "#" then |
|||
adjacent = adjacent + 1 |
|||
end |
|||
if cell == "#" then |
|||
if adjacent ~= 1 then |
|||
cell = "." |
|||
end |
|||
else |
|||
if adjacent == 1 or adjacent == 2 then |
|||
cell = "#" |
|||
end |
|||
end |
|||
table.insert(new_row, cell) |
|||
end |
|||
table.insert(new, new_row) |
|||
end |
|||
return new |
|||
end |
|||
|
|||
field_orig = {table.unpack(field)} |
|||
|
|||
hashes = {} |
|||
while true do |
|||
cur_hash = hash(field) |
|||
if hashes[cur_hash] then |
|||
break |
|||
end |
|||
hashes[cur_hash] = true |
|||
field = next_field(field) |
|||
end |
|||
|
|||
for _, row in ipairs(field) do |
|||
for _, cell in ipairs(row) do |
|||
io.stdout:write(cell) |
|||
end |
|||
io.stdout:write("\n") |
|||
end |
|||
|
|||
print("Part 1:", hash(field)) |
|||
|
|||
field = field_orig |
@ -0,0 +1,5 @@ |
|||
#..## |
|||
##... |
|||
.#.#. |
|||
##### |
|||
####. |
@ -0,0 +1,5 @@ |
|||
....# |
|||
#..#. |
|||
#..## |
|||
..#.. |
|||
#.... |
Loading…
Reference in new issue