use deque in fish solution
This commit is contained in:
parent
d99434ac43
commit
563e354df5
@ -1,26 +1,26 @@
|
|||||||
#!/usr/bin/env luajit
|
#!/usr/bin/env luajit
|
||||||
require("utils")
|
require("utils")
|
||||||
|
require("deque")
|
||||||
|
|
||||||
local fishes = {}
|
local fishes = deque()
|
||||||
for timer = 0, 9 do
|
for timer = 0, 9 do
|
||||||
fishes[timer] = 0
|
fishes:append(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, timer in pairs(split(io.open(arg[3]):read("*a"), ",")) do
|
for _, timer in pairs(split(io.open(arg[3]):read("*a"), ",")) do
|
||||||
fishes[tonumber(timer)] = fishes[tonumber(timer)] + 1
|
fishes:set(tonumber(timer), fishes:get(tonumber(timer)) + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
for day = 1, 80 do
|
for day = 1, 80 do
|
||||||
fishes[9] = (fishes[0] or 0) + (fishes[9] or 0)
|
local breeding = fishes:popleft() or 0
|
||||||
fishes[7] = (fishes[0] or 0) + (fishes[7] or 0)
|
fishes:append(0)
|
||||||
for timer = 0, 9 do
|
fishes:set(8, (fishes:get(8) or 0) + breeding)
|
||||||
fishes[timer] = fishes[timer + 1]
|
fishes:set(6, (fishes:get(6) or 0) + breeding)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local sum = 0
|
local sum = 0
|
||||||
for timer, fishday in pairs(fishes) do
|
while fishes.size > 0 do
|
||||||
sum = sum + fishday
|
sum = sum + (fishes:pop() or 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
print(sum)
|
print(sum)
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
#!/usr/bin/env luajit
|
#!/usr/bin/env luajit
|
||||||
require("utils")
|
require("utils")
|
||||||
|
require("deque")
|
||||||
|
|
||||||
local fishes = {}
|
local fishes = deque()
|
||||||
for timer = 0, 9 do
|
for timer = 0, 9 do
|
||||||
fishes[timer] = 0
|
fishes:append(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, timer in pairs(split(io.open(arg[3]):read("*a"), ",")) do
|
for _, timer in pairs(split(io.open(arg[3]):read("*a"), ",")) do
|
||||||
fishes[tonumber(timer)] = fishes[tonumber(timer)] + 1
|
fishes:set(tonumber(timer), fishes:get(tonumber(timer)) + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
for day = 1, 256 do
|
for day = 1, 256 do
|
||||||
fishes[9] = (fishes[0] or 0) + (fishes[9] or 0)
|
local breeding = fishes:popleft() or 0
|
||||||
fishes[7] = (fishes[0] or 0) + (fishes[7] or 0)
|
fishes:append(0)
|
||||||
for timer = 0, 9 do
|
fishes:set(8, (fishes:get(8) or 0) + breeding)
|
||||||
fishes[timer] = fishes[timer + 1]
|
fishes:set(6, (fishes:get(6) or 0) + breeding)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local sum = 0
|
local sum = 0
|
||||||
for timer, fishday in pairs(fishes) do
|
while fishes.size > 0 do
|
||||||
sum = sum + fishday
|
sum = sum + (fishes:pop() or 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
print(sum)
|
print(sum)
|
||||||
|
16
deque.lua
16
deque.lua
@ -14,6 +14,8 @@ function deque()
|
|||||||
prepend = prepend,
|
prepend = prepend,
|
||||||
append = append,
|
append = append,
|
||||||
popleft = popleft,
|
popleft = popleft,
|
||||||
|
get = get,
|
||||||
|
set = set,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -79,3 +81,17 @@ function popleft(self)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function get(self, i)
|
||||||
|
if 0 <= i and i < self.size then
|
||||||
|
return self.elems[(self.start + i) % self.cap]
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function set(self, i, n)
|
||||||
|
if 0 <= i and i < self.size then
|
||||||
|
self.elems[(self.start + i) % self.cap] = n
|
||||||
|
end
|
||||||
|
end
|
||||||
|
1
prepare
1
prepare
@ -7,6 +7,7 @@ day="$(printf '%02d' "$day")"
|
|||||||
if test -f "day${day}/part${part}.lua"; then
|
if test -f "day${day}/part${part}.lua"; then
|
||||||
/usr/bin/env luajit -v >/dev/null
|
/usr/bin/env luajit -v >/dev/null
|
||||||
cp "day${day}/part${part}.lua" run
|
cp "day${day}/part${part}.lua" run
|
||||||
|
chmod u+x run
|
||||||
elif test -f "day${day}/part${part}.hs"; then
|
elif test -f "day${day}/part${part}.hs"; then
|
||||||
#stack build
|
#stack build
|
||||||
stack ghc -- -O3 "day${day}/part${part}.hs" -o run
|
stack ghc -- -O3 "day${day}/part${part}.hs" -o run
|
||||||
|
Loading…
Reference in New Issue
Block a user