-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-1.lua
53 lines (50 loc) · 1.54 KB
/
day-1.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
require "lua-string"
local linq = require("lazylualinq")
local client = require("client")
local rex = require("rex_pcre2")
return {
init = function(self) end,
puzzle1 = function(self)
return linq(client:getDayInput(1):trim():split("\n"))
:select(function(line)
return linq.iterator(line:gmatch("%d")):toArray()
end)
:select(function(linechars)
return tonumber(linechars[1]) * 10 + tonumber(linechars[#linechars])
end)
:sum()
end,
puzzle2 = function(self)
return linq(client:getDayInput(1):trim():split("\n"))
:select(function(line)
return linq(
rex.match(
line,
"(\\d|one|two|three|four|five|six|seven|eight|nine)"
),
rex.match(
line,
".+(\\d|one|two|three|four|five|six|seven|eight|nine)"
)
):toArray()
end)
:select(function(lineDigits)
return 10 * self:parseDigit(lineDigits[1]) + self:parseDigit(lineDigits[#lineDigits])
end)
:sum()
end,
digitLookup = {
one = 1,
two = 2,
three = 3,
four = 4,
five = 5,
six = 6,
seven = 7,
eight = 8,
nine = 9,
},
parseDigit = function(self, digitString)
return self.digitLookup[digitString] or tonumber(digitString)
end,
}