2015/day06a.cob

84 lines
2.3 KiB
COBOL
Raw Normal View History

2022-12-04 02:42:29 +00:00
identification division.
program-id. day06.
data division.
working-storage section.
01 input-line pic x(100).
01 action pic x(10).
01 coords pic x(100).
01 start-coords pic x(100).
01 end-coords pic x(100).
01 start-x pic 9(10).
01 end-x pic 9(10).
01 start-y pic 9(10).
01 end-y pic 9(10).
01 x pic 9(10).
01 y pic 9(10).
01 temp pic 9.
01 matrix-table.
05 occurs 1000 times.
10 occurs 1000 times.
15 matrix pic 9 value 0.
procedure division.
accept input-line.
perform input-loop-para until input-line = SPACE.
* Count the lit lights
move 0 to x.
inspect matrix-table tallying x for all '1'.
display x.
stop run.
input-loop-para.
display input-line.
if input-line(1:7) = "turn on" then
move "turn on" to action
move input-line(9:) to coords
end-if.
if input-line(1:8) = "turn off" then
move "turn off" to action
move input-line(10:) to coords
end-if.
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.
move start-x to x.
perform inner-loop-para until x > end-x.
add 1 to y.
inner-loop-para.
if action = "turn on" then
move 1 to matrix(y + 1, x + 1).
if action = "turn off" then
move 0 to matrix(y + 1, x + 1).
if action = "toggle" then
move 1 to temp
subtract matrix(y + 1, x + 1) from temp
move temp to matrix(y + 1, x + 1)
end-if.
add 1 to x.