summaryrefslogtreecommitdiff
path: root/5a.c
diff options
context:
space:
mode:
authorian.cowburn <ian.cowburn@ianc-work-macbook.local>2021-12-09 15:50:12 +0000
committerian.cowburn <ian.cowburn@ianc-work-macbook.local>2021-12-09 15:50:12 +0000
commite34fd16b64ebf0ddd7ede60e938935320135d3ee (patch)
tree54e10e2305a8e1934c076c388b05fb6ccab2179f /5a.c
parent0d1e83e9ec2d28b792e912fa21e1aa2815303874 (diff)
Done day 5.
Diffstat (limited to '5a.c')
-rw-r--r--5a.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/5a.c b/5a.c
new file mode 100644
index 0000000..2776906
--- /dev/null
+++ b/5a.c
@@ -0,0 +1,107 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#define MAX_BOARDS 100
+
+static char *ReadLine(char *p, size_t size, FILE *fp)
+{
+ if ((p=fgets(p, size, fp)))
+ {
+ size_t l = strlen(p);
+
+ while(l && p[l-1] == '\n')
+ {
+ p[--l] = 0;
+ }
+ }
+
+ return p;
+}
+
+static int Max(int a, int b)
+{
+ return a > b ? a : b;
+}
+
+static int Min(int a, int b)
+{
+ return a < b ? a : b;
+}
+
+int main(void)
+{
+ char buff[1024];
+ int board[1000][1000] = {0};
+ char *p = NULL;
+ int f = 0;
+ int x1 = 0;
+ int y1 = 0;
+ int x2 = 0;
+ int y2 = 0;
+ int x = 0;
+ int y = 0;
+ int sum = 0;
+
+ while (ReadLine(buff, sizeof buff, stdin))
+ {
+ p = strtok(buff, " ,->");
+ x1 = atoi(p);
+ p = strtok(NULL, " ,->");
+ y1 = atoi(p);
+ p = strtok(NULL, " ,->");
+ x2 = atoi(p);
+ p = strtok(NULL, " ,->");
+ y2 = atoi(p);
+
+ if (x1 == x2)
+ {
+ for(y = Min(y1, y2); y <= Max(y1, y2); y++)
+ {
+ board[y][x1]++;
+ }
+ }
+ else if (y1 == y2)
+ {
+ for(x = Min(x1, x2); x <= Max(x1, x2); x++)
+ {
+ board[y1][x]++;
+ }
+ }
+ else
+ {
+ int dx;
+ int dy;
+
+ dx = x1 < x2 ? 1 : -1;
+ dy = y1 < y2 ? 1 : -1;
+
+ x = x1;
+ y = y1;
+
+ while(x != x2 && y != y2)
+ {
+ board[y][x]++;
+ x += dx;
+ y += dy;
+ }
+
+ board[y][x]++;
+ }
+ }
+
+ for(y = 0; y < 999; y++)
+ {
+ for(x = 0; x < 999; x++)
+ {
+ if (board[y][x] > 1)
+ {
+ sum++;
+ }
+ }
+ }
+
+ printf("sum = %d\n", sum);
+
+ return 0;
+}