142 lines
2.3 KiB
C
142 lines
2.3 KiB
C
#include <stdio.h>
|
|
|
|
void part1() {
|
|
printf("Part 1\n");
|
|
|
|
char ch = getchar();
|
|
int visits[1000][1000];
|
|
for(int i = 0; i < 1000; i++) {
|
|
for(int j = 0; j < 1000; j++) {
|
|
visits[i][j] = 0;
|
|
}
|
|
}
|
|
int x = 0, y = 0, minx = 0, maxx = 0, miny = 0, maxy = 0;
|
|
int inc = 0;
|
|
while(ch) {
|
|
visits[x+500][y+500]++;
|
|
if(ch == '\n') { break; }
|
|
switch(ch) {
|
|
case '>':
|
|
x++;
|
|
break;
|
|
case 'v':
|
|
y--;
|
|
break;
|
|
case '<':
|
|
x--;
|
|
break;
|
|
case '^':
|
|
y++;
|
|
break;
|
|
}
|
|
if(x > maxx) {
|
|
maxx = x;
|
|
}
|
|
if(x < minx) {
|
|
minx = x;
|
|
}
|
|
if(y > maxy) {
|
|
maxy = y;
|
|
}
|
|
if(y < miny) {
|
|
miny = y;
|
|
}
|
|
ch = getchar();
|
|
}
|
|
int result = 0;
|
|
for(int i = 0; i < 1000; i++) {
|
|
for(int j = 0; j < 1000; j++) {
|
|
if(visits[i][j] > 0) {
|
|
result++;
|
|
}
|
|
}
|
|
}
|
|
printf("%d houses\n", result);
|
|
}
|
|
|
|
void part2() {
|
|
printf("Part 2\n");
|
|
char ch = getchar();
|
|
int visits[1000][1000];
|
|
for(int i = 0; i < 1000; i++) {
|
|
for(int j = 0; j < 1000; j++) {
|
|
visits[i][j] = 0;
|
|
}
|
|
}
|
|
int x1 = 0, y1 = 0, x2 = 0, y2 = 0, minx = 0, maxx = 0, miny = 0, maxy = 0;
|
|
int inc = 0;
|
|
int robo = -1;
|
|
int lastOutLngth = 0;
|
|
while(ch) {
|
|
robo++;
|
|
int usex = x1, usey = y1;
|
|
if(robo % 2 == 1) {
|
|
usex = x2;
|
|
usey = y2;
|
|
}
|
|
visits[usex+500][usey+500]++;
|
|
if(ch == '\n') { break; }
|
|
|
|
switch(ch) {
|
|
case '>':
|
|
usex++;
|
|
break;
|
|
case 'v':
|
|
usey--;
|
|
break;
|
|
case '<':
|
|
usex--;
|
|
break;
|
|
case '^':
|
|
usey++;
|
|
break;
|
|
}
|
|
|
|
if(usex > maxx) {
|
|
maxx = usex;
|
|
}
|
|
if(usex < minx) {
|
|
minx = usex;
|
|
}
|
|
if(usey > maxy) {
|
|
maxy = usey;
|
|
}
|
|
if(usey < miny) {
|
|
miny = usey;
|
|
}
|
|
// Get the next char
|
|
if(robo % 2 == 1) {
|
|
x2 = usex;
|
|
y2 = usey;
|
|
} else {
|
|
x1 = usex;
|
|
y1 = usey;
|
|
}
|
|
ch = getchar();
|
|
}
|
|
|
|
// Now find how many houses got presents
|
|
int result = 0;
|
|
for(int i = 0; i < 1000; i++) {
|
|
for(int j = 0; j < 1000; j++) {
|
|
if(visits[i][j] > 0) {
|
|
result++;
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("%d houses\n", result);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int doPart = 1;
|
|
if(argc > 1) { doPart = argv[1][0] - '0'; }
|
|
if(doPart == 1) {
|
|
part1();
|
|
} else {
|
|
part2();
|
|
}
|
|
return 0;
|
|
}
|
|
|