-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.lua
60 lines (44 loc) · 1.51 KB
/
day06.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
56
57
58
59
60
require 'util'
DAY = 6
local filename = string.format("inputs/input_%02d.txt", DAY)
local f = assert(io.open(filename, "r"))
-- Function definitions here
local data = {} -- {times, distances}
local data2 = {} -- {time, distance}
local function getWays(n, d)
local low = math.floor((-n + math.sqrt(n ^ 2 - 4 * d)) / -2 + 1)
local high = math.ceil((-n - math.sqrt(n ^ 2 - 4 * d)) / -2 - 1)
low = math.max(low, 0)
high = math.min(high, (n - 1))
return high - low + 1
end
for line in f:lines() do
-- Process the file here
local start = line:match(":()")
local row = {}
local row2 = ""
for s in split(line, " ", start) do
row[#row + 1] = tonumber(s)
row2 = row2 .. s
end
data[#data + 1] = row
data2[#data2 + 1] = row2
end
-- Do everything else here
local part1 = 1
local part2 = 0
local times = data[1]
local distances = data[2]
for i = 1, #times do
part1 = part1 * getWays(times[i], distances[i])
end
part2 = getWays(tonumber(data2[1]), tonumber(data2[2]))
print("Part 1:", part1)
print("Part 2:", part2)
-- Explanation / Working Out
-- Given a race duration n, your distance is equal to
-- x*(n-x), given a duration of holding the button, x
-- We want to determine for how many values of m is x*(n-x) greater than some distance, d
-- -x^2 + xn - d > 0
-- For a value of n, x = [-n +/- sqrt(n^2 - 4 * 1 * d)]/-2 (By the quadratic formula)
-- In the code we do some stuffs with floor/ceil to ensure that the value is an integer and that the distance doesn't equal d