-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.lua
55 lines (42 loc) · 1.24 KB
/
day3.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package.path = package.path .. ";../?.lua"
local utils = require "utils"
local total1 = 0
for _, line in ipairs(utils.lines) do
local mtch = utils.extractMatches(line, "mul%((%d+,%d+)%)")
local nums = {}
for i = 1, #mtch do
table.insert(nums, utils.split(mtch[i], ","))
nums[i][1] = tonumber(nums[i][1])
nums[i][2] = tonumber(nums[i][2])
end
for i, _ in ipairs(nums) do
total1 = total1 + (nums[i][1] * nums[i][2])
end
end
print("Part 1: "..total1)
local total2 = 0
local mul = true
for _, line in ipairs(utils.lines) do
local statement = ""
local nums = {}
local mul_pattern = "mul%((%d+,%d+)%)$"
for chr in string.gmatch(line, ".") do
statement = statement..chr
if string.match(statement, "do%(%)$") then
mul = true
elseif string.match(statement, "don't%(%)$") then
mul = false
elseif mul and string.match(statement, mul_pattern) then
local mtch = utils.extractMatches(statement, mul_pattern)
table.insert(nums, utils.split(mtch[#mtch], ","))
end
for i = 1, #nums do
nums[i][1] = tonumber(nums[i][1])
nums[i][2] = tonumber(nums[i][2])
end
end
for i, _ in ipairs(nums) do
total2 = total2 + (nums[i][1] * nums[i][2])
end
end
print("Part 2: "..total2)