aoc24-d4 in cobol
@oppi.li · 26d ago · plaintext · 186 loc · raw · 1 comment
1 IDENTIFICATION DIVISION.2 PROGRAM-ID. D4.34 ENVIRONMENT DIVISION.5 INPUT-OUTPUT SECTION.6 FILE-CONTROL.7 SELECT INPUT-FILE ASSIGN TO "IN/4.TXT"8 ORGANIZATION IS LINE SEQUENTIAL.910 DATA DIVISION.11 FILE SECTION.12 FD INPUT-FILE.13 01 STR PIC X(140).1415 WORKING-STORAGE SECTION.16 78 ROW-SIZE VALUE 140.17 01 TBL.18 05 ROW OCCURS ROW-SIZE TIMES.19 10 ELT PIC X(ROW-SIZE).2021 01 IDX PIC 9(3).22 01 JDX PIC 9(3).23 01 TMP-STR PIC X(3).2425 01 FILE-POS PIC X.26 88 EOF VALUE 'Y'.27 88 NOT-EOF VALUE 'N'.2829 01 BKSLSH PIC X.30 01 FWSLSH PIC X.3132 01 ANS-1 PIC 9(9).33 01 ANS-2 PIC 9(9).3435 PROCEDURE DIVISION.36 OPEN INPUT INPUT-FILE.37 PERFORM PARSE-FILE.38 PERFORM SOLVE OF P1.39 PERFORM SOLVE OF P2.40 DISPLAY 'ANS-1: ' ANS-1.41 DISPLAY 'ANS-2: ' ANS-2.42 CLOSE INPUT-FILE.43 STOP RUN.4445 PARSE-FILE.46 PERFORM VARYING IDX FROM 1 BY 1 UNTIL EOF47 READ INPUT-FILE48 AT END SET EOF TO TRUE49 NOT AT END 50 MOVE STR TO ROW(IDX)51 END-READ52 END-PERFORM.5354 P1 SECTION.55 SOLVE.56 PERFORM 57 VARYING IDX FROM 1 BY 1 UNTIL IDX > ROW-SIZE58 AFTER JDX FROM 1 BY 1 UNTIL JDX > ROW-SIZE59 IF ELT(IDX)(JDX:1) = 'X'60 PERFORM START-COUNT THRU END-COUNT61 END-IF62 END-PERFORM.6364 START-COUNT.6566 COUNT-FORWARD.67 IF JDX + 3 <= ROW-SIZE68 IF ELT(IDX)(JDX:4) = 'XMAS'69 ADD 1 TO ANS-170 END-IF71 END-IF.727374 COUNT-BACKWARD.75 IF JDX >= 376 IF ELT(IDX)(JDX - 3:4) = 'SAMX'77 ADD 1 TO ANS-178 END-IF79 END-IF.8081 COUNT-DOWNWARD.82 IF IDX + 3 <= ROW-SIZE83 IF ELT(IDX)(JDX:1) = 'X' AND84 ELT(IDX + 1)(JDX:1) = 'M' AND85 ELT(IDX + 2)(JDX:1) = 'A' AND86 ELT(IDX + 3)(JDX:1) = 'S'87 ADD 1 TO ANS-188 END-IF89 END-IF.9091 COUNT-UPWARD.92 IF IDX > 393 IF ELT(IDX - 3)(JDX:1) = 'S' AND94 ELT(IDX - 2)(JDX:1) = 'A' AND95 ELT(IDX - 1)(JDX:1) = 'M' AND96 ELT(IDX)(JDX:1) = 'X'97 ADD 1 TO ANS-198 END-IF99 END-IF.100101 COUNT-FORwARD-DOWN.102 IF IDX + 3 <= ROW-SIZE AND JDX + 3 <= ROW-SIZE103 IF ELT(IDX)(JDX:1) = 'X' AND104 ELT(IDX + 1)(JDX + 1:1) = 'M' AND105 ELT(IDX + 2)(JDX + 2:1) = 'A' AND106 ELT(IDX + 3)(JDX + 3:1) = 'S'107 ADD 1 TO ANS-1108 END-IF109 END-IF.110111 COUNT-FORwARD-UP.112 IF IDX > 3 AND JDX + 3 <= ROW-SIZE113 IF ELT(IDX)(JDX:1) = 'X' AND114 ELT(IDX - 1)(JDX + 1:1) = 'M' AND115 ELT(IDX - 2)(JDX + 2:1) = 'A' AND116 ELT(IDX - 3)(JDX + 3:1) = 'S'117 ADD 1 TO ANS-1118 END-IF119 END-IF.120121 COUNT-BACKWARD-UP.122 IF IDX > 3 AND JDX > 3123 IF ELT(IDX)(JDX:1) = 'X' AND124 ELT(IDX - 1)(JDX - 1:1) = 'M' AND125 ELT(IDX - 2)(JDX - 2:1) = 'A' AND126 ELT(IDX - 3)(JDX - 3:1) = 'S'127 ADD 1 TO ANS-1128 END-IF129 END-IF.130131 COUNT-BACKWARD-DOWN.132 IF IDX + 3 <= ROW-SIZE AND JDX > 3133 IF ELT(IDX)(JDX:1) = 'X' AND134 ELT(IDX + 1)(JDX - 1:1) = 'M' AND135 ELT(IDX + 2)(JDX - 2:1) = 'A' AND136 ELT(IDX + 3)(JDX - 3:1) = 'S'137 ADD 1 TO ANS-1138 END-IF139 END-IF.140141 END-COUNT.142143 P2 SECTION.144 SOLVE.145 PERFORM 146 VARYING IDX FROM 1 BY 1 UNTIL IDX > ROW-SIZE147 AFTER JDX FROM 1 BY 1 UNTIL JDX > ROW-SIZE148 IF ELT(IDX)(JDX:1) = 'A'149 MOVE 'N' TO BKSLSH FWSLSH150 PERFORM START-COUNT THRU END-COUNT151 END-IF152 END-PERFORM.153154 START-COUNT.155156 COUNT-BACKSLASH.157 IF IDX > 1 AND IDX < ROW-SIZE AND158 JDX > 1 AND JDX < ROW-SIZE159 STRING ELT(IDX - 1)(JDX - 1:1)160 ELT(IDX )(JDX :1)161 ELT(IDX + 1)(JDX + 1:1)162 INTO TMP-STR163 IF TMP-STR = 'MAS' OR TMP-STR = 'SAM'164 MOVE 'Y' TO BKSLSH165 END-IF166 END-IF.167168 COUNT-FORWARDSLASH.169 IF IDX > 1 AND IDX < ROW-SIZE AND170 JDX > 1 AND JDX < ROW-SIZE171 STRING ELT(IDX - 1)(JDX + 1:1)172 ELT(IDX )(JDX :1)173 ELT(IDX + 1)(JDX - 1:1)174 INTO TMP-STR175 IF TMP-STR = 'MAS' OR TMP-STR = 'SAM'176 MOVE 'Y' TO FWSLSH177 END-IF178 END-IF.179180 BOTH.181 IF BKSLSH = 'Y' And FWSLSH = 'Y'182 ADD 1 to ANS-2183 END-IF.184185 END-COUNT.186
comments
login to post a comment
@oppi.li · 26d ago