Code for the Advent of Code event
aoc advent-of-code
at main 69 lines 1.1 kB view raw
1local input = io.open("input.txt", "r") 2 3local map = {} 4 5local deliveries = 1 6 7local santa_x = 0 8local santa_y = 0 9 10local robot_x = 0 11local robot_y = 0 12 13local mode = true 14 15local function init_pos(x, y) 16 if type(map[x]) ~= "table" then map[x] = {} end 17end 18 19local function get_pos() 20 if mode then return santa_x, santa_y else return robot_x, robot_y end 21end 22 23local function move(dx, dy) 24 if mode then 25 santa_x = santa_x + dx 26 santa_y = santa_y + dy 27 else 28 robot_x = robot_x + dx 29 robot_y = robot_y + dy 30 end 31 32 mode = not mode 33end 34 35local function deliver() 36 local x, y = get_pos() 37 38 init_pos(x, y) 39 40 if not map[x][y] then 41 map[x][y] = true 42 deliveries = deliveries + 1 43 end 44end 45 46map[santa_x] = {} 47map[santa_x][santa_y] = true 48 49local char 50 51repeat 52 char = input:read(1) 53 54 if char == "^" then 55 move(0, 1) 56 elseif char == "v" then 57 move(0, -1) 58 elseif char == "<" then 59 move(-1, 0) 60 elseif char == ">" then 61 move(1, 0) 62 end 63 64 deliver() 65until not char 66 67print("Delivered at least once to " .. deliveries .. " houses") 68 69input:close()