Skip to content

Commit

Permalink
Parse cube stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Dec 2, 2023
1 parent 601c110 commit b21fa69
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions day02/src/day02.cob
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@
WORKING-STORAGE SECTION.
01 FileName PIC X(100).
01 ReachedEndOfFile PIC A(1) VALUE 'N'.
01 GameIndex PIC 9(2) VALUE 1.
01 GameIndex PIC 9(3) VALUE 1.
01 ParsedLine.
05 GameName PIC X(12).
05 RawCubeSets PIC X(256).
01 ParsedCubeSets.
05 RawCubeSet PIC X(32) OCCURS 6 TIMES.
01 CubeSetIndex PIC 9(2) VALUE 0.
01 CubeSetIndex PIC 9(1) VALUE 0.
01 ParsedCubeSet.
05 CubeStack OCCURS 3 TIMES.
10 CubeCount PIC 9(2).
10 CubeColor PIC X(16).
10 CubeColor PIC X(1).
01 CubeStackIndex PIC 9(1) VALUE 0.
01 CubeSet.
05 Red PIC 9(2) VALUE 0.
05 Green PIC 9(2) VALUE 0.
05 Blue PIC 9(2) VALUE 0.
01 Total.
05 TotalRed PIC 9(2) VALUE 12.
05 TotalGreen PIC 9(2) VALUE 13.
05 TotalBlue PIC 9(2) VALUE 14.
01 Result.
05 Part1 PIC 9(4) VALUE 0.

PROCEDURE DIVISION.
ACCEPT FileName FROM COMMAND-LINE.
Expand Down Expand Up @@ -96,6 +103,22 @@
INTO CubeCount(1), CubeColor(1),
CubeCount(2), CubeColor(2),
CubeCount(3), CubeColor(3).
DISPLAY "First: " CubeCount(1) " " CubeColor(1) ", "
"Second: " CubeCount(2) " " CubeColor(2) ", "
"Third: " CubeCount(3) " " CubeColor(3).

MOVE 0 TO Red;
MOVE 0 TO Green;
MOVE 0 TO Blue;

PERFORM ProcessCubeStack
VARYING CubeStackIndex
FROM 1 BY 1
UNTIL CubeStackIndex > 3.

DISPLAY "Red: " Red " Green: " Green " Blue: " Blue.

ProcessCubeStack.
EVALUATE CubeColor(CubeStackIndex)
WHEN 'r' COMPUTE Red = CubeCount(CubeStackIndex)
WHEN 'g' COMPUTE Green = CubeCount(CubeStackIndex)
WHEN 'b' COMPUTE Blue = CubeCount(CubeStackIndex)
WHEN OTHER CONTINUE
END-EVALUATE.

0 comments on commit b21fa69

Please sign in to comment.