Search code examples
oberoncomponent-pascal

Way of Solving Advent of Code using BlackBox environment (Component-Pascal)


I'm looking for examples on how to solve problems with Black Box Component Builder (BBCB). However, all examples including the documentation (tutorial and samples) are very constricted to the environment.

for example what i look for is how to solve the problem (i.e. https://adventofcode.com/2023/day/1) using BBCB. I know how to solve it in other languages (Common-Lisp, Forth, C, ...etc). But how to target the problem from within BBCB. is there a standard approach? is there a way of thinking? or how this language can shape my mind in a different way.

I tried documentation (inside BBCB), i read and played with Wirth's book (Algorithms and Data Structure) as well as Computing Fundamentals by Stanley. but these examples tried are constricted to their respective environments command line for Oberon2 and BBCB for CP


Solution

  • There is a module In, that is usually used in BlackBox Component Builder for some educational examples like Advent of Code.

    So I prepared the demonstration module of using it for Day1 example.

    MODULE AdventofcodeDay1;
    
    IMPORT In, Log;
    
    PROCEDURE Do*;
    CONST undefined = -1;
    VAR firstDigit, lastDigit, twoDigitNumber, res, digit: INTEGER; char: CHAR;
    BEGIN
        In.Open;
        res := 0; firstDigit := undefined; lastDigit := undefined;
        WHILE In.Done DO
            In.Char(char);
            IF (char = 0DX) OR ~In.Done THEN
                IF firstDigit # undefined THEN
                    twoDigitNumber := firstDigit * 10 + lastDigit;
                    Log.Tab; Log.Int(twoDigitNumber);
                    res := res + twoDigitNumber
                END;
                firstDigit := undefined; lastDigit := undefined;
                Log.Ln
            ELSE
                digit := ORD(char) - 30H;
                IF (firstDigit = undefined) & (digit >= 0) & (digit <= 9) THEN
                    firstDigit := digit;
                    lastDigit := firstDigit
                ELSIF (firstDigit # undefined) & (digit >= 0) & (digit <= 9) THEN
                    lastDigit := digit
                END;
                Log.Char(char)
            END
        END;
        Log.Int(res); Log.Ln;
    END Do;
    
    END AdventofcodeDay1.
    
    
    ^Q AdventofcodeDay1.Do
    
    1abc2
    pqr3stu8vwx
    a1b2c3d4e5f
    treb7uchet
    

    What you need, is to select the text bellow the command and press commander ^Q which can be inserted by Ctrl+q (if you read some documentation for BBCP, you can know how to use commanders already to start exported procedures).