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