@@ 0,0 1,41 @@
+io.input(arg[1])
+local line = io.read()
+local function hash(s)
+ local h = 0
+ for i = 1, #s do
+ h = ((h + string.byte(s, i)) * 17) & 0xff
+ end
+ return h
+end
+local map, ans1, ans2 = {}, 0, 0
+for step, label, op, val in line:gmatch('((%a+)([-=])(%d*))') do
+ ans1 = ans1 + hash(step)
+ local h = hash(label)
+ local box = map[h]
+ if not box then box = {} map[h] = box end
+ if op == '-' then
+ for i, t in ipairs(box) do
+ if t[1] == label then
+ table.remove(box, i)
+ break
+ end
+ end
+ else
+ local found
+ for i, t in ipairs(box) do
+ if t[1] == label then
+ t[2] = val
+ found = true
+ end
+ end
+ if not found then
+ table.insert(box, {label, val})
+ end
+ end
+end
+for i, box in pairs(map) do
+ for j, t in ipairs(box) do
+ ans2 = ans2 + (i + 1) * j * t[2]
+ end
+end
+print(ans1, ans2)
@@ 4,7 4,7 @@
LUA?=lua
.PHONY: all
-all: 1 2 3 4 5 6 7 9 10 11 13 14
+all: 1 2 3 4 5 6 7 9 10 11 13 14 15
.DEFAULT:
@printf 'day %s\t' $< && $(LUA) $<.lua < $<.txt