diff options
| author | Alex Pickering <alex@cogarr.net> | 2025-02-07 12:49:48 -0600 |
|---|---|---|
| committer | Alex Pickering <alex@cogarr.net> | 2025-02-07 12:49:48 -0600 |
| commit | 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 (patch) | |
| tree | 278876284d07118ecdea5c48cb6453f3122887f0 /03/2.lua | |
| download | advent_of_code_2022-master.tar.gz advent_of_code_2022-master.tar.bz2 advent_of_code_2022-master.zip | |
Diffstat (limited to '03/2.lua')
| -rw-r--r-- | 03/2.lua | 33 |
1 files changed, 33 insertions, 0 deletions
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) |
