Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@randrew

randrew/part1.c Secret

Last active December 3, 2020 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrew/1e3f324fc790e0bc197b03030ddf638c to your computer and use it in GitHub Desktop.
Save randrew/1e3f324fc790e0bc197b03030ddf638c to your computer and use it in GitHub Desktop.
aoc 2020 day 3
#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);
}
#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