Skip to content

Instantly share code, notes, and snippets.

@vijfhoek
Created November 6, 2021 21:57
Show Gist options
  • Save vijfhoek/8e3c4437930fdd58906e98c7e4e09501 to your computer and use it in GitHub Desktop.
Save vijfhoek/8e3c4437930fdd58906e98c7e4e09501 to your computer and use it in GitHub Desktop.
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(10) value 0.
01 brightness pic 9(10).
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.
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
add 1 to matrix(y + 1, x + 1).
if action = "turn off" and matrix(y + 1, x + 1) > 0 then
subtract 1 from matrix(y + 1, x + 1).
if action = "toggle" then
add 2 to matrix(y + 1, x + 1).
add 1 to x.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment