-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_6.lua
87 lines (77 loc) · 1.82 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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
local eio = require "libs.eio"
local profile = require "libs.profile"
local input = eio.lines()
local printf = eio.printf
local sub = string.sub
profile.start()
local stream = input[1]
local function makeMarker ()
return {
["a"] = 0,
["b"] = 0,
["c"] = 0,
["d"] = 0,
["e"] = 0,
["f"] = 0,
["g"] = 0,
["h"] = 0,
["i"] = 0,
["j"] = 0,
["k"] = 0,
["l"] = 0,
["m"] = 0,
["n"] = 0,
["o"] = 0,
["p"] = 0,
["q"] = 0,
["r"] = 0,
["s"] = 0,
["t"] = 0,
["u"] = 0,
["v"] = 0,
["w"] = 0,
["x"] = 0,
["y"] = 0,
["z"] = 0,
uniqueCount = 0
}
end
local function add (counters, letter)
local c = counters[letter] + 1
if c == 1 then
counters.uniqueCount = counters.uniqueCount + 1
elseif c == 2 then
counters.uniqueCount = counters.uniqueCount - 1
end
counters[letter] = c
end
local function del (counters, letter)
local c = counters[letter] - 1
if c == 0 then
counters.uniqueCount = counters.uniqueCount - 1
elseif c == 1 then
counters.uniqueCount = counters.uniqueCount + 1
end
if c >= 0 then
counters[letter] = c
end
end
local function firstMarkerPosition (len)
local marker = makeMarker()
for i = 1, len do
add(marker, sub(stream, i, i))
end
local i = len
while marker.uniqueCount ~= len do
i = i + 1
local begin = i - len
del(marker, sub(stream, begin, begin))
add(marker, sub(stream, i, i))
end
return i
end
local answer1 = firstMarkerPosition(4)
printf("Part 1: %i\n", answer1)
local answer2 = firstMarkerPosition(14)
printf("Part 2: %i\n", answer2)
return answer1, answer2, profile.finish()