Day 1 Check In

main
Joey Hines 2021-12-01 19:43:08 -07:00
commit a02302a516
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
7 changed files with 2172 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
.idea
cmake-build-*

6
CMakeLists.txt 100644
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(aoc_2021 C)
set(CMAKE_C_STANDARD 99)
add_executable(aoc_2021 src/main.c src/day_1.c src/day.h)

3
README.md 100644
View File

@ -0,0 +1,3 @@
# Advent of C(ode) 2021
[AOC 2021](https://adventofcode.com/2021) attempt using C. Trying to keep it bare bones, minimal dependencies, no malloc,
the pain of c, etc.

2000
inputs/day_1.txt 100644

File diff suppressed because it is too large Load Diff

6
src/day.h 100644
View File

@ -0,0 +1,6 @@
#ifndef AOC_2021_DAY_H
#define AOC_2021_DAY_H
int day_1();
#endif //AOC_2021_DAY_H

129
src/day_1.c 100644
View File

@ -0,0 +1,129 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include "day.h"
#define DATA_MAX_LEN 2048
int parse_long_from_buff(char * buffer, int64_t * val) {
*val = strtol(buffer, NULL, 16);
if (*val == LONG_MIN || *val == LONG_MAX) {
return -2;
}
return 0;
}
int read_input(const char * file_path, int64_t *data, uint16_t max_len) {
char buffer[64];
int c;
int ret = 0;
FILE * file = fopen(file_path, "r");
int data_ndx = 0;
if (file != NULL) {
int buffer_ndx = 0;
while ((c = fgetc(file)) != EOF) {
if (c == '\n') {
buffer[buffer_ndx] = 0;
ret = parse_long_from_buff(buffer, &data[data_ndx]);
if (ret < 0) {
break;
}
data_ndx++;
if (data_ndx >= max_len) {
ret = -3;
break;
}
buffer_ndx = 0;
}
else {
buffer[buffer_ndx] = (char)c;
buffer_ndx++;
}
}
if (buffer_ndx > 0) {
ret = parse_long_from_buff(buffer, &data[data_ndx]);
if (ret == 0) {
data_ndx++;
}
}
}
else {
ret = -1;
}
if (ret == 0) {
ret = data_ndx;
}
if (file != NULL) {
fclose(file);
}
return ret;
}
void part_1(const int64_t * data, uint16_t data_len) {
int64_t last_value;
int16_t inc_count = 0;
last_value = data[0];
for (uint16_t i = 1; i < (uint16_t)data_len; i++) {
if (data[i] > last_value) {
inc_count++;
}
last_value = data[i];
}
printf("PART 1: %d values increased!\n", inc_count);
}
void part_2(const int64_t * data, uint16_t data_len) {
int64_t old_window_sum;
int64_t window_sum;
int16_t inc_count = 0;
old_window_sum = data[0] + data[1] + data[2];
for (uint16_t i = 3; i < (uint16_t)data_len; i++) {
window_sum = old_window_sum - data[i-3] + data[i];
if (window_sum > old_window_sum) {
inc_count++;
}
old_window_sum = window_sum;
}
printf("PART 2: %d windows increased!\n", inc_count);
}
int day_1() {
char * path = "../inputs/day_1.txt";
int data_len;
int64_t data[DATA_MAX_LEN];
data_len = read_input(path, data, DATA_MAX_LEN);
if (data_len < 0) {
printf("read_input returned an error: %d\n", data_len);
return data_len;
}
part_1(data, data_len);
part_2(data, data_len);
return 0;
}

26
src/main.c 100644
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "day.h"
int main(int argc, char * argv[]) {
uint64_t day_number;
if (argc < 2) {
printf("Advent of **C**ode\n");
printf("%s DAY_NUMBER", argv[0]);
return -1;
}
day_number = strtol(argv[1], NULL, 10);
switch (day_number) {
case 1:
day_1();
break;
default:
printf("Invalid day ding dong!\n");
return -2;
}
}