...
 
Commits (3)
......@@ -8,7 +8,7 @@ BIN_DIR := bin
COMMON_SRC := $(wildcard $(SOURCEDIR)/common/*.c)
COMMON_OBJ := $(BUILD)/common/$(notdir $(COMMON_SRC:.c=.o))
all: day1p1.out day1p2.out day2p1.out day2p2.out
all: day1p1.out day1p2.out day2p1.out day2p2.out day4p1.out day4p2.out
$(BUILD)/%.o: $(SOURCEDIR)/%.c Makefile | setup_builddir
@printf "\033[1mCC\033[0m $<\n"
......@@ -34,3 +34,9 @@ day2p1.out: $(BUILD)/2p1.o $(COMMON_OBJ)
day2p2.out: $(BUILD)/2p2.o $(COMMON_OBJ)
@$(CC) $^ -o $(BIN_DIR)/[email protected]
@printf "\033[1mLINK\033[0m [email protected]\n"
day4p1.out: $(BUILD)/4p1.o $(COMMON_OBJ)
@$(CC) $^ -o $(BIN_DIR)/[email protected]
@printf "\033[1mLINK\033[0m [email protected]\n"
day4p2.out: $(BUILD)/4p2.o $(COMMON_OBJ)
@$(CC) $^ -o $(BIN_DIR)/[email protected]
@printf "\033[1mLINK\033[0m [email protected]\n"
# Advent-of-Code-2019
https://adventofcode.com/2019 solutions
Requires some std c compiler and a make.
To compile. Run `make all`
Binaries and test data will be located in /bin
#include <stdio.h>
int is_valid(int val)
{
char buf[16];
int hasDouble = 0, valid = 1;
int i, len;
len = sprintf(buf, "%d", val);
for(i = 1; i < len; i++)
{
if(buf[i] < buf[i-1])
{
valid = 0;
break;
}
if(buf[i-1] == buf[i])
{
hasDouble = 1;
}
}
return (valid && hasDouble);
}
int part1(int start, int end)
{
int sum = 0;
int i;
for(i = start; i <= end; i++)
{
if(is_valid(i))
{
sum++;
}
}
return sum;
}
int main(int argc, char* argv[])
{
int start, end, status;
if(argc != 3)
{
fprintf(stderr, "%s takes 2 arguments. Start & End numbers\n", argv[0]);
return -1;
}
status = sscanf(argv[1], "%d", &start);
status += sscanf(argv[2], "%d", &end);
if(status != 2)
{
fprintf(stderr, "%s requires 2 numbers as input\n", argv[0]);
return -1;
}
int result = part1(start, end);
printf("%d\n", result);
return 0;
}
#include <stdio.h>
int is_valid(int val)
{
char buf[16];
int hasDouble = 0, valid = 1;
int inARow[10] = {1,1,1,1,1,1,1,1,1,1};
int i, len;
len = sprintf(buf, "%d", val);
for(i = 1; i < len; i++)
{
if(buf[i] < buf[i-1])
{
valid = 0;
break;
}
if(buf[i-1] == buf[i])
{
inARow[buf[i]-0x30]++;
}
}
for(i = 0; i < 10; i++)
{
if(inARow[i] == 2)
{
hasDouble = 1;
}
}
return (valid && hasDouble);
}
int part2(int start, int end)
{
int sum = 0;
int i;
for(i = start; i <= end; i++)
{
if(is_valid(i))
{
sum++;
}
}
return sum;
}
int main(int argc, char* argv[])
{
int start, end, status;
if(argc != 3)
{
fprintf(stderr, "%s takes 2 arguments. Start & End numbers\n", argv[0]);
return -1;
}
status = sscanf(argv[1], "%d", &start);
status += sscanf(argv[2], "%d", &end);
if(status != 2)
{
fprintf(stderr, "%s requires 2 numbers as input\n", argv[0]);
return -1;
}
int result = part2(start, end);
printf("%d\n", result);
return 0;
}