advent2022

Advent of Code 2022 Solutions
git clone https://todayiwilllaunchmyinfantsonintoorbit.com/advent2022.git
Log | Files | Refs

commit 8490469fa2cd35ad0f82ae158e785fa5a0cdc4ad
parent 6e1c74890e2ea7d8033c1e9494f1186ea8101022
Author: Decay <decay@todayiwilllaunchmyinfantsonintoorbit.com>
Date:   Thu,  8 Dec 2022 08:57:35 -0800

Day 8 (QuickBASIC)

Diffstat:
A8/8.bas | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 137 insertions(+), 0 deletions(-)

diff --git a/8/8.bas b/8/8.bas @@ -0,0 +1,137 @@ +REM QBasic/QuickBasic implementation of AOC2022 day 8 + +REM visibility reference constants; tree height is stored in forest%(x,y,HEIGHT) +const TOPV = 0 +const LEFTV = 1 +const BOTTOMV = 2 +const RIGHTV = 3 +const HEIGHT = 4 + +REM Open and read file +open "input" for input as #1 +line input #1, line$ +let side% = len(line$) + +REM Create array to store the forest and visibilities, assume square +dim forest%(side%, side%, 5) +let visible% = 0 + +REM Construct a matrix of the forest from file input and the top and left visibilities +for y% = 1 to side% + if y% > 1 then + line input #1, line$ + end if + for x% = 1 to side% + let tree$ = mid$(line$, x%, 1) + let forest%(x% - 1, y% - 1, HEIGHT) = asc(tree$) - 48 + + REM Determine visibility from the left + if x% = 1 then + forest%(0, y% - 1, LEFTV) = -1 + elseif forest%(x% - 2, y% - 1, HEIGHT) > forest%(x% - 2, y% - 1, LEFTV) then + forest%(x% - 1, y% - 1, LEFTV) = forest%(x% - 2, y% - 1, HEIGHT) + else + forest%(x% - 1, y% - 1, LEFTV) = forest%(x% - 2, y% - 1, LEFTV) + end if + + REM Determine visibility from the top + if y% = 1 then + forest%(x% - 1, 0, TOPV) = -1 + elseif forest%(x% - 1, y% - 2, HEIGHT) > forest%(x% - 1, y% - 2, TOPV) then + forest%(x% - 1, y% - 1, TOPV) = forest%(x% - 1, y% - 2, HEIGHT) + else + forest%(x% - 1, y% - 1, TOPV) = forest%(x% - 1, y% - 2, TOPV) + end if + next x% +next y% + +REM Close file +close #1 + +REM Go backward through the array and collect visibility in the opposite direction +REM and count visible trees +for y% = side% to 1 step - 1 + for x% = side% to 1 step -1 + REM Determine visibility from the right + if x% = side% then + forest%(side% - 1, y% - 1, RIGHTV) = -1 + elseif forest%(x%, y% - 1, HEIGHT) > forest%(x%, y% - 1, RIGHTV) then + forest%(x% - 1, y% - 1, RIGHTV) = forest%(x%, y% - 1, HEIGHT) + else + forest%(x% - 1, y% - 1, RIGHTV) = forest%(x%, y% - 1, RIGHTV) + end if + + REM Determine visibility from the bottom + if y% = side% then + forest%(x% - 1, side% - 1, BOTTOMV) = -1 + elseif forest%(x% - 1, y%, HEIGHT) > forest%(x% - 1, y%, BOTTOMV) then + forest%(x% - 1, y% - 1, BOTTOMV) = forest%(x% - 1, y%, HEIGHT) + else + forest%(x% - 1, y% - 1, BOTTOMV) = forest%(x% - 1, y%, BOTTOMV) + end if + + let treeheight% = forest%(x% - 1, y% - 1, HEIGHT) + if (forest%(x% - 1, y% - 1, LEFTV) < treeheight%) OR (forest%(x% - 1, y% - 1, TOPV) < treeheight%) OR (forest%(x% - 1, y% - 1, RIGHTV) < treeheight%) OR (forest%(x% - 1, y% - 1, BOTTOMV) < treeheight%) then + visible% = visible% + 1 + end if + next x% +next y% + +REM Loop over again and find the highest viewing distance +let maxsc = 0 +for y% = side% to 1 step - 1 + for x% = side% to 1 step -1 + let treeheight% = forest%(x% - 1, y% - 1, HEIGHT) + let lv = 0 + let tv = 0 + let rv = 0 + let bv = 0 + + REM Find left visibility + if x% = 1 then + lv = 0 + else + lv = 0 + do + lv = lv + 1 + loop until (forest%(x% - (lv + 1), y% - 1, HEIGHT) >= treeheight%) or (x% - lv = 1) + end if + + REM Find top visibility + if y% = 1 then + tv = 0 + else + tv = 0 + do + tv = tv + 1 + loop until (forest%(x% - 1, y% - (tv + 1), HEIGHT) >= treeheight%) or (y% - tv = 1) + end if + + REM Find right visibility + if x% = side% then + rv = 0 + else + rv = 0 + do + rv = rv + 1 + loop until (forest%(x% + (rv - 1), y% - 1, HEIGHT) >= treeheight%) or (x% + rv = side%) + end if + + REM Find bottom visibility + if y% = side% then + bv = 0 + else + bv = 0 + do + bv = bv + 1 + loop until (forest%(x% - 1, y% + (bv - 1), HEIGHT) >= treeheight%) or (y% + bv = side%) + end if + + let scenic = lv * tv * rv * bv + if scenic > maxsc then maxsc = scenic + next x% +next y% + +print visible%; " visible trees" +print maxsc; " max scenic value" +end