Skip to content

Instantly share code, notes, and snippets.

@NoUsername
Created December 8, 2017 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NoUsername/eb56f8d81a2230e8fb51f708b701075c to your computer and use it in GitHub Desktop.
Save NoUsername/eb56f8d81a2230e8fb51f708b701075c to your computer and use it in GitHub Desktop.
Cobol solution for advent of code 2017 day 1
IDENTIFICATION DIVISION.
PROGRAM-ID. advofcode01.
AUTHOR Paul Klingelhuber
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT InpFile ASSIGN TO "ADVOFCODE01.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD InpFile.
01 FileFormat.
88 EndOfInpFile VALUE HIGH-VALUES.
02 SomeNumber PIC 9(1).
WORKING-STORAGE SECTION.
01 PrevValue PIC 9 VALUE ZEROS.
01 FirstValue PIC 9 VALUE ZEROS.
01 Result PIC 9(6) VALUE ZEROS.
PROCEDURE DIVISION.
Begin.
OPEN INPUT InpFile
READ InpFile
AT END SET EndOfInpFile TO TRUE
END-READ
SET FirstValue TO SomeNumber
SET PrevValue TO SomeNumber
SET Result TO 0
READ InpFile
AT END SET EndOfInpFile TO TRUE
END-READ
PERFORM UNTIL EndOfInpFile
*> DISPLAY "Checking " SPACE PrevValue SPACE SomeNumber
IF PrevValue IS = SomeNumber THEN
COMPUTE Result = Result + SomeNumber
END-IF
*> DISPLAY "CurrSum " SPACE Result
SET PrevValue TO SomeNumber
READ InpFile
AT END SET EndOfInpFile TO TRUE
END-READ
END-PERFORM
CLOSE InpFile
*> DISPLAY "Checking " SPACE PrevValue SPACE FirstValue SPACE "(first/last)"
IF PrevValue IS = FirstValue THEN
COMPUTE Result = Result + PrevValue
END-IF
DISPLAY "Final Result: " SPACE Result
STOP RUN.
*> compile with: cobc -x -free advofcode01.cob
*> put input data in a file "ADVOFCODE01.DAT", 1 number per line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment