day 2
This commit is contained in:
72
day1.c
72
day1.c
@@ -1,23 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdcountof.h>
|
||||
#include "lib.h"
|
||||
|
||||
#define DBG 1
|
||||
|
||||
#define RUN_PART1 1
|
||||
#define RUN_PART2 1
|
||||
#define RUN_TEST1 1
|
||||
#define RUN_TEST2 1
|
||||
|
||||
#define TEST1_EXPECT 3
|
||||
#define TEST2_EXPECT 6
|
||||
|
||||
static
|
||||
char input[] = {
|
||||
#embed "day1_input"
|
||||
#embed "day1_input.txt"
|
||||
};
|
||||
|
||||
static
|
||||
char test[] = {
|
||||
#embed "day1_test"
|
||||
#embed "day1_test.txt"
|
||||
};
|
||||
|
||||
unsigned grabnum(const char *s, const char **end) {
|
||||
unsigned r = 0;
|
||||
for (; *s >= '0' && *s <= '9'; s++)
|
||||
r = r * 10 + (*s - '0');
|
||||
*end = s;
|
||||
return r;
|
||||
}
|
||||
|
||||
static
|
||||
unsigned long do_part1(size_t src_len, const char src[src_len]) {
|
||||
const char *s = src;
|
||||
unsigned long count = 0;
|
||||
@@ -36,6 +39,7 @@ unsigned long do_part1(size_t src_len, const char src[src_len]) {
|
||||
return count;
|
||||
}
|
||||
|
||||
static
|
||||
unsigned do_part2(size_t src_len, const char src[src_len]) {
|
||||
const char *s = src, *ns = s + 1;
|
||||
unsigned count = 0;
|
||||
@@ -70,24 +74,40 @@ unsigned do_part2(size_t src_len, const char src[src_len]) {
|
||||
return count;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("PART 1 TEST: ");
|
||||
if (unsigned long v = do_part1(countof(test), test); v != 3) {
|
||||
printf("FAILED (got %lu, expected 3)\n", v);
|
||||
void _start() {
|
||||
#if RUN_TEST1
|
||||
print("PART 1 TEST: ");
|
||||
if (unsigned long v = do_part1(countof(test), test); v != TEST1_EXPECT) {
|
||||
print("FAILED (got ");
|
||||
printd(v);
|
||||
print(", expected " xstr(TEST1_EXPECT) ")\n");
|
||||
} else {
|
||||
printf("PASSED\n");
|
||||
print("PASSED\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
printf("PART 1 RESULT: %lu\n",
|
||||
do_part1(countof(input), input));
|
||||
#if RUN_PART1
|
||||
print("PART 1 RESULT: ");
|
||||
printd(do_part1(countof(input), input));
|
||||
print("\n");
|
||||
#endif
|
||||
|
||||
printf("PART 2 TEST: ");
|
||||
if (unsigned v = do_part2(countof(test), test); v != 6) {
|
||||
printf("FAILED (got %u, expected 6)\n", v);
|
||||
#if RUN_TEST2
|
||||
print("PART 2 TEST: ");
|
||||
if (unsigned v = do_part2(countof(test), test); v != TEST2_EXPECT) {
|
||||
print("FAILED (got ");
|
||||
printd(v);
|
||||
print(", expected " xstr(TEST2_EXPECT) ")\n");
|
||||
} else {
|
||||
printf("PASSED\n");
|
||||
print("PASSED\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
printf("PART 2 RESULT: %u\n",
|
||||
do_part2(countof(input), input));
|
||||
#if RUN_PART2
|
||||
print("PART 2 RESULT: ");
|
||||
printd(do_part2(countof(input), input));
|
||||
print("\n");
|
||||
#endif
|
||||
|
||||
syscall(SYS_exit_group, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user