aoc-2021/src/day_2.c

123 lines
2.9 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "puzzle_input.h"
#include "day.h"
#define MAX_COURSE_SIZE 1024
typedef enum {
FORWARD,
DOWN ,
UP,
} course_dir_t;
typedef struct {
course_dir_t dir;
int64_t units;
} course_entry_t;
int parse_course_entry(char * buffer, void * data, uint16_t data_index) {
char * save_ptr;
char * dir_str = strtok_r(buffer, " ", &save_ptr);
char * unit_str = strtok_r(NULL, " ", &save_ptr);
parse_ret_t ret;
int64_t units;
ret = parse_long(unit_str, &units, 10);
if (ret == PARSER_OK) {
course_dir_t dir;
switch (dir_str[0]) {
case 'f':
dir = FORWARD;
break;
case 'd':
dir = DOWN;
break;
case 'u':
dir = UP;
break;
default:
ret = PARSER_INVALID;
}
if (ret == PARSER_OK) {
course_entry_t entry = {
.dir = dir,
.units = units
};
course_entry_t *course = (course_entry_t *)data;
course[data_index] = entry;
}
}
return ret;
}
static void part_1(course_entry_t * course, uint16_t course_len) {
int64_t horizontal_pos = 0;
int64_t depth = 0;
for (int course_ndx = 0; course_ndx < course_len; course_ndx++) {
int dir_sign = 1;
switch (course[course_ndx].dir) {
case FORWARD:
horizontal_pos += course[course_ndx].units;
break;
case UP:
dir_sign = -1;
case DOWN:
depth += dir_sign*course[course_ndx].units;
}
}
printf("PART 1: Final Horizontal * Final Depth = %ld\n", horizontal_pos*depth);
}
static void part_2(course_entry_t * course, uint16_t course_len) {
int64_t horizontal_pos = 0;
int64_t depth = 0;
int64_t aim = 0;
for (int course_ndx = 0; course_ndx < course_len; course_ndx++) {
int dir_sign = 1;
int64_t units = course[course_ndx].units;
switch (course[course_ndx].dir) {
case FORWARD:
horizontal_pos += units;
depth += units * aim;
break;
case UP:
dir_sign = -1;
case DOWN:
aim += dir_sign*units;
}
}
printf("PART 2: Final Horizontal * Final Depth = %ld\n", horizontal_pos*depth);
}
int day_2() {
course_entry_t course[MAX_COURSE_SIZE];
uint16_t data_len;
parse_ret_t res = read_input_single_line("../inputs/day_2.txt", (void *) course, MAX_COURSE_SIZE,
parse_course_entry,
&data_len);
if (res == PARSER_OK) {
part_1(course, data_len);
part_2(course, data_len);
}
else {
printf("Failed to parse data: %d", res);
}
return 0;
}