-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.c
84 lines (68 loc) · 1.49 KB
/
3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define height 323
#define width 31
#define cases 5
void reset(char line[], int length);
void resetI(int line[], int length);
int main (int argc, char *argv[])
{
FILE *in = fopen("in3", "r");
/*
Right 1, down 1.
Right 3, down 1. (This is the slope you already checked.)
Right 5, down 1.
Right 7, down 1.
Right 1, down 2.
*/
int down[cases] = {1,1,1,1,2};
int right[cases] = {1,3,5,7,1};
char currentLine[width+1];
int y = 0;
int x[cases];
resetI(x, cases);
int treeCount[cases];
resetI(treeCount, cases);
int prod = 1;
// We start at (0,0), where no tree is
fscanf(in, "%s\n", currentLine);
reset(currentLine, width);
while(fscanf(in, "%s\n", currentLine))
{
if (strlen(currentLine) < 30)
{
printf("Empty line - I reached my goal\n");
break;
}
y++;
for (int i = 0; i < cases; i++)
{
if (y % down[i] == 0)
{
x[i] = (x[i] + right[i]) % width;
if (currentLine[x[i]] == '#')
treeCount[i]++;
}
}
reset(currentLine, width);
}
for (int i = 0; i < cases; i++)
{
printf("Encountered %d trees for case %d.\n", treeCount[i], i);
prod *= treeCount[i];
}
printf("Product: %d\n", prod);
fclose(in);
return 0;
}
void reset(char line[], int length)
{
for (int i = 0; i < length; i++)
line[i] = 0;
}
void resetI(int line[], int length)
{
for (int i = 0; i < length; i++)
line[i] = 0;
}