summaryrefslogtreecommitdiff
path: root/2.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2021-12-09 13:32:48 +0000
committerIan C <ianc@noddybox.co.uk>2021-12-09 13:32:48 +0000
commit8833b7a5b246cad02e253838c07ba1689834e523 (patch)
treede8ae5cbb97a802a7d2ce30e7db37798ac02be00 /2.c
Initial checkin
Diffstat (limited to '2.c')
-rw-r--r--2.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/2.c b/2.c
new file mode 100644
index 0000000..84b511c
--- /dev/null
+++ b/2.c
@@ -0,0 +1,50 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+int main(void)
+{
+ char buff[80];
+ int depth = 0;
+ int horiz = 0;
+
+ while(fgets(buff, sizeof buff, stdin))
+ {
+ char *tok[2] = {0};
+
+ tok[0] = strtok(buff," ");
+
+ if (tok[0]) tok[1] = strtok(NULL, " ");
+
+ if (tok[0] && tok[1])
+ {
+ int i = atoi(tok[1]);
+
+ if(strcmp(tok[0], "forward") == 0)
+ {
+ horiz += i;
+ }
+
+ if(strcmp(tok[0], "down") == 0)
+ {
+ depth += i;
+ }
+
+ if(strcmp(tok[0], "up") == 0)
+ {
+ depth -= i;
+ }
+ }
+ else
+ {
+ printf("Bad input");
+ exit(1);
+ }
+ }
+
+ printf("depth=%d\n", depth);
+ printf("horiz=%d\n", horiz);
+ printf("product=%d\n", depth*horiz);
+
+ return 0;
+}