-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-6.lua
35 lines (29 loc) · 1.31 KB
/
day-6.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
require "lua-string"
local linq = require("lazylualinq")
local client = require("client")
return {
day = 0,
init = function(self, day)
self.day = day
end,
puzzle1 = function(self)
local rawData = client:getDayInput(self.day):trim():split("\n")
local times = linq(rawData[1]:split("%s+", true)):skip(1):select("v => tonumber(v)"):toArray()
local distances = linq(rawData[2]:split("%s+", true)):skip(1):select("v => tonumber(v)"):toArray()
return linq(times):zip(linq(distances), function(time, _, distance)
return { time = time, distance = distance }
end):select(self.solveRace):aggregate(1, "a, b => a * b")
end,
puzzle2 = function(self)
local rawData = client:getDayInput(self.day):trim():split("\n")
local time = tonumber((rawData[1]:match("Time:%s*([%d%s]+)"):gsub("%s", "")))
local distance = tonumber((rawData[2]:match("Distance:%s*([%d%s]+)"):gsub("%s", "")))
return self.solveRace({ time = time, distance = distance }, 1)
end,
solveRace = function(race, i)
local p, q = -race.time, race.distance + 0.1
local c1 = -p / 2 + math.sqrt((p / 2) * (p / 2) - q)
local c2 = -p / 2 - math.sqrt((p / 2) * (p / 2) - q)
return math.floor(c1) - math.ceil(c2) + 1
end,
}