From eeaffa5fa6e5f9f8c42c931eadb9787368bbeac5 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 25 Dec 2020 15:52:04 +0100 Subject: Add day 24 --- 24/a.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 24/a.lua (limited to '24/a.lua') diff --git a/24/a.lua b/24/a.lua new file mode 100644 index 0000000..dbf6868 --- /dev/null +++ b/24/a.lua @@ -0,0 +1,72 @@ +tiles = {} + +directions = {'se', 'sw', 'ne', 'nw', 'e', 'w'} + +function navigate(v, h, direction) + if direction == 'e' then + return v, h+1 + elseif direction == 'w' then + return v, h-1 + end + + local even = v % 2 == 0 + local new_v = v + local new_h = h + + if direction:sub(1, 1) == 'n' then + new_v = new_v - 1 + else + new_v = new_v + 1 + end + + + if even and direction:sub(2, 2) == 'e' then + new_h = new_h + 1 + elseif not even and direction:sub(2, 2) == 'w' then + new_h = new_h - 1 + end + + return new_v, new_h +end + +function parse_tile(line) + local vertical = '' + local tile = {} + for i = 1, #line do + local current = line:sub(i, i) + if current == 'n' or current == 's' then + vertical = current + else + table.insert(tile, vertical .. current) + vertical = '' + end + end + + return tile +end + +for line in io.lines('input.txt') do + local tile = parse_tile(line) + local v = 0 + local h = 0 + + for _, direction in pairs(tile) do + v, h = navigate(v, h, direction) + end + + if not tiles[v] then tiles[v] = {} end + if not tiles[v][h] then tiles[v][h] = 0 end + + tiles[v][h] = tiles[v][h] + 1 +end + +count = 0 +for _, row in pairs(tiles) do + for _, flips in pairs(row) do + if flips % 2 == 1 then + count = count + 1 + end + end +end + +print(count) -- cgit v1.2.3