DEV Community

loading...

Discussion on: Advent of Code 2020 Solution Megathread - Day 4: Passport Processing

Collapse
galoisgirl profile image
Anna

Not my finest work, I took advantage of the fact that some problems didn't arise in the test data. Then again, this isn't real life, it's a game and I got the stars. 😉

Part 2 here.

   IDENTIFICATION DIVISION.
   PROGRAM-ID. AOC-2020-04-1.
   AUTHOR. ANNA KOSIERADZKA.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
       SELECT INPUTFILE ASSIGN TO "d4.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-ROW PIC X(16) OCCURS 8 TIMES.
     01 WS-CHAR PIC X.

   LOCAL-STORAGE SECTION.
     01 CORRECT-PASSPORTS UNSIGNED-INT VALUE 0.
     01 FOUND-FIELDS UNSIGNED-INT VALUE 0.
     01 STRING-PTR UNSIGNED-INT VALUE 1.
     01 I UNSIGNED-INT VALUE 1.

   PROCEDURE DIVISION.
   001-MAIN.
       OPEN INPUT INPUTFILE.
       PERFORM 002-READ UNTIL FILE-STATUS = 1.
       CLOSE INPUTFILE.
       PERFORM 004-NEXT-PASSPORT.
       DISPLAY CORRECT-PASSPORTS.
       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-PASSPORT
       ELSE 
          PERFORM 005-PROCESS-ROW
       END-IF.

   004-NEXT-PASSPORT.
       IF FOUND-FIELDS = 7 THEN 
          ADD 1 TO CORRECT-PASSPORTS
       END-IF.
       MOVE 0 TO FOUND-FIELDS.

   005-PROCESS-ROW.
       MOVE 1 TO STRING-PTR.
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > 8
         UNSTRING INPUTRECORD DELIMITED BY SPACE INTO WS-ROW(I)
         WITH POINTER STRING-PTR
       END-PERFORM.

       PERFORM VARYING I FROM 1 BY 1 UNTIL I > 8
          MOVE WS-ROW(I)(1:1) TO WS-CHAR
          IF NOT WS-CHAR ='c' AND NOT WS-CHAR = ' ' THEN
             ADD 1 TO FOUND-FIELDS
          END-IF
       END-PERFORM.
Enter fullscreen mode Exit fullscreen mode