DEV Community 👩‍💻👨‍💻

Discussion on: Advent of Code 2020 Solution Megathread - Day 6: Custom Customs

Collapse
 
galoisgirl profile image
Anna

COBOL

   IDENTIFICATION DIVISION.
   PROGRAM-ID. AOC-2020-06-2.
   AUTHOR. ANNA KOSIERADZKA.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
       SELECT INPUTFILE ASSIGN TO "d6.input"
       ORGANIZATION IS LINE SEQUENTIAL.

   DATA DIVISION.
   FILE SECTION.
     FD INPUTFILE
     RECORD IS VARYING IN SIZE FROM 1 to 99
     DEPENDING ON REC-LEN.
     01 INPUTRECORD PIC X(99).
   WORKING-STORAGE SECTION.
     01 FILE-STATUS PIC 9 VALUE 0.
     01 REC-LEN PIC 9(2) COMP.
     01 WS-GROUP-ANSWERS PIC 9 OCCURS 26 TIMES.
     01 WS-CHAR PIC X.

   LOCAL-STORAGE SECTION.
     01 I UNSIGNED-INT VALUE 1.
     01 C UNSIGNED-INT VALUE 1.
     01 X UNSIGNED-INT VALUE 1.
     01 GROUP-SIZE UNSIGNED-INT VALUE 0.
     01 GROUP-TOTAL UNSIGNED-INT VALUE 0.
     01 TOTAL UNSIGNED-INT VALUE 0.

   PROCEDURE DIVISION.
   001-MAIN.
       PERFORM 004-INIT-VARIABLES.
       OPEN INPUT INPUTFILE.
       PERFORM 002-READ UNTIL FILE-STATUS = 1.
       CLOSE INPUTFILE.
       PERFORM 004-NEXT-GROUP.
       DISPLAY TOTAL.
       STOP RUN.

   002-READ.
        READ INPUTFILE
            AT END MOVE 1 TO FILE-STATUS
            NOT AT END PERFORM 003-PROCESS-RECORD
        END-READ.

   003-PROCESS-RECORD.
       IF REC-LEN = 0 THEN
          PERFORM 004-NEXT-GROUP
       ELSE 
          PERFORM 005-PROCESS-ROW
       END-IF.

   004-INIT-VARIABLES.
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > 26
          MOVE 0 TO WS-GROUP-ANSWERS(I)
       END-PERFORM.
       MOVE 0 TO GROUP-SIZE.
       MOVE 0 TO GROUP-TOTAL.

   004-NEXT-GROUP.
       IF GROUP-SIZE > 0 THEN
          PERFORM 006-TALLY-GROUP-TOTAL
       END-IF.
       ADD GROUP-TOTAL TO TOTAL.
       PERFORM 004-INIT-VARIABLES.

   005-PROCESS-ROW.
       ADD 1 TO GROUP-SIZE.
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > REC-LEN
          MOVE INPUTRECORD(I:1) TO WS-CHAR
          COMPUTE C = FUNCTION ORD(WS-CHAR)
          COMPUTE X = WS-GROUP-ANSWERS(C - 97) + 1
          MOVE X TO WS-GROUP-ANSWERS(C - 97)
       END-PERFORM.

   006-TALLY-GROUP-TOTAL.
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > 26
          IF WS-GROUP-ANSWERS(I) = GROUP-SIZE THEN 
             ADD 1 TO GROUP-TOTAL
          END-IF 
       END-PERFORM.
Enter fullscreen mode Exit fullscreen mode