DEV Community

loading...

Discussion on: Advent of Code 2020 Solution Megathread - Day 1: Report Repair

Collapse
galoisgirl profile image
Anna

COBOL:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Advent-of-Code-2020-day-1-part-2.
   AUTHOR. ANNA KOSIERADZKA.

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

   DATA DIVISION.
   FILE SECTION.
     FD INPUTFILE.
     01 INPUTRECORD PIC 9(4).
   WORKING-STORAGE SECTION.
     01 FILE-STATUS PIC 9 VALUE 0.
     01 WS-ARRAY-LEN PIC 9(3) VALUE 200.
     01 WS-ARRAY PIC 9(4) OCCURS 0 to 200 DEPENDING ON WS-ARRAY-LEN.
     01 WS-SUM PIC 9(4).
     01 WS-PRODUCT-1 PIC 9(8).
     01 WS-PRODUCT-2 PIC 9(12).

   LOCAL-STORAGE SECTION.
     01 I USAGE UNSIGNED-INT VALUE 1.
     01 J USAGE UNSIGNED-INT VALUE 1.
     01 K USAGE UNSIGNED-INT VALUE 1.

   PROCEDURE DIVISION.
   001-MAIN.
        OPEN INPUT INPUTFILE.
        PERFORM 002-READ UNTIL FILE-STATUS = 1.
        CLOSE INPUTFILE.
        PERFORM 004-LOOP.
        STOP RUN.

   002-READ.
        READ INPUTFILE
            AT END MOVE 1 TO FILE-STATUS
            NOT AT END PERFORM 003-WRITE-TO-TABLE
        END-READ.

   003-WRITE-TO-TABLE.
       COMPUTE WS-ARRAY(I) = INPUTRECORD
       ADD 1 to I.

   004-LOOP.
       PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-ARRAY-LEN
       AFTER J FROM 1 BY 1 UNTIL J > WS-ARRAY-LEN
       AFTER K FROM 1 BY 1 UNTIL K > WS-ARRAY-LEN
         ADD WS-ARRAY(I) WS-ARRAY(J) WS-ARRAY(K) GIVING WS-SUM
         IF WS-SUM = 2020 THEN
           MULTIPLY WS-ARRAY(I) BY WS-ARRAY(J) GIVING WS-PRODUCT-1 
           MULTIPLY WS-PRODUCT-1 BY WS-ARRAY(K) GIVING WS-PRODUCT-2
           DISPLAY WS-PRODUCT-2
           EXIT PERFORM
         END-IF
       END-PERFORM.
Enter fullscreen mode Exit fullscreen mode
Collapse
rpalo profile image
Ryan Palo Author

This is awesome! I'm so happy I get to add COBOL to the list  😁