diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2020-12-25 15:52:04 +0100 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2020-12-25 15:52:04 +0100 |
commit | eeaffa5fa6e5f9f8c42c931eadb9787368bbeac5 (patch) | |
tree | 8290f2656e3948ce1480524bdbb7ffedfdb15077 /24/a.lua | |
parent | d4b47d15a1a837c777c1f155d24d7c3ae111a7c5 (diff) |
Add day 24
Diffstat (limited to '24/a.lua')
-rw-r--r-- | 24/a.lua | 72 |
1 files changed, 72 insertions, 0 deletions
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) |