Compare commits

...

7 Commits

Author SHA1 Message Date
809b25b1ac Use more idiomatic clargs 2024-12-27 12:35:07 -05:00
c619806f7b Ruby STDIN conversion 2024-12-27 12:19:28 -05:00
5cc6c76ac1 -Wall fixes 2024-12-27 05:38:45 -05:00
0579cf7c05 Use header files properly 2024-12-27 05:30:39 -05:00
89789e5191 Remove unused variable 2024-12-26 14:18:29 -05:00
ceb9db2967 Plug memory leaks 2024-12-25 12:45:28 -05:00
1612cd1f04 Make AocHashmap more versatile 2024-12-25 11:58:51 -05:00
19 changed files with 215 additions and 140 deletions

41
bin/run
View File

@ -1,21 +1,45 @@
#!/usr/bin/env bash
show_help() {
echo "usage: $0 --lang=lang --year=year --day=day"
}
language=''
year='0'
day='0'
#
# check args
#
while :; do
case $1 in
-h|-\?|--help)
show_help # Display a usage synopsis.
exit
;;
--lang=?*)
language=${1#*=}
;;
--year=?*)
year=${1#*=}
;;
--day=?*)
day=${1#*=}
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*)
break
esac
if [[ $# -ne 3 ]]
then
echo "usage: $0 year day"
exit 1
fi
shift
done
#
# check year and day
#
year=$(($2))
if [[ $year -lt 2015 ]]
then
echo "invalid year: $2"
@ -23,8 +47,6 @@ then
exit 2
fi
day=$(($3))
if [[ $day -lt 1 ]] || [[ $day -gt 25 ]]
then
echo "invalid day: $3"
@ -55,7 +77,6 @@ then
curl -s -b "$(cat ../cookie)" $input_url > $input_path
fi
language=$1
valid_languages=""
valid_language="false"

View File

@ -80,8 +80,8 @@ int main() {
big_score += calculate_score(&m, i, false);
}
printf("Part 1: %ld\n", score);
printf("Part 2: %ld\n", big_score);
printf("Part 1: %d\n", score);
printf("Part 2: %d\n", big_score);
aoc_free();
return 0;

View File

@ -98,7 +98,6 @@ int region_outer_corner_count(Region *r) {
int outer_corners = 0;
for (int i = 0; i < r->positions_count; i++) {
int position = r->positions[i];
int fences = 0;
bool top_fence = false, left_fence = false, right_fence = false, bottom_fence = false;
if (position - r->map->width < 0 || r->map->data[position - r->map->width] != r->plant - 'A' + 'a') {
top_fence = true;
@ -211,7 +210,7 @@ int main() {
}
printf("Part 1: %d\n", cost);
printf("Part 2: %lld\n", cost_2);
printf("Part 2: %d\n", cost_2);
aoc_free();
return 0;

View File

@ -37,12 +37,12 @@ Ratio intersect_x(Line l1, Line l2) {
Ratio left_side = ratio_subtract(l1.slope, l2.slope);
Ratio right_side = ratio_subtract(l2.offset, l1.offset);
Ratio divided = ratio_divide(right_side, left_side);
printf("left: %lld/%lld, right: %lld/%lld, divided: %lld/%lld\n", left_side.num, left_side.denom, right_side.num, right_side.denom, divided.num, divided.denom);
// printf("left: %lld/%lld, right: %lld/%lld, divided: %lld/%lld\n", left_side.num, left_side.denom, right_side.num, right_side.denom, divided.num, divided.denom);
return divided;
}
long long li_cost(int ax, int ay, int bx, int by, long long px, long long py) {
printf("prize at %lld, %lld\n", px, py);
// printf("prize at %lld, %lld\n", px, py);
Line b_line;
b_line.slope.num = by;
b_line.slope.denom = bx;
@ -58,7 +58,7 @@ long long li_cost(int ax, int ay, int bx, int by, long long px, long long py) {
Ratio r = intersect_x(b_line, a_line);
if (!proper(r)) {
printf("bzzt\n");
// printf("bzzt\n");
return 0;
}
@ -68,16 +68,16 @@ long long li_cost(int ax, int ay, int bx, int by, long long px, long long py) {
long long a_x_movement = px - b_x_movement;
if (a_x_movement % ax != 0) return 0; // Fuuuuuuuuuuuuuuck this
long long a_cost = (a_x_movement / ax) * 3;
printf("b: %lld, a: %lld\n", b_x_movement, a_x_movement);
printf("b presses: %lld, a presses: %lld\n", b_x_movement / bx, a_x_movement / ax);
// printf("b: %lld, a: %lld\n", b_x_movement, a_x_movement);
// printf("b presses: %lld, a presses: %lld\n", b_x_movement / bx, a_x_movement / ax);
if (b_x_movement < 0 || a_x_movement < 0) return 0;
printf("cost: %lld\n", b_cost + a_cost);
// printf("cost: %lld\n", b_cost + a_cost);
return b_cost + a_cost;
}
int main() {
int ax, ay, bx, by;
long long px, py;
int px, py;
char *line;
long long cost = 0;
@ -90,7 +90,7 @@ int main() {
cost += li_cost(ax, ay, bx, by, px, py);
cost_2 += li_cost(ax, ay, bx, by, px + 10000000000000, py + 10000000000000);
printf("\n");
// printf("\n");
}
printf("Part 1: %lld\n", cost);

View File

@ -70,8 +70,8 @@ int main() {
// Uncomment this to print the pretty christmas tree
// printf("%s\n", map);
printf("Part 1: %lld\n", q1 * q2 * q3 * q4);
printf("Part 2: %lld\n", i);
printf("Part 1: %d\n", q1 * q2 * q3 * q4);
printf("Part 2: %d\n", i);
aoc_free();
return 0;

View File

@ -4,6 +4,7 @@
#include <stdbool.h>
#include "../../lib/aoc.h"
#include "../../lib/hashmap.h"
typedef char Towel[10];
@ -25,20 +26,20 @@ bool towels_valid_design(Towels *t, char *design) {
long towels_valid_designs(Towels *t, char *design) {
AocHashmapNode *memo = aoc_hashmap_get(t->map, design);
if (memo != NULL) return memo->value;
if (memo != NULL) return *(long *)memo->value;
if (strlen(design) == 0) return 1;
long valid_designs = 0;
long *valid_designs = malloc(sizeof(long));
for (int i = 0; i < t->towels_count; i++) {
if (strstr(design, t->towels[i]) == design) {
valid_designs += towels_valid_designs(t, design + strlen(t->towels[i]));
*valid_designs += towels_valid_designs(t, design + strlen(t->towels[i]));
}
}
aoc_hashmap_put(t->map, design, valid_designs);
return valid_designs;
return *valid_designs;
}
int main() {
@ -47,11 +48,11 @@ int main() {
Towels towels;
towels.towels_count = 0;
towels.map = malloc(sizeof(AocHashmap));
for (int i = 0; i < AOC_HASHMAP_RADIX; i++) {
towels.map->buckets[i] = NULL;
}
line = aoc_read_line();
char *token = strtok(line, ", ");
while (token != NULL) {
@ -71,6 +72,7 @@ int main() {
printf("Part 1: %d\n", valid_single_designs);
printf("Part 2: %ld\n", valid_multi_designs);
aoc_hashmap_free(towels.map);
aoc_free();
exit(0);
}

View File

@ -63,7 +63,7 @@ int main() {
Grove g;
char *line, *token;
char l[4], op[4], r[4], name[4];
char name[4];
int value;
bool done_with_init = false;

View File

@ -5,11 +5,9 @@
#include "../../lib/aoc.h"
enum State { M, U, L, OPEN, D1, COMMA, D2, CLOSE };
enum State { M, U, L, OPEN, D1, D2 };
int main() {
char *line;
int sum = 0;
int cond_sum = 0;

View File

@ -90,7 +90,7 @@ int target_string_count(Puzzle *puzzle, int index) {
char mas[4];
int xmas_count(Puzzle *puzzle, int index) {
if (puzzle->text[index] != 'A') return 0;
int count = 0;
int row = index / puzzle->width;
int column = index % puzzle->width;
if (

View File

@ -67,7 +67,6 @@ int main() {
int reorder_sum = 0;
Rules rules;
int rule_index = 0;
int first, last;
Update update;

View File

@ -44,7 +44,7 @@ bool true_permutation_p2(Calibration *c, char *permutation) {
value *= c->numbers[i];
} else {
char concat[100];
sprintf(concat, "%ld%ld", value, c->numbers[i]);
sprintf(concat, "%ld%d", value, c->numbers[i]);
value = atol(concat);
}
}
@ -76,7 +76,7 @@ void calibration_from_line(Calibration *c, char *line) {
c->number_count = 0;
char *token = strtok(line, ":");
c->test_value = atol(token);
while(token = strtok(NULL, " ")) {
while((token = strtok(NULL, " "))) {
c->numbers[c->number_count++] = atoi(token);
}
}

View File

@ -155,7 +155,7 @@ int main() {
for (int i = 0; i < input_size; i++) {
if (resonant_antinode_grid[i] == '#') resonant_antinode_count++;
}
printf("Part 2: %lld\n", resonant_antinode_count);
printf("Part 2: %d\n", resonant_antinode_count);
free(empty_grid);
free(antinode_grid);

View File

@ -11,5 +11,5 @@ if [[ -z $source_file ]] ; then
exit 1
fi
gcc -o $year/$day/problem $source_file -lm &&
gcc -Wall -o $year/$day/problem $source_file lib/aoc.c -lm &&
time (cat ../data/$year/$day/input.txt | ./$year/$day/problem)

73
c/lib/aoc.c Normal file
View File

@ -0,0 +1,73 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "./hashmap.c"
#define INITIAL_BUFFER_SIZE 2048
char *input_buffer;
char *aoc_read_input(void) {
int buffer_size = INITIAL_BUFFER_SIZE;
input_buffer = malloc(buffer_size);
int chars_read = read(STDIN_FILENO, input_buffer, buffer_size);
char *buffer_position = &input_buffer[chars_read];
while (chars_read == buffer_size) {
buffer_size *= 2;
input_buffer = realloc(input_buffer, buffer_size);
buffer_position = &input_buffer[chars_read];
chars_read += read(STDIN_FILENO, buffer_position, buffer_size / 2);
}
input_buffer[chars_read] = 0;
return input_buffer;
}
char *read_line_buffer = NULL;
char *seek = NULL;
char *aoc_read_line(void) {
if (input_buffer == NULL) aoc_read_input();
if (read_line_buffer == NULL) {
read_line_buffer = malloc(strlen(input_buffer) + 1);
strcpy(read_line_buffer, input_buffer);
}
if (seek == NULL) seek = read_line_buffer;
char *line_break = strstr(seek, "\n");
if (line_break == NULL) return NULL;
*line_break = '\0';
char *token = seek;
seek = line_break + 1;
return token;
}
void aoc_reset_read_line(void) {
strcpy(read_line_buffer, input_buffer);
seek = NULL;
}
void aoc_free(void) {
free(input_buffer);
free(read_line_buffer);
}
int aoc_line_count(void) {
if (input_buffer == NULL) aoc_read_input();
int count = 0;
for (int i = 0; i < strlen(input_buffer); i++) {
if (input_buffer[i] == '\n') count++;
}
return count;
}
int aoc_sort_int(const void *a, const void *b) {
return *(int*) a - *(int*) b;
}

View File

@ -1,72 +1,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#ifndef AOC_H
#define AOC_H
#include "hashmap.h"
#define INITIAL_BUFFER_SIZE 2
char *input_buffer;
char *aoc_read_input(void) {
int buffer_size = INITIAL_BUFFER_SIZE;
input_buffer = malloc(buffer_size);
int chars_read = read(STDIN_FILENO, input_buffer, buffer_size);
char *buffer_position = &input_buffer[chars_read];
while (chars_read == buffer_size) {
buffer_size *= 2;
input_buffer = realloc(input_buffer, buffer_size);
buffer_position = &input_buffer[chars_read];
chars_read += read(STDIN_FILENO, buffer_position, buffer_size / 2);
}
input_buffer[chars_read] = 0;
return input_buffer;
}
char *read_line_buffer = NULL;
char *seek = NULL;
char *aoc_read_line(void) {
if (input_buffer == NULL) aoc_read_input();
if (read_line_buffer == NULL) {
read_line_buffer = malloc(strlen(input_buffer) + 1);
strcpy(read_line_buffer, input_buffer);
}
if (seek == NULL) seek = read_line_buffer;
char *line_break = strstr(seek, "\n");
if (line_break == NULL) return NULL;
*line_break = '\0';
char *token = seek;
seek = line_break + 1;
return token;
}
void aoc_reset_read_line(void) {
strcpy(read_line_buffer, input_buffer);
seek = NULL;
}
void aoc_free(void) {
free(input_buffer);
free(read_line_buffer);
}
int aoc_line_count(void) {
if (input_buffer == NULL) aoc_read_input();
int count = 0;
for (int i = 0; i < strlen(input_buffer); i++) {
if (input_buffer[i] == '\n') count++;
}
return count;
}
int aoc_sort_int(const void *a, const void *b) {
return *(int*) a - *(int*) b;
}
char *aoc_read_input(void);
char *aoc_read_line(void);
void aoc_reset_read_line(void);
void aoc_free(void);
int aoc_line_count(void);
int aoc_sort_int(const void *a, const void *b);
#endif

56
c/lib/hashmap.c Normal file
View File

@ -0,0 +1,56 @@
#include "./hashmap.h"
int aoc_hash(const char *string) {
int len = strlen(string);
int hash = 1;
for (int i = 0; i < len; i++) {
hash *= 7;
hash += string[i] * 13;
hash %= AOC_HASHMAP_RADIX;
}
return hash;
}
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) {
int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash];
while (node != NULL && strcmp(node->key, key) != 0) node = node->next;
return node;
}
void aoc_hashmap_put(AocHashmap *map, char *key, void *value) {
int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash];
if (node == NULL) {
map->buckets[hash] = malloc(sizeof(AocHashmapNode));
node = map->buckets[hash];
node->key = malloc(strlen(key) + 1);
strcpy(node->key, key);
node->value = value;
node->next = NULL;
} else {
while (node->next != NULL) {
node = node->next;
}
node->next = malloc(sizeof(AocHashmapNode));
node->next->key = malloc(strlen(key) + 1);
strcpy(node->next->key, key);
node->next->value = value;
node->next->next = NULL;
}
}
void aoc_hashmap_free(AocHashmap *map) {
for (int i = 0; i < AOC_HASHMAP_RADIX; i++) {
AocHashmapNode *node = map->buckets[i];
while (node != NULL) {
AocHashmapNode *next = node->next;
free(node->key);
free(node->value);
free(node);
node = next;
}
}
free(map);
}

View File

@ -1,22 +1,15 @@
#ifndef AOC_HASHMAP
#define AOC_HASHMAP
#define AOC_HASHMAP_RADIX 1024
int aoc_hash(const char *string) {
int len = strlen(string);
int hash = 1;
for (int i = 0; i < len; i++) {
hash *= 7;
hash += string[i] * 13;
hash %= AOC_HASHMAP_RADIX;
}
return hash;
}
int aoc_hash(const char *string);
typedef struct AocHashmapNode AocHashmapNode;
typedef struct AocHashmapNode {
char *key;
long value;
void *value;
AocHashmapNode *next;
} AocHashmapNode;
@ -24,21 +17,8 @@ typedef struct AocHashmap {
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
} AocHashmap;
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) {
int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash];
while (node != NULL && strcmp(node->key, key) != 0) node = node->next;
return node;
}
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key);
void aoc_hashmap_put(AocHashmap *map, char *key, void *value);
void aoc_hashmap_free(AocHashmap *map);
void *aoc_hashmap_put(AocHashmap *map, char *key, long value) {
int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash];
while (node != NULL) node = node->next;
map->buckets[hash] = malloc(sizeof(AocHashmapNode));
node = map->buckets[hash];
node->key = malloc(strlen(key) + 1);
strcpy(node->key, key);
node->value = value;
node->next = NULL;
}
#endif

View File

@ -1,4 +1,4 @@
input = File.read("../data/2015/1/input.txt").split("")
input = STDIN.read.split("")
counts = input.tally
final_floor = counts["("] - counts[")"]
puts "Part 1: #{final_floor}"

View File

@ -3,4 +3,12 @@
year=$1
day=$2
time ruby $year/$day/problem.rb
mkdir -p $year/$day
source_file=$(ls $year/$day/*.rb)
if [[ -z $source_file ]] ; then
echo "No ruby source file found in $year/$day"
exit 1
fi
time (cat ../data/$year/$day/input.txt | ruby $source_file)