Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.1 kB · 69 lines
Lua
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970local input = io.open("input.txt", "r")
local map = {}
local deliveries = 1
local santa_x = 0local santa_y = 0
local robot_x = 0local robot_y = 0
local mode = true
local function init_pos(x, y) if type(map[x]) ~= "table" then map[x] = {} endend
local function get_pos() if mode then return santa_x, santa_y else return robot_x, robot_y endend
local function move(dx, dy) if mode then santa_x = santa_x + dx santa_y = santa_y + dy else robot_x = robot_x + dx robot_y = robot_y + dy end
mode = not modeend
local function deliver() local x, y = get_pos()
init_pos(x, y)
if not map[x][y] then map[x][y] = true deliveries = deliveries + 1 endend
map[santa_x] = {}map[santa_x][santa_y] = true
local char
repeat char = input:read(1)
if char == "^" then move(0, 1) elseif char == "v" then move(0, -1) elseif char == "<" then move(-1, 0) elseif char == ">" then move(1, 0) end
deliver()until not char
print("Delivered at least once to " .. deliveries .. " houses")
input:close()