From d64c2a654d96eb6cac6616f005fa468b6b2c5da3 Mon Sep 17 00:00:00 2001 From: mid-kid Date: Wed, 15 Jul 2020 22:18:02 +0200 Subject: [PATCH] Add remaining days --- d11.lua | 145 +++++++++++++++++++++++++++++++++++++++++++++ d12.lua | 53 +++++++++++++++++ d18.lua | 74 +++++++++++++++++++++++ d18_input.txt | 81 +++++++++++++++++++++++++ d20_input.txt | 125 ++++++++++++++++++++++++++++++++++++++ d21_input.txt | 1 + d22.lua | 71 ++++++++++++++++++++++ d22_assistance.txt | 1 + d22_input.txt | 100 +++++++++++++++++++++++++++++++ d24.lua | 82 +++++++++++++++++++++++++ d24_input.txt | 5 ++ test.txt | 5 ++ 12 files changed, 743 insertions(+) create mode 100644 d11.lua create mode 100644 d12.lua create mode 100644 d18.lua create mode 100644 d18_input.txt create mode 100644 d20_input.txt create mode 100644 d21_input.txt create mode 100644 d22.lua create mode 100644 d22_assistance.txt create mode 100644 d22_input.txt create mode 100644 d24.lua create mode 100644 d24_input.txt create mode 100644 test.txt diff --git a/d11.lua b/d11.lua new file mode 100644 index 0000000..fc4d37c --- /dev/null +++ b/d11.lua @@ -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 diff --git a/d12.lua b/d12.lua new file mode 100644 index 0000000..8fed1b8 --- /dev/null +++ b/d12.lua @@ -0,0 +1,53 @@ +moons = {} +for line in io.lines(arg[1]) do + x, y, z = line:match("") + 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=, vel=", 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=, vel=", 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) diff --git a/d18.lua b/d18.lua new file mode 100644 index 0000000..308f0cd --- /dev/null +++ b/d18.lua @@ -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) diff --git a/d18_input.txt b/d18_input.txt new file mode 100644 index 0000000..8fb155a --- /dev/null +++ b/d18_input.txt @@ -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..#...#...# +#.#.###.###.#.###.#####.#.#####.#.#.###.#.###.###################.#######.###.#.# +#.#.........#...........#.....#...#.....#...#.....................#...........#.# +################################################################################# diff --git a/d20_input.txt b/d20_input.txt new file mode 100644 index 0000000..df1ec1d --- /dev/null +++ b/d20_input.txt @@ -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 diff --git a/d21_input.txt b/d21_input.txt new file mode 100644 index 0000000..eafdd7c --- /dev/null +++ b/d21_input.txt @@ -0,0 +1 @@ +109,2050,21101,0,966,1,21102,1,13,0,1105,1,1378,21101,0,20,0,1106,0,1337,21101,0,27,0,1106,0,1279,1208,1,65,748,1005,748,73,1208,1,79,748,1005,748,110,1208,1,78,748,1005,748,132,1208,1,87,748,1005,748,169,1208,1,82,748,1005,748,239,21102,1041,1,1,21101,73,0,0,1105,1,1421,21101,78,0,1,21102,1041,1,2,21102,1,88,0,1105,1,1301,21101,0,68,1,21101,1041,0,2,21102,1,103,0,1106,0,1301,1101,0,1,750,1106,0,298,21102,1,82,1,21101,1041,0,2,21101,0,125,0,1106,0,1301,1101,0,2,750,1105,1,298,21101,0,79,1,21101,1041,0,2,21102,147,1,0,1106,0,1301,21101,84,0,1,21102,1041,1,2,21102,162,1,0,1105,1,1301,1101,3,0,750,1106,0,298,21101,0,65,1,21101,1041,0,2,21101,184,0,0,1106,0,1301,21101,76,0,1,21101,1041,0,2,21102,199,1,0,1106,0,1301,21102,1,75,1,21101,0,1041,2,21101,0,214,0,1106,0,1301,21102,221,1,0,1106,0,1337,21101,10,0,1,21101,0,1041,2,21101,0,236,0,1105,1,1301,1106,0,553,21101,0,85,1,21102,1,1041,2,21102,1,254,0,1105,1,1301,21101,0,78,1,21101,1041,0,2,21102,1,269,0,1106,0,1301,21101,276,0,0,1106,0,1337,21101,10,0,1,21101,1041,0,2,21101,291,0,0,1106,0,1301,1102,1,1,755,1106,0,553,21102,1,32,1,21101,0,1041,2,21102,313,1,0,1106,0,1301,21101,320,0,0,1106,0,1337,21102,1,327,0,1106,0,1279,2102,1,1,749,21101,65,0,2,21101,0,73,3,21102,346,1,0,1105,1,1889,1206,1,367,1007,749,69,748,1005,748,360,1102,1,1,756,1001,749,-64,751,1106,0,406,1008,749,74,748,1006,748,381,1101,0,-1,751,1105,1,406,1008,749,84,748,1006,748,395,1102,1,-2,751,1105,1,406,21102,1100,1,1,21102,406,1,0,1105,1,1421,21101,32,0,1,21101,1100,0,2,21101,0,421,0,1105,1,1301,21101,428,0,0,1105,1,1337,21101,435,0,0,1105,1,1279,2101,0,1,749,1008,749,74,748,1006,748,453,1101,0,-1,752,1105,1,478,1008,749,84,748,1006,748,467,1101,0,-2,752,1105,1,478,21102,1168,1,1,21101,478,0,0,1105,1,1421,21102,1,485,0,1106,0,1337,21102,1,10,1,21101,1168,0,2,21102,1,500,0,1106,0,1301,1007,920,15,748,1005,748,518,21101,0,1209,1,21102,1,518,0,1105,1,1421,1002,920,3,529,1001,529,921,529,1001,750,0,0,1001,529,1,537,1001,751,0,0,1001,537,1,545,102,1,752,0,1001,920,1,920,1105,1,13,1005,755,577,1006,756,570,21101,1100,0,1,21101,0,570,0,1106,0,1421,21101,0,987,1,1105,1,581,21102,1001,1,1,21102,1,588,0,1106,0,1378,1102,1,758,594,102,1,0,753,1006,753,654,21002,753,1,1,21102,1,610,0,1106,0,667,21101,0,0,1,21101,0,621,0,1105,1,1463,1205,1,647,21101,0,1015,1,21101,635,0,0,1105,1,1378,21102,1,1,1,21101,646,0,0,1105,1,1463,99,1001,594,1,594,1105,1,592,1006,755,664,1102,0,1,755,1106,0,647,4,754,99,109,2,1102,1,726,757,22102,1,-1,1,21102,1,9,2,21101,697,0,3,21101,0,692,0,1105,1,1913,109,-2,2105,1,0,109,2,101,0,757,706,2101,0,-1,0,1001,757,1,757,109,-2,2106,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,255,63,95,159,191,223,127,0,109,152,253,182,236,231,212,42,69,154,142,196,86,214,120,166,139,175,117,102,178,124,204,189,59,156,244,92,110,184,241,87,220,245,122,167,227,53,50,197,93,85,234,190,252,248,54,163,217,94,207,58,115,70,99,71,188,247,168,221,116,239,155,186,232,49,213,218,137,103,56,170,119,34,242,76,169,173,46,172,187,171,141,153,238,61,125,121,222,199,243,174,229,235,226,201,215,108,138,126,78,249,62,51,79,57,118,181,38,39,84,228,55,77,113,179,107,136,198,140,35,246,205,162,219,43,68,185,111,237,100,183,47,157,98,230,114,158,202,216,203,200,206,106,233,254,143,60,251,101,250,177,123,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,73,110,112,117,116,32,105,110,115,116,114,117,99,116,105,111,110,115,58,10,13,10,87,97,108,107,105,110,103,46,46,46,10,10,13,10,82,117,110,110,105,110,103,46,46,46,10,10,25,10,68,105,100,110,39,116,32,109,97,107,101,32,105,116,32,97,99,114,111,115,115,58,10,10,58,73,110,118,97,108,105,100,32,111,112,101,114,97,116,105,111,110,59,32,101,120,112,101,99,116,101,100,32,115,111,109,101,116,104,105,110,103,32,108,105,107,101,32,65,78,68,44,32,79,82,44,32,111,114,32,78,79,84,67,73,110,118,97,108,105,100,32,102,105,114,115,116,32,97,114,103,117,109,101,110,116,59,32,101,120,112,101,99,116,101,100,32,115,111,109,101,116,104,105,110,103,32,108,105,107,101,32,65,44,32,66,44,32,67,44,32,68,44,32,74,44,32,111,114,32,84,40,73,110,118,97,108,105,100,32,115,101,99,111,110,100,32,97,114,103,117,109,101,110,116,59,32,101,120,112,101,99,116,101,100,32,74,32,111,114,32,84,52,79,117,116,32,111,102,32,109,101,109,111,114,121,59,32,97,116,32,109,111,115,116,32,49,53,32,105,110,115,116,114,117,99,116,105,111,110,115,32,99,97,110,32,98,101,32,115,116,111,114,101,100,0,109,1,1005,1262,1270,3,1262,20102,1,1262,0,109,-1,2106,0,0,109,1,21102,1288,1,0,1105,1,1263,20101,0,1262,0,1101,0,0,1262,109,-1,2106,0,0,109,5,21102,1,1310,0,1106,0,1279,22102,1,1,-2,22208,-2,-4,-1,1205,-1,1332,21201,-3,0,1,21101,0,1332,0,1106,0,1421,109,-5,2106,0,0,109,2,21101,0,1346,0,1105,1,1263,21208,1,32,-1,1205,-1,1363,21208,1,9,-1,1205,-1,1363,1106,0,1373,21101,1370,0,0,1106,0,1279,1105,1,1339,109,-2,2106,0,0,109,5,1201,-4,0,1385,21001,0,0,-2,22101,1,-4,-4,21102,0,1,-3,22208,-3,-2,-1,1205,-1,1416,2201,-4,-3,1408,4,0,21201,-3,1,-3,1106,0,1396,109,-5,2106,0,0,109,2,104,10,22102,1,-1,1,21102,1,1436,0,1106,0,1378,104,10,99,109,-2,2105,1,0,109,3,20002,594,753,-1,22202,-1,-2,-1,201,-1,754,754,109,-3,2106,0,0,109,10,21101,5,0,-5,21102,1,1,-4,21102,0,1,-3,1206,-9,1555,21101,3,0,-6,21102,1,5,-7,22208,-7,-5,-8,1206,-8,1507,22208,-6,-4,-8,1206,-8,1507,104,64,1105,1,1529,1205,-6,1527,1201,-7,716,1515,21002,0,-11,-8,21201,-8,46,-8,204,-8,1105,1,1529,104,46,21201,-7,1,-7,21207,-7,22,-8,1205,-8,1488,104,10,21201,-6,-1,-6,21207,-6,0,-8,1206,-8,1484,104,10,21207,-4,1,-8,1206,-8,1569,21102,0,1,-9,1106,0,1689,21208,-5,21,-8,1206,-8,1583,21102,1,1,-9,1105,1,1689,1201,-5,716,1588,21001,0,0,-2,21208,-4,1,-1,22202,-2,-1,-1,1205,-2,1613,22101,0,-5,1,21101,1613,0,0,1105,1,1444,1206,-1,1634,22101,0,-5,1,21101,1627,0,0,1105,1,1694,1206,1,1634,21102,1,2,-3,22107,1,-4,-8,22201,-1,-8,-8,1206,-8,1649,21201,-5,1,-5,1206,-3,1663,21201,-3,-1,-3,21201,-4,1,-4,1106,0,1667,21201,-4,-1,-4,21208,-4,0,-1,1201,-5,716,1676,22002,0,-1,-1,1206,-1,1686,21101,1,0,-4,1106,0,1477,109,-10,2106,0,0,109,11,21101,0,0,-6,21102,1,0,-8,21101,0,0,-7,20208,-6,920,-9,1205,-9,1880,21202,-6,3,-9,1201,-9,921,1724,21002,0,1,-5,1001,1724,1,1732,21001,0,0,-4,21201,-4,0,1,21102,1,1,2,21102,9,1,3,21102,1754,1,0,1105,1,1889,1206,1,1772,2201,-10,-4,1766,1001,1766,716,1766,21001,0,0,-3,1106,0,1790,21208,-4,-1,-9,1206,-9,1786,22101,0,-8,-3,1106,0,1790,22102,1,-7,-3,1001,1732,1,1796,20102,1,0,-2,21208,-2,-1,-9,1206,-9,1812,22102,1,-8,-1,1105,1,1816,21202,-7,1,-1,21208,-5,1,-9,1205,-9,1837,21208,-5,2,-9,1205,-9,1844,21208,-3,0,-1,1105,1,1855,22202,-3,-1,-1,1106,0,1855,22201,-3,-1,-1,22107,0,-1,-1,1106,0,1855,21208,-2,-1,-9,1206,-9,1869,21202,-1,1,-8,1105,1,1873,21202,-1,1,-7,21201,-6,1,-6,1106,0,1708,21201,-8,0,-10,109,-11,2105,1,0,109,7,22207,-6,-5,-3,22207,-4,-6,-2,22201,-3,-2,-1,21208,-1,0,-6,109,-7,2106,0,0,0,109,5,1202,-2,1,1912,21207,-4,0,-1,1206,-1,1930,21101,0,0,-4,22101,0,-4,1,21202,-3,1,2,21102,1,1,3,21102,1949,1,0,1106,0,1954,109,-5,2105,1,0,109,6,21207,-4,1,-1,1206,-1,1977,22207,-5,-3,-1,1206,-1,1977,21201,-5,0,-5,1105,1,2045,21202,-5,1,1,21201,-4,-1,2,21202,-3,2,3,21102,1,1996,0,1105,1,1954,21201,1,0,-5,21102,1,1,-2,22207,-5,-3,-1,1206,-1,2015,21101,0,0,-2,22202,-3,-2,-3,22107,0,-4,-1,1206,-1,2037,22102,1,-2,1,21101,2037,0,0,106,0,1912,21202,-3,-1,-3,22201,-5,-3,-5,109,-6,2106,0,0 diff --git a/d22.lua b/d22.lua new file mode 100644 index 0000000..3edbb41 --- /dev/null +++ b/d22.lua @@ -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)) diff --git a/d22_assistance.txt b/d22_assistance.txt new file mode 100644 index 0000000..b208886 --- /dev/null +++ b/d22_assistance.txt @@ -0,0 +1 @@ +https://boards.4channel.org/g/thread/74086545#p74095430 diff --git a/d22_input.txt b/d22_input.txt new file mode 100644 index 0000000..589edee --- /dev/null +++ b/d22_input.txt @@ -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 diff --git a/d24.lua b/d24.lua new file mode 100644 index 0000000..1a50d47 --- /dev/null +++ b/d24.lua @@ -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 diff --git a/d24_input.txt b/d24_input.txt new file mode 100644 index 0000000..4f21dbc --- /dev/null +++ b/d24_input.txt @@ -0,0 +1,5 @@ +#..## +##... +.#.#. +##### +####. diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..704a112 --- /dev/null +++ b/test.txt @@ -0,0 +1,5 @@ +....# +#..#. +#..## +..#.. +#....