From 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Fri, 7 Feb 2025 12:49:48 -0600 Subject: inital commit --- 03/2.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 03/2.lua (limited to '03/2.lua') diff --git a/03/2.lua b/03/2.lua new file mode 100644 index 0000000..8c2d155 --- /dev/null +++ b/03/2.lua @@ -0,0 +1,33 @@ +#!/usr/bin/env lua +require("ext") +local items = {} +for start, finish in pairs({a = 'z', A = 'Z'}) do + for k = start:byte(), finish:byte() do + items[string.char(k)] = true + end +end + +local groups = {} +for line in io.lines() do + local expanded = {} + line:gsub("(%w)",function(w) expanded[w] = true end) + table.insert(groups,expanded) +end +local common = {} +for i = 1,#groups,3 do + for item, _ in pairs(items) do + if groups[i][item] and groups[i + 1][item] and groups[i + 2][item] then + table.insert(common, item) + break + end + end +end +local sum = 0 +for _, p in pairs(common) do + if p >= 'a' and p <= 'z' then + sum = sum + (p:byte() - ('a'):byte() + 1) + elseif p >= 'A' and p <= 'Z' then + sum = sum + (p:byte() - ('A'):byte() + 1) + 26 + end +end +print(sum) -- cgit v1.2.3-70-g09d2