-
-
Save randrew/1e3f324fc790e0bc197b03030ddf638c to your computer and use it in GitHub Desktop.
aoc 2020 day 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
int main(void) | |
{ | |
FILE *f = fopen("input.txt", "r"); | |
char *buf = malloc(1 << 26); | |
int width = 0, height = 0, x, y, hits; | |
while (fgets(buf + width * height, BUFSIZ, f)) | |
{ | |
width = (int)strlen(buf + width * height) - 1; | |
height++; | |
} | |
for (x = 0, y = 1, hits = 0; y < height; y++) | |
{ | |
x = (x + 3) % width; | |
if (buf[y * width + x] == '#') hits++; | |
} | |
printf("%d\n", hits); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
int main(void) | |
{ | |
FILE *f = fopen("input.txt", "r"); | |
char *buf = malloc(1 << 26); | |
int width = 0, height = 0, dx[] = {1, 3, 5, 7, 1}, dy[] = {1, 1, 1, 1, 2}; | |
long long total = 1; | |
while (fgets(buf + width * height, BUFSIZ, f)) | |
{ | |
width = (int)strlen(buf + width * height) - 1; | |
height++; | |
} | |
for (int i = 0, x, y, hits; i < 5; i++) | |
{ | |
for (x = 0, y = dy[i], hits = 0; y < height; y += dy[i]) | |
{ | |
x = (x + dx[i]) % width; | |
if (buf[y * width + x] == '#') hits++; | |
} | |
total *= hits; | |
} | |
printf("%lld\n", total); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment