Skip to content

Commit

Permalink
Port 1b to COBOL
Browse files Browse the repository at this point in the history
  • Loading branch information
ttencate committed Dec 10, 2016
1 parent 9c09d09 commit 0dfc1ce
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
98 changes: 98 additions & 0 deletions 01_cobol/01b.cob
@@ -0,0 +1,98 @@
identification division.
program-id. 01a.

environment division.
input-output section.
file-control.
select inputfile assign to keyboard
organization is line sequential.

data division.
file section.
fd inputfile is external
record is varying in size
data record is input-line.
01 input-line pic A(999).
working-storage section.
01 idx pic 999 value 001.
01 len pic 9.
01 lr pic A.
01 distance pic 999.
01 done pic 9 value 0.
01 visited.
05 row occurs 2001 times.
10 cell pic 9 value 0 occurs 2001 times.

01 x pic S999 value +000.
01 y pic S999 value +000.
01 total-distance pic 999.
01 direction pic 9 value 1.

procedure division.
main.
open input inputfile.
read inputfile.
close inputfile.

perform until done = 1
perform read-command
perform apply-command
end-perform

move function abs(x) to x.
move function abs(y) to y.
add x to y giving total-distance.
display total-distance.

stop run.

read-command.
move input-line(idx:1) to lr.
if lr <> "R" and lr <> "L" then
move 1 to done
end-if.
add 1 to idx.
move 0 to len.
perform until input-line(idx + len:1) = "," or
input-line(idx + len:1) = " "
add 1 to len
end-perform.
move ' ' to distance.
move input-line(idx:len) to distance.
add len to idx.
add 2 to idx.

apply-command.
if lr = "R" then
add 1 to direction
else
subtract 1 from direction
end-if.
if direction = 5 then
move 1 to direction
end-if.
if direction = 0 then
move 4 to direction
end-if.

perform until distance = 0
if direction = 1 then
add 1 to y
end-if
if direction = 2 then
add 1 to x
end-if
if direction = 3 then
subtract 1 from y
end-if
if direction = 4 then
subtract 1 from x
end-if
if cell(x + 1000, y + 1000) = 1 then
move 1 to done
move 0 to distance
else
move 1 to cell(x + 1000, y + 1000)
subtract 1 from distance
end-if
end-perform.
2 changes: 1 addition & 1 deletion 01_cobol/Makefile
@@ -1,5 +1,5 @@
.PHONY: all
all: 01a
all: 01a 01b

01%: 01%.cob
cobc -O -x -o $@ $<
10 changes: 9 additions & 1 deletion 01_cobol/README.md
Expand Up @@ -24,4 +24,12 @@ value +000.`. The `S999` bit means "a sign, followed by three digits"; this
specifies how the variable will be laid out in memory. As a string, unless you
write `usage is computational`, but why should I? This format, sadly, makes
reading the variable-length integers in the input difficult, but I'm not going
to cheat by formatting the input better.
to cheat by formatting the input in a more suitable way. I _am_ going to cheat
by assuming limits on the length of the input and the length of each individual
command, though.

Anyway, after reading the input correctly, running the actual commands was
pretty straightforward. For the second part of the problem, I needed to keep
track of where we've been, which of course is what we can use a table for.

So, I've COBOLed. Let's call it a once in a lifetime experience. I hope.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -37,10 +37,10 @@ both on what I know, and what might be suitable for such puzzles):
* J
* APL
* Ada
* Cobol

Used:

* Cobol
* Fortran 90
* Pascal
* Python
Expand Down

0 comments on commit 0dfc1ce

Please sign in to comment.