86 lines
2.3 KiB
COBOL
86 lines
2.3 KiB
COBOL
identification division.
|
|
program-id. day06.
|
|
|
|
data division.
|
|
working-storage section.
|
|
01 input-line pic x(40).
|
|
|
|
01 action pic x(8).
|
|
01 coords pic x(30).
|
|
01 start-coords pic x(7).
|
|
01 end-coords pic x(7).
|
|
|
|
01 start-x pic 9(3) comp-x.
|
|
01 end-x pic 9(3) comp-x.
|
|
01 start-y pic 9(3) comp-x.
|
|
01 end-y pic 9(3) comp-x.
|
|
|
|
01 x pic 9(4) comp-x.
|
|
01 y pic 9(4) comp-x.
|
|
|
|
01 matrix-table.
|
|
05 occurs 1000 times.
|
|
10 occurs 1000 times.
|
|
15 matrix pic 9(2) comp-x value 0.
|
|
|
|
01 brightness pic 9(9) comp-x.
|
|
|
|
procedure division.
|
|
accept input-line.
|
|
perform input-loop-para until input-line = SPACE.
|
|
|
|
move 1 to y.
|
|
perform count-outer-para until y > 1000.
|
|
display brightness.
|
|
|
|
stop run.
|
|
|
|
input-loop-para.
|
|
if input-line(1:7) = "turn on" then
|
|
move "turn on" to action
|
|
move input-line(9:) to coords
|
|
else if input-line(1:8) = "turn off" then
|
|
move "turn off" to action
|
|
move input-line(10:) to coords
|
|
else if input-line(1:7) = "toggle" then
|
|
move "toggle" to action
|
|
move input-line(8:) to coords
|
|
end-if.
|
|
|
|
unstring coords delimited by " through "
|
|
into start-coords, end-coords.
|
|
|
|
unstring start-coords delimited by "," into start-x, start-y
|
|
unstring end-coords delimited by "," into end-x, end-y
|
|
|
|
move start-y to y.
|
|
perform outer-loop-para until y > end-y.
|
|
|
|
accept input-line.
|
|
|
|
outer-loop-para.
|
|
add 1 to y.
|
|
|
|
move start-x to x.
|
|
perform inner-loop-para until x > end-x.
|
|
|
|
inner-loop-para.
|
|
add 1 to x.
|
|
|
|
if action = "turn on" then
|
|
add 1 to matrix(y, x)
|
|
else if action = "turn off" and matrix(y, x) > 0 then
|
|
subtract 1 from matrix(y, x)
|
|
else if action = "toggle" then
|
|
add 2 to matrix(y, x)
|
|
end-if.
|
|
|
|
count-outer-para.
|
|
move 1 to x.
|
|
perform count-inner-para until x > 1000.
|
|
add 1 to y.
|
|
|
|
count-inner-para.
|
|
add matrix(y, x) to brightness.
|
|
add 1 to x.
|