Compare commits

...

4 commits

Author SHA1 Message Date
46af70bb90
Day 4 complete 2022-12-15 15:35:21 -05:00
594996acb9
Day 3 complete 2022-12-14 01:43:33 -05:00
4f8fa53dbf
Day 2 complete 2022-12-12 21:16:54 -05:00
a8851c4f3d
Day 1 complete 2022-12-12 04:34:29 -05:00
8 changed files with 6407 additions and 0 deletions

45
2022/1.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int comp(const void *a, const void *b) {
int n = *(int *)a;
int m = *(int *)b;
return n - m;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s input_file\n", argv[0]);
return 1;
}
FILE *fd = fopen(argv[1], "r");
if (!fd) {
fprintf(stderr, "Error opening file\n");
return 1;
}
char line[50] = {0};
int idx = 0, elves[1000] = {0};
while (fgets(line, sizeof line / sizeof *line, fd) != NULL) {
if (strlen(line) == 1) {
idx++;
} else {
elves[idx] += atoi(line);
}
}
qsort(elves, idx, sizeof(int), comp);
printf("Elf with the most calories: %d\n", elves[idx - 1]);
printf("Elf with the 2nd most calories: %d\n", elves[idx - 2]);
printf("Elf with the 3rd most calories: %d\n", elves[idx - 3]);
printf("Total: %d\n",
elves[idx - 1] + elves[idx - 2] + elves[idx - 3]);
fclose(fd);
return 0;
}

2250
2022/1.txt Normal file

File diff suppressed because it is too large Load diff

117
2022/2.c Normal file
View file

@ -0,0 +1,117 @@
#include <stdio.h>
#include <string.h>
struct RPS_Game {
// Part 1 Rock / Part 2 Lose
const int X;
// Part 1 Paper / Part 2 Draw
const int Y;
// Part 1 Scissors / Part 2 Win
const int Z;
// Outcomes
const int draw;
const int win;
};
// Part 1 - Calculate score
void process_round(struct RPS_Game *game, int *score, const char opponent_move,
const char my_move) {
switch (my_move) {
case 'X': // Rock
*score += game->X;
if (opponent_move == 'A') {
*score += game->draw;
} else if (opponent_move == 'C') {
*score += game->win;
}
break;
case 'Y': // Paper
*score += game->Y;
if (opponent_move == 'A') {
*score += game->win;
} else if (opponent_move == 'B') {
*score += game->draw;
}
break;
case 'Z': // Scissors
*score += game->Z;
if (opponent_move == 'B') {
*score += game->win;
} else if (opponent_move == 'C') {
*score += game->draw;
}
break;
}
}
// Part 2 - Find move given a desired result, then calculate score
void find_move(struct RPS_Game *game, int *score, const char opponent_move,
const char result) {
char my_move = '\0';
switch (result) {
case 'X': // need to lose
if (opponent_move == 'A') {
my_move = 'Z';
} else if (opponent_move == 'B') {
my_move = 'X';
} else if (opponent_move == 'C') {
my_move = 'Y';
}
break;
case 'Y': // need a draw
if (opponent_move == 'A') {
my_move = 'X';
} else if (opponent_move == 'B') {
my_move = 'Y';
} else if (opponent_move == 'C') {
my_move = 'Z';
}
break;
case 'Z': // need to win
if (opponent_move == 'A') {
my_move = 'Y';
} else if (opponent_move == 'B') {
my_move = 'Z';
} else if (opponent_move == 'C') {
my_move = 'X';
}
break;
}
process_round(game, score, opponent_move, my_move);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s input_file\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
return 1;
}
int part1_score = 0;
int part2_score = 0;
char round[5] = {0};
struct RPS_Game game = {.X = 1, .Y = 2, .Z = 3, .draw = 3, .win = 6};
while (fgets(round, sizeof round / sizeof *round, file) != NULL) {
if (strlen(round) == 1) {
break;
}
process_round(&game, &part1_score, round[0], round[2]);
find_move(&game, &part2_score, round[0], round[2]);
}
fclose(file);
printf("My part 1 score: %d\n", part1_score);
printf("My part 2 score: %d\n", part2_score);
return 0;
}

2500
2022/2.txt Normal file

File diff suppressed because it is too large Load diff

105
2022/3.c Normal file
View file

@ -0,0 +1,105 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Find badges for part 2
char find_badge(char group[3][50]) {
for (int i = 0; i < strlen(group[0]); i++) {
for (int j = 0; j < strlen(group[1]); j++) {
for (int k = 0; k < strlen(group[2]); k++) {
if (group[0][i] == group[1][j] && group[1][j] == group[2][k]) {
return group[0][i];
}
}
}
}
return 0;
}
// Find items for part 1
char find_item(char *items) {
int len = strlen(items);
int mid = len / 2;
for (int i = 0; i < mid; i++) {
for (int j = mid; j < len; j++) {
if (items[i] == items[j]) {
return items[i];
}
}
}
return 0;
}
// Updates item_sum / badge_sum when match is found
void update_sum(const char c, int *sum) {
if (c > 64 && c < 91) {
*sum += c - 38;
} else if (c > 96 && c < 123) {
*sum += c - 96;
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s input_file\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file) {
perror("fopen");
return 1;
}
char items[50] = {0};
char group[3][50] = {0};
int item_sum = 0, badge_sum = 0, idx = 0;
while (fgets(items, sizeof items / sizeof *items, file) != NULL) {
if (strlen(items) == 1) {
break;
}
// Part 1 - find misplaced items
items[strlen(items) - 1] = '\0';
char match = find_item(items);
if (!match) {
fprintf(stderr, "Match not found: %s\n", items);
}
update_sum(match, &item_sum);
// Part 2 - find badges among groups of 3
memcpy(group[idx], items, strlen(items));
idx++;
if (idx == 3) {
idx = 0;
char badge = find_badge(group);
if (!badge) {
fprintf(stderr,
"Badge not found among the following group:\n%s\n%s\n%s\n",
group[0], group[1], group[2]);
}
update_sum(badge, &badge_sum);
for (int i = 0; i < sizeof group / sizeof *group; i++) {
memset(group[i], 0, sizeof group[0] / sizeof *group[0]);
}
}
memset(items, 0, sizeof items / sizeof *items);
}
fclose(file);
printf("Part 1 - Sum of item priorities: %d\n", item_sum);
printf("Part 2 - Sum of badge priorities: %d\n", badge_sum);
return 0;
}

300
2022/3.txt Normal file
View file

@ -0,0 +1,300 @@
DsPhSBQQQhqmBDhPDsFwjwsLjlRjlttvjvvtRb
rNJMNNbrHrtjHLHjvwtg
fNbNzZdrZnMnMPnQShFPDmnqFm
QWVCFfQffgQCVZzVVpHsHJBqtpspJFRHqq
mwDbmnnGNlNcwNDDNRbnND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qlZtBPZq
HTzDwmSvCvCmsmmvTSwvFwcfRRNRLcQRWNssNNbRWLWL
PpMgDMDnsWSnjBQnrjbn
LFcVVGChCFdhdVFZVpVCdVbvQbRrbvBBbBjQSJZrrJrR
FNwGNCCFHcVTHcFdHHHTDzMzfsgzwpPWMmPflmtt

90
2022/4.c Normal file
View file

@ -0,0 +1,90 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Assignment {
char *range;
int start;
int end;
};
// Split each line into 2 strings representing the assignment range for each elf
void parse_assignments(char *assignments, struct Assignment *elf1,
struct Assignment *elf2) {
elf1->range = strtok(assignments, ",");
elf2->range = strtok(NULL, ",");
}
// Split each assignment range into a start and end
void parse_sections(struct Assignment *elf) {
elf->start = atoi(strtok(elf->range, "-,"));
elf->end = atoi(strtok(NULL, "-,"));
}
// Count the number of assignments that fully contain the other
void count_subsets(int *count, struct Assignment *elf1,
struct Assignment *elf2) {
if ((elf1->start >= elf2->start && elf1->end <= elf2->end) ||
(elf2->start >= elf1->start && elf2->end <= elf1->end)) {
*count += 1;
}
}
// Count the number of assignments with any overlapping sections
void count_overlaps(int *count, struct Assignment *elf1,
struct Assignment *elf2) {
if ((elf1->start <= elf2->end && elf1->end >= elf2->start) ||
(elf2->start <= elf1->end && elf2->end >= elf1->start)) {
*count += 1;
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s input_file\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file) {
perror("fopen");
return 1;
}
int part1_count = 0;
int part2_count = 0;
char assignments[16] = {0};
while (fgets(assignments, sizeof assignments / sizeof *assignments, file)) {
if (strlen(assignments) == 1) {
break;
}
struct Assignment elf1;
struct Assignment elf2;
assignments[strlen(assignments) - 1] = '\0';
// split input into starting and ending points
parse_assignments(assignments, &elf1, &elf2);
parse_sections(&elf1);
parse_sections(&elf2);
// Part 1 - count sections that fully are fully contained by another
count_subsets(&part1_count, &elf1, &elf2);
// Part 2 - count all overlapping sections
count_overlaps(&part2_count, &elf1, &elf2);
}
printf("Part 1 - %d ranges fully contain another\n", part1_count);
printf("part 2 - %d assignments overlap\n", part2_count);
fclose(file);
return 0;
}

1000
2022/4.txt Normal file

File diff suppressed because it is too large Load diff