Ala'a Mohammad
Ala'a Mohammad

Reputation: 143

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

Upvotes: 0

Views: 146

Answers (2)

August Karlstrom
August Karlstrom

Reputation: 11377

Here is a solution using the "standard" Oakwood modules In and Out available in most freestanding Oberon compilers, like OBNC:

MODULE aoc2023d1;

    IMPORT In, Out;

    VAR
        sum: INTEGER;
        ch, first, last: CHAR;

BEGIN
    first := 0X;
    sum := 0;
    In.Char(ch);
    WHILE In.Done DO
        IF (ch >= "0") & (ch <= "9") THEN
            IF first = 0X THEN
                first := ch
            END;
            last := ch
        ELSIF (ch = 0AX) & (first # 0X) THEN
            INC(sum, (ORD(first) - ORD("0")) * 10 + ORD(last) - ORD("0"));
            first := 0X
        END;
        In.Char(ch)
    END;
    Out.Int(sum, 0);
    Out.Ln
END aoc2023d1.

Upvotes: 0

Ivan Denisov
Ivan Denisov

Reputation: 16

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).

Upvotes: 0

Related Questions