parent
531dee8cef
commit
d7a79f15f2
|
@ -3,5 +3,5 @@ project(aoc_2021 C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
add_executable(aoc_2021 src/main.c src/day_1.c src/day.h src/day_2.c src/puzzle_input.c src/puzzle_input.h src/day_3.c src/day_4.c src/day_5.c src/day_6.c)
|
add_executable(aoc_2021 src/main.c src/day_1.c src/day.h src/day_2.c src/puzzle_input.c src/puzzle_input.h src/day_3.c src/day_4.c src/day_5.c src/day_6.c src/day_7.c)
|
||||||
target_link_libraries(aoc_2021 m)
|
target_link_libraries(aoc_2021 m)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,533,1,1251,2,37,207,428,173,711,1167,722,462,640,375,441,952,274,1746,54,233,1381,302,441,1639,298,133,763,51,834,859,1116,93,146,496,464,902,773,1390,162,183,590,1792,79,346,752,1053,711,898,187,118,1105,604,97,1298,172,271,229,353,0,496,91,317,467,136,1459,19,343,189,1197,865,808,555,847,682,51,124,486,191,449,435,890,537,373,577,77,7,59,755,445,1461,413,10,924,918,164,11,1005,854,597,939,131,820,24,1402,376,671,1663,21,117,23,345,280,667,1719,143,1498,751,52,923,573,1252,943,786,20,852,151,9,63,826,916,123,138,59,954,586,1123,629,898,1475,753,1122,47,108,151,217,580,950,72,12,589,196,347,198,10,449,1424,152,713,435,423,446,180,73,259,559,19,291,1826,633,129,17,203,13,230,606,411,494,75,1194,677,658,751,1681,1441,1451,1348,461,327,1755,932,75,1045,48,651,799,1412,19,76,126,957,887,251,146,351,1129,305,36,654,144,735,149,60,310,440,36,154,243,163,522,187,1688,944,374,565,527,282,114,179,41,328,617,432,97,39,71,112,618,1746,8,244,415,490,352,727,608,1463,979,102,436,256,841,1216,118,1408,37,67,529,505,20,12,18,1014,107,823,788,1933,102,16,110,14,300,197,89,592,321,1596,139,66,1614,79,73,256,566,160,684,1321,207,925,415,650,68,921,377,64,1052,96,1301,841,174,480,16,726,348,241,307,1188,1057,463,479,698,613,33,808,178,37,635,115,1724,30,17,80,19,247,719,328,56,24,917,1,422,804,176,32,9,107,15,104,114,193,684,172,667,337,607,883,31,41,358,415,468,761,944,660,134,201,158,78,86,45,117,254,136,267,678,1069,36,224,54,76,904,324,765,9,38,89,109,783,593,10,1313,1345,146,399,344,743,77,74,218,1122,667,226,449,205,174,959,54,376,52,83,156,563,239,178,1097,69,129,417,684,137,85,1246,1347,111,473,1451,18,1294,964,129,648,125,578,338,885,577,546,1201,595,71,323,109,874,73,1855,487,43,1095,1473,636,88,287,282,61,767,171,584,532,269,212,321,1957,1153,339,250,188,484,9,4,176,78,770,2,1020,1105,437,687,239,4,1275,37,1506,623,1044,53,0,47,59,360,730,969,465,37,94,296,956,823,207,24,305,264,126,241,1443,150,624,70,33,1481,84,138,481,365,249,406,140,191,673,600,441,3,6,40,824,237,559,740,1146,4,767,309,1425,559,257,382,758,560,333,1371,9,315,537,28,1256,100,202,586,271,100,1038,6,633,25,1261,148,976,52,261,972,20,150,518,43,21,101,1538,122,132,340,323,393,1183,177,681,9,796,106,364,464,58,856,271,120,225,23,21,135,220,5,46,144,324,1139,1236,383,245,367,304,698,1379,392,1288,220,596,5,416,905,1845,287,4,74,369,1042,643,6,0,852,489,43,592,844,10,12,579,17,243,12,317,21,571,2,1433,427,1438,11,75,226,1293,14,104,1576,213,24,805,127,347,195,564,102,816,696,1278,205,172,238,46,74,205,537,313,1790,142,411,4,1197,208,701,67,216,384,49,362,559,933,626,333,343,241,86,316,891,13,181,850,238,61,93,407,740,660,323,43,190,606,281,660,573,241,386,59,262,1811,1,810,875,71,318,60,368,589,219,1028,934,561,1351,429,635,1382,1693,848,229,82,330,1570,232,132,433,1110,869,904,75,265,358,1132,1536,526,1177,387,1078,96,709,1013,961,600,464,1214,1135,33,696,660,41,599,43,209,807,782,491,62,1552,536,119,406,2,998,191,2,1617,28,116,127,269,425,305,37,1160,396,47,289,1110,510,278,1089,52,145,1016,1413,118,1022,141,185,25,30,254,202,315,53,603,1362,1154,36,20,24,646,177,48,128,178,1179,1621,503,703,744,517,977,13,414,626,472,916,1164,59,340,25,1517,339,896,644,108,615,34,531,84,23,79,13,41,479,108,625,225,893,170,1399,788,164,255,831,5,105,1167,1145,39,219,273,1817,147,299,1023,34,986,189,191,1239,327,595,20,195,199,195,319,93,170,1172,158,945,10,134,42,1337,148,931,893,735,1659,106,1042,1338,881,567,197,569,79,177,625,15,844,1116,484,546,307,1384,205,462,708,595,264,1055,25,186,96,484,326,635,953,1593,966,3,179,69,1210,327,90,122,55,577,214,606,685,463,331,102,823,18,409,440,68,1484,83,80,138,77,24,727,375,1203,81
|
|
@ -7,5 +7,6 @@ int day_3();
|
||||||
int day_4();
|
int day_4();
|
||||||
int day_5();
|
int day_5();
|
||||||
int day_6();
|
int day_6();
|
||||||
|
int day_7();
|
||||||
|
|
||||||
#endif //AOC_2021_DAY_H
|
#endif //AOC_2021_DAY_H
|
||||||
|
|
|
@ -31,7 +31,7 @@ parse_ret_t parse_diagnostic_data(char * buffer, void * data, uint16_t index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void part_1(diagnostic_word_t * diagnostic_data, uint16_t len) {
|
static void part_1(diagnostic_word_t * diagnostic_data, uint16_t len) {
|
||||||
uint64_t gamma_rate = 0;
|
uint64_t gamma_rate = 0;
|
||||||
uint64_t epsilon_rate = 0;
|
uint64_t epsilon_rate = 0;
|
||||||
uint16_t bit_len = diagnostic_data[0].len;
|
uint16_t bit_len = diagnostic_data[0].len;
|
||||||
|
|
18
src/day_6.c
18
src/day_6.c
|
@ -28,22 +28,6 @@ void inc_day(fish_buffer_t * buff) {
|
||||||
buff->day_ndx = (buff->day_ndx + 1) % FISH_DAYS;
|
buff->day_ndx = (buff->day_ndx + 1) % FISH_DAYS;
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_ret_t parse_byte_from_buffer(char * buffer, void * data, uint16_t index) {
|
|
||||||
uint8_t * initial_states = (uint8_t *)data;
|
|
||||||
int64_t val;
|
|
||||||
parse_ret_t res;
|
|
||||||
|
|
||||||
res = parse_long(buffer, &val, 10);
|
|
||||||
|
|
||||||
if (res != PARSER_OK) {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
initial_states[index] = (uint8_t)val;
|
|
||||||
|
|
||||||
return PARSER_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
int day_6() {
|
int day_6() {
|
||||||
parse_ret_t res;
|
parse_ret_t res;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
|
@ -51,7 +35,7 @@ int day_6() {
|
||||||
uint64_t fish_count;
|
uint64_t fish_count;
|
||||||
fish_buffer_t fish_buffer = {0};
|
fish_buffer_t fish_buffer = {0};
|
||||||
|
|
||||||
res = read_input_split_on("../inputs/day_6.txt", (void *)states_g, 500, parse_byte_from_buffer, &len, ',');
|
res = read_input_split_on("../inputs/day_6.txt", (void *)states_g, 500, parse_byte_from_buff, &len, ',');
|
||||||
|
|
||||||
if (res != PARSER_OK) {
|
if (res != PARSER_OK) {
|
||||||
printf("Unable to parse input data: %d", res);
|
printf("Unable to parse input data: %d", res);
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "puzzle_input.h"
|
||||||
|
#include "day.h"
|
||||||
|
|
||||||
|
#define MAX_POSITIONS 1024
|
||||||
|
|
||||||
|
//#define DEBUG
|
||||||
|
|
||||||
|
int int64_t_cmp(const void * a, const void * b) {
|
||||||
|
int64_t * a_int = (int64_t *)a;
|
||||||
|
int64_t * b_int = (int64_t *)b;
|
||||||
|
|
||||||
|
return (int)*a_int - (int)*b_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
int int64_t_cmp_exp(const void * a, const void * b) {
|
||||||
|
int64_t * a_int = (int64_t *)a;
|
||||||
|
int64_t * b_int = (int64_t *)b;
|
||||||
|
int64_t dist = ((int)*a_int - (int)*b_int);
|
||||||
|
|
||||||
|
int64_t exp_dist = 0;
|
||||||
|
for (int i = 0; i < labs(dist); i++) {
|
||||||
|
exp_dist += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dist < 0) {
|
||||||
|
return -(int)exp_dist;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (int)exp_dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int64_t part_1(int64_t *positions, uint16_t len) {
|
||||||
|
int64_t median;
|
||||||
|
int64_t fuel_cost = 0;
|
||||||
|
qsort((void *) positions, len, sizeof(uint64_t), int64_t_cmp);
|
||||||
|
median = positions[len/2];
|
||||||
|
|
||||||
|
for (int pos_ndx = 0; pos_ndx < len; pos_ndx++) {
|
||||||
|
fuel_cost += labs(positions[pos_ndx] - median);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fuel_cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int64_t part_2(const int64_t *positions, uint16_t len) {
|
||||||
|
int64_t max = positions[len-1];
|
||||||
|
int64_t min = positions[0];
|
||||||
|
int64_t best_pos = INT64_MAX;
|
||||||
|
int64_t best_fuel_cost = INT64_MAX;
|
||||||
|
|
||||||
|
for (int64_t i = min; i <= max; i++) {
|
||||||
|
int64_t total_fuel_cost = 0;
|
||||||
|
for (int pos_ndx = 0; pos_ndx < len; pos_ndx++) {
|
||||||
|
int64_t fuel_cost;
|
||||||
|
int64_t distance;
|
||||||
|
|
||||||
|
distance = labs(i - positions[pos_ndx]);
|
||||||
|
fuel_cost = ((distance << 6) * ((1 + distance) << 8)) >> 15;
|
||||||
|
|
||||||
|
total_fuel_cost += fuel_cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (total_fuel_cost < best_fuel_cost) {
|
||||||
|
best_fuel_cost = total_fuel_cost;
|
||||||
|
best_pos = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("PART 2: Best Pos %ld\n", best_pos);
|
||||||
|
#endif
|
||||||
|
return best_fuel_cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t part_2_fuel_calc(int64_t median, int64_t crab_pos) {
|
||||||
|
return labs(crab_pos - median);
|
||||||
|
}
|
||||||
|
|
||||||
|
int day_7() {
|
||||||
|
int64_t positions[MAX_POSITIONS] = {0};
|
||||||
|
uint16_t len;
|
||||||
|
parse_ret_t res;
|
||||||
|
|
||||||
|
res = read_input_split_on("../inputs/day_7.txt", (void *)positions, MAX_POSITIONS, parse_long_from_buff, &len, ',');
|
||||||
|
|
||||||
|
if (res != PARSER_OK) {
|
||||||
|
printf("Unable to parse input: %d\n", res);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("PART 1: Total fuel cost is %ld\n", part_1(positions, len));
|
||||||
|
printf("PART 2: Total fuel cost is %ld\n", part_2(positions, len));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -34,6 +34,9 @@ int main(int argc, char * argv[]) {
|
||||||
case 6:
|
case 6:
|
||||||
day_6();
|
day_6();
|
||||||
break;
|
break;
|
||||||
|
case 7:
|
||||||
|
day_7();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Invalid day ding dong!\n");
|
printf("Invalid day ding dong!\n");
|
||||||
return -2;
|
return -2;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "day.h"
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
@ -181,3 +182,19 @@ parse_ret_t read_input_multi_line(const char * file_path, void * data, uint16_t
|
||||||
parse_ret_t read_input_single_line(const char * file_path, void * data, uint16_t max_len, input_parser_t parser, uint16_t * len) {
|
parse_ret_t read_input_single_line(const char * file_path, void * data, uint16_t max_len, input_parser_t parser, uint16_t * len) {
|
||||||
read_input_split_on(file_path, data, max_len, parser, len, '\n');
|
read_input_split_on(file_path, data, max_len, parser, len, '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parse_ret_t parse_byte_from_buff(char * buffer, void * data, uint16_t index) {
|
||||||
|
uint8_t * initial_states = (uint8_t *)data;
|
||||||
|
int64_t val;
|
||||||
|
parse_ret_t res;
|
||||||
|
|
||||||
|
res = parse_long(buffer, &val, 10);
|
||||||
|
|
||||||
|
if (res != PARSER_OK) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
initial_states[index] = (uint8_t)val;
|
||||||
|
|
||||||
|
return PARSER_OK;
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ parse_ret_t read_input_multi_line(const char * file_path, void * data, uint16_t
|
||||||
|
|
||||||
// Parsers
|
// Parsers
|
||||||
parse_ret_t parse_long_from_buff(char * buffer, void * data, uint16_t index);
|
parse_ret_t parse_long_from_buff(char * buffer, void * data, uint16_t index);
|
||||||
|
parse_ret_t parse_byte_from_buff(char * buffer, void * data, uint16_t index);
|
||||||
parse_ret_t parse_long_base_2_from_buff(char * buffer, void * data, uint16_t index);
|
parse_ret_t parse_long_base_2_from_buff(char * buffer, void * data, uint16_t index);
|
||||||
|
|
||||||
//Helpers
|
//Helpers
|
||||||
|
|
Loading…
Reference in New Issue