G-code is a set of instructions used to manipulate machines in CNC, 3d printing, or other similar applications. It controlls the position of a tool, manuvering it to create a path.
Your goal is to interpret a cut-down version of g-code, to find where the tool ends up after it has finished executing.
You will receive the input as a series of g-code commands, with their arguments. Everything after the first semicolon in a line ;, denoting a comment, is ignored. For example:
G90; enter absolute mode
G00 X2 Y2; quickly go to (2,2)
G01 X10 Y10 F100; go to (10,10) at a feed rate of 100
G91; enter relative mode
G01 X5 Y0; move relatively by (5,0)
The final position would be (15,10,0)
You only need to handle these commands:
| Command | Arguments | Description |
|---|---|---|
| G90 | None | Enter absolute mode |
| G91 | None | Enter relative mode |
| G00 | X, Y, Z coordinates | Go to X, Y, Z coordinates quickly |
| G01 | X, Y, Z coordinates, F feed rate | Go to X, Y, Z coordinates at feed rate F |
Importantly, arguments are all optional. In other words, if an axis is omitted, the tool will simply not move on that axis. If the feedrate is omitted, it will use the previous feed rate. You can assume that at least one axis is always provided. Each given axis will be provided in alphabetical order, followed by the feed rate if there is one.
The tool position will always be positive in every axis. However a relative movement may contain negative values.
Note that the feed rate will not actually affect your final position so you can ignore it. But it may be included in your input, so you must not error if this happens.
The first command will always be G90 or G91 (although these commands may also appear subsequently), and the tool will always start at (0,0,0).
Comments may also be present. The line will be formatted as command and arguments (each separated by a space), followed by an optional semicolon and a comment. The semicolon will not have a space before it.
While input and output is flexible, you must still parse each line. I.e.: you cannot format your input such that the data structure itself indicates the commands and arguments. It must be a string or similar containing the entire command.
Code golf scoring.
Test cases
====
G90
G00 X2 Y2; MOVE TO START POINT
G01 X10 Y10 F100
====> (10,10,0)
====
G90
G00 X10 Y10
G91; ENTER RELATIVE MODE
G01 X1 Y2 F100
====> (11,12,0)
====
G90
G00 X1 Y1 Z1
G90
G00 X2 Y2 Z2
====> (2,2,2)
====
G91
G00 X1 Y1 Z1
G90
G00 X2 Y2 Z2
G91
G01 X1 Y2 Z3 F100
====> (3,4,5)
====
G90
G00 X2 Y2
G91
G00 Y2
====> (2,4,0)
====
G90
G00 X10 Y10 Z10
G91
G00 X-2 Y-3 Z-4
====> (8,7,6)
====
G90
G00 X12 Z34; TODO: REPLACE WITH;G00 X23 Z45
====> (12,0,34)
====
G90
G00 X10 Y10
G90
====> (10,10,0)
====
G90
G00 X10 Y10
G90
G91
G01 X1 Y1
G90
====> (11,11,0)
====
G90
G00 X3 Z5
G00 Y7
====> (3,7,5)

WITH;G00is just an attempt at confusing the parser some more? :-) (I would have put that one as a separate test case, though) \$\endgroup\$(0,0,0)rather than(0,0)? \$\endgroup\$G90G00 X3 Z5G00 Y7, which I believe should produce(3, 7, 5)due to the rule about not moving in unprovided axes. Also, probably worth having a test to show that zero values are handled. \$\endgroup\$