2
1
Fork 0
aoc2021/02/SLTN2.CBL

52 lines
1.3 KiB
Text
Raw Normal View History

2021-12-02 14:31:44 +01:00
IDENTIFICATION DIVISION.
PROGRAM-ID. DAY2PT2.
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-AIM PIC 9(10) USAGE IS COMP.
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
COMPUTE WS-V=WS-V+WS-AIM*WS-VAL
END-IF
IF WS-CMD = 'down' THEN
ADD WS-VAL TO WS-AIM
END-IF
IF WS-CMD = 'up' THEN
SUBTRACT WS-VAL FROM WS-AIM
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.