added viz for day2

This commit is contained in:
Simon Cambier 2023-12-02 10:56:50 +01:00
parent b595d9ce2f
commit dee419d2fd

47
day2.p8
View File

@ -105,6 +105,9 @@ local input = split(
Game 100:7 blue,6 red,5 green;3 blue,13 green,11 red;6 red,13 green,14 blue;8 red,10 blue,15 green]], "\n" Game 100:7 blue,6 red,5 green;3 blue,13 green,11 red;6 red,13 green,14 blue;8 red,10 blue,15 green]], "\n"
) )
-- viz
local flakes = {}
--strdsum --strdsum
--v0.1 --v0.1
--@shiftalow / bitchunk --@shiftalow / bitchunk
@ -124,6 +127,7 @@ local maxblue = 14
local sum = 0 local sum = 0
local sumpower = "0" local sumpower = "0"
local iter = cocreate(function()
for i, game in ipairs(input) do for i, game in ipairs(input) do
local ok = true local ok = true
local minred = 0 local minred = 0
@ -131,12 +135,13 @@ for i, game in ipairs(input) do
local minblue = 0 local minblue = 0
local data = split(game, ":")[2] local data = split(game, ":")[2]
printh(" ") -- printh(" ")
printh("GAME " .. i) -- printh("GAME " .. i)
printh(data) -- printh(data)
local hands = split(data, ";") local hands = split(data, ";")
for hand in all(hands) do for hand in all(hands) do
yield()
local grouped = split(hand, ",") local grouped = split(hand, ",")
local red = 0 local red = 0
local green = 0 local green = 0
@ -152,18 +157,50 @@ for i, game in ipairs(input) do
if green > mingreen then mingreen = green end if green > mingreen then mingreen = green end
if blue > minblue then minblue = blue end if blue > minblue then minblue = blue end
end end
printh("red: " .. red .. ", green: " .. green .. ", blue: " .. blue) -- printh("red: " .. red .. ", green: " .. green .. ", blue: " .. blue)
if red > maxred or green > maxgreen or blue > maxblue then if red > maxred or green > maxgreen or blue > maxblue then
ok = false ok = false
end end
end end
-- viz
for i = 1, minred do
add(flakes, { rnd(128) \ 1, -rnd(20) \ 1, 8 })
end
for i = 1, mingreen do
add(flakes, { rnd(128) \ 1, -rnd(20) \ 1, 11 })
end
for i = 1, minblue do
add(flakes, { rnd(128) \ 1, -rnd(20) \ 1, 12 })
end
sumpower = strdsum(sumpower, tostr(minred * mingreen * minblue)) sumpower = strdsum(sumpower, tostr(minred * mingreen * minblue))
if ok then if ok then
printh("OK") sum += i sum += i
end end
end end
printh("sum: " .. sum) printh("sum: " .. sum)
printh("powers: " .. sumpower) printh("powers: " .. sumpower)
end)
function _update()
coresume(iter)
local todel = {}
for i, flake in ipairs(flakes) do
local x = flake[1] + sin(t() / 5 + i / 20) / 2
local y = flake[2] + 1
if pget(x, y) == 0 and y < 128 then
flake[1] = x
flake[2] = y
end
end
end
function _draw()
cls()
for flake in all(flakes) do
pset(flake[1], flake[2], flake[3])
end
end
__gfx__ __gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000