This commit is contained in:
Zack Buhman 2024-12-05 03:16:25 -06:00
parent 071cf1748b
commit 4db7412d48
9 changed files with 1568 additions and 2 deletions

1366
day5/input.txt Normal file

File diff suppressed because it is too large Load Diff

15
day5/input.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_day5_input_txt_start __asm("_binary_day5_input_txt_start");
extern uint32_t _binary_day5_input_txt_end __asm("_binary_day5_input_txt_end");
extern uint32_t _binary_day5_input_txt_size __asm("_binary_day5_input_txt_size");
#ifdef __cplusplus
}
#endif

28
day5/sample1.txt Normal file
View File

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

15
day5/sample1.txt.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t _binary_day5_sample1_txt_start __asm("_binary_day5_sample1_txt_start");
extern uint32_t _binary_day5_sample1_txt_end __asm("_binary_day5_sample1_txt_end");
extern uint32_t _binary_day5_sample1_txt_size __asm("_binary_day5_sample1_txt_size");
#ifdef __cplusplus
}
#endif

126
day5/solution.c Normal file
View File

@ -0,0 +1,126 @@
#include <stdbool.h>
#include "parse.h"
#include "printf.h"
struct rule {
char l;
char r;
};
static void error(void)
{
printf("day5: invalid input\n");
while (1);
}
static const char * parse_rules(const char * input, const char * end,
struct rule * rules, int * rules_length)
{
int i = 0;
while (input < end) {
int num;
input = parse_base10(input, &num);
if (*input++ != '|') { error(); }
rules[i].l = num;
input = parse_base10(input, &num);
if (*input++ != '\n') { error(); }
rules[i].r = num;
i += 1;
if (*input == '\n')
break; // end of rules
}
*rules_length = i;
return parse_skip(input, '\n');
}
static const char * parse_update(const char * input, const char * end,
char * update, int * update_length)
{
int i = 0;
while (input < end) {
int num;
input = parse_base10(input, &num);
input = parse_skip(input, ',');
update[i] = num;
i += 1;
if (*input == '\n')
break; // end of update
}
*update_length = i;
return parse_skip(input, '\n');
}
static bool rule_match(const struct rule * rule, int a, int b)
{
return rule->l == a && rule->r == b;
}
static bool sort_numbers(const struct rule * rules, int rules_length,
char * nums, int nums_length)
{
bool at_least_one_swap = false;
while (true) {
bool swapped = false;
for (int i = 0; i < nums_length - 1; i++) {
int a = nums[i + 0];
int b = nums[i + 1];
for (int j = 0; j < rules_length; j++) {
if (rule_match(&rules[j], b, a)) {
swapped = true;
at_least_one_swap = true;
nums[i + 0] = b;
nums[i + 1] = a;
}
}
}
if (!swapped)
break;
}
return at_least_one_swap;
}
static int solve(const char * input, int length, bool correctly_ordered)
{
const char * end = input + length;
struct rule rules[1200];
int rules_length;
input = parse_rules(input, end, rules, &rules_length);
int sum = 0;
char update[24];
while (input < end) {
int update_length;
input = parse_update(input, end, update, &update_length);
bool at_least_one_swap = sort_numbers(rules, rules_length, update, update_length);
if (correctly_ordered ^ at_least_one_swap) {
sum += update[update_length / 2];
}
}
return sum;
}
int day5_part1(const char * input, int length)
{
return solve(input, length, true);
}
int day5_part2(const char * input, int length)
{
return solve(input, length, false);
}

View File

@ -7,6 +7,8 @@
#include "day3/input.txt.h"
#include "day4/sample1.txt.h"
#include "day4/input.txt.h"
#include "day5/sample1.txt.h"
#include "day5/input.txt.h"
static struct start_size sample[][2] = {
{
@ -33,6 +35,12 @@ static struct start_size sample[][2] = {
{ ( char *)&_binary_day4_sample1_txt_start,
(uint32_t)&_binary_day4_sample1_txt_size },
},
{
{ ( char *)&_binary_day5_sample1_txt_start,
(uint32_t)&_binary_day5_sample1_txt_size },
{ ( char *)&_binary_day5_sample1_txt_start,
(uint32_t)&_binary_day5_sample1_txt_size },
},
};
static struct start_size input[] = {
@ -44,4 +52,6 @@ static struct start_size input[] = {
(uint32_t)&_binary_day3_input_txt_size },
{ ( char *)&_binary_day4_input_txt_start,
(uint32_t)&_binary_day4_input_txt_size },
{ ( char *)&_binary_day5_input_txt_start,
(uint32_t)&_binary_day5_input_txt_size },
};

View File

@ -6,10 +6,13 @@ int day3_part1(char * input, int length);
int day3_part2(char * input, int length);
int day4_part1(char * input, int length);
int day4_part2(char * input, int length);
int day5_part1(char * input, int length);
int day5_part2(char * input, int length);
part_func solution[][2] = {
{day1_part1, day1_part2},
{day2_part1, day2_part2},
{day3_part1, day3_part2},
{day4_part1, day4_part2},
{day5_part1, day5_part2},
};

View File

@ -370,7 +370,7 @@ int main()
done = true;
done_frame = frame;
}
if (done && (frame == done_frame + 2))
if (done && (frame == done_frame + 3))
break;
}

View File

@ -11,4 +11,7 @@ DAY_OBJ = \
day3/solution.o \
day4/sample1.txt.o \
day4/input.txt.o \
day4/solution.o
day4/solution.o \
day5/sample1.txt.o \
day5/input.txt.o \
day5/solution.o