added viz for day2

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

101
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"
)
-- viz
local flakes = {}
--strdsum
--v0.1
--@shiftalow / bitchunk
@ -124,46 +127,80 @@ local maxblue = 14
local sum = 0
local sumpower = "0"
for i, game in ipairs(input) do
local ok = true
local minred = 0
local mingreen = 0
local minblue = 0
local iter = cocreate(function()
for i, game in ipairs(input) do
local ok = true
local minred = 0
local mingreen = 0
local minblue = 0
local data = split(game, ":")[2]
printh(" ")
printh("GAME " .. i)
printh(data)
local data = split(game, ":")[2]
-- printh(" ")
-- printh("GAME " .. i)
-- printh(data)
local hands = split(data, ";")
for hand in all(hands) do
local grouped = split(hand, ",")
local red = 0
local green = 0
local blue = 0
for item in all(grouped) do
local q = split(item, " ")
if q[2] == "red" then red = q[1] end
if q[2] == "green" then green = q[1] end
if q[2] == "blue" then blue = q[1] end
local hands = split(data, ";")
for hand in all(hands) do
yield()
local grouped = split(hand, ",")
local red = 0
local green = 0
local blue = 0
for item in all(grouped) do
local q = split(item, " ")
if q[2] == "red" then red = q[1] end
if q[2] == "green" then green = q[1] end
if q[2] == "blue" then blue = q[1] end
-- part 2
if red > minred then minred = red end
if green > mingreen then mingreen = green end
if blue > minblue then minblue = blue end
-- part 2
if red > minred then minred = red end
if green > mingreen then mingreen = green end
if blue > minblue then minblue = blue end
end
-- printh("red: " .. red .. ", green: " .. green .. ", blue: " .. blue)
if red > maxred or green > maxgreen or blue > maxblue then
ok = false
end
end
printh("red: " .. red .. ", green: " .. green .. ", blue: " .. blue)
if red > maxred or green > maxgreen or blue > maxblue then
ok = false
-- 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))
if ok then
sum += i
end
end
sumpower = strdsum(sumpower, tostr(minred * mingreen * minblue))
if ok then
printh("OK") sum += i
printh("sum: " .. sum)
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
printh("sum: " .. sum)
printh("powers: " .. sumpower)
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000