2
1
Fork 0
aoc2021/02/SLTN1.CBL

49 lines
1.2 KiB
COBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. DAY2PT1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'input.txt'
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD PIC A(16).
WORKING-STORAGE SECTION.
01 WS-COMMAND.
05 WS-CMD PIC A(16).
05 WS-VAL PIC 9(1).
01 WS-H PIC 9(10) USAGE IS COMP.
01 WS-V PIC 9(10) USAGE IS COMP.
01 WS-RESULT PIC Z(10).
PROCEDURE DIVISION.
START-ROUTINE.
OPEN INPUT INPUT-FILE.
READ-ROUTINE.
MOVE SPACES TO INPUT-RECORD.
MOVE SPACES TO WS-COMMAND.
READ INPUT-FILE AT END GO TO END-ROUTINE.
UNSTRING INPUT-RECORD DELIMITED BY SPACE
INTO WS-CMD, WS-VAL.
IF WS-CMD = 'forward' THEN
ADD WS-VAL TO WS-H
END-IF
IF WS-CMD = 'down' THEN
ADD WS-VAL TO WS-V
END-IF
IF WS-CMD = 'up' THEN
SUBTRACT WS-VAL FROM WS-V
END-IF
GO TO READ-ROUTINE.
END-ROUTINE.
CLOSE INPUT-FILE.
MULTIPLY WS-H BY WS-V GIVING WS-RESULT.
DISPLAY 'Answer: 'WS-RESULT.
STOP RUN.