Compare commits
7 Commits
bd3ea98f7b
...
809b25b1ac
Author | SHA1 | Date | |
---|---|---|---|
809b25b1ac | |||
c619806f7b | |||
5cc6c76ac1 | |||
0579cf7c05 | |||
89789e5191 | |||
ceb9db2967 | |||
1612cd1f04 |
41
bin/run
41
bin/run
@ -1,21 +1,45 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
show_help() {
|
||||||
|
echo "usage: $0 --lang=lang --year=year --day=day"
|
||||||
|
}
|
||||||
|
|
||||||
|
language=''
|
||||||
|
year='0'
|
||||||
|
day='0'
|
||||||
|
|
||||||
#
|
#
|
||||||
# check args
|
# 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 ]]
|
shift
|
||||||
then
|
done
|
||||||
echo "usage: $0 year day"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# check year and day
|
# check year and day
|
||||||
#
|
#
|
||||||
|
|
||||||
year=$(($2))
|
|
||||||
|
|
||||||
if [[ $year -lt 2015 ]]
|
if [[ $year -lt 2015 ]]
|
||||||
then
|
then
|
||||||
echo "invalid year: $2"
|
echo "invalid year: $2"
|
||||||
@ -23,8 +47,6 @@ then
|
|||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
day=$(($3))
|
|
||||||
|
|
||||||
if [[ $day -lt 1 ]] || [[ $day -gt 25 ]]
|
if [[ $day -lt 1 ]] || [[ $day -gt 25 ]]
|
||||||
then
|
then
|
||||||
echo "invalid day: $3"
|
echo "invalid day: $3"
|
||||||
@ -55,7 +77,6 @@ then
|
|||||||
curl -s -b "$(cat ../cookie)" $input_url > $input_path
|
curl -s -b "$(cat ../cookie)" $input_url > $input_path
|
||||||
fi
|
fi
|
||||||
|
|
||||||
language=$1
|
|
||||||
valid_languages=""
|
valid_languages=""
|
||||||
valid_language="false"
|
valid_language="false"
|
||||||
|
|
||||||
|
@ -80,8 +80,8 @@ int main() {
|
|||||||
big_score += calculate_score(&m, i, false);
|
big_score += calculate_score(&m, i, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Part 1: %ld\n", score);
|
printf("Part 1: %d\n", score);
|
||||||
printf("Part 2: %ld\n", big_score);
|
printf("Part 2: %d\n", big_score);
|
||||||
|
|
||||||
aoc_free();
|
aoc_free();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -98,7 +98,6 @@ int region_outer_corner_count(Region *r) {
|
|||||||
int outer_corners = 0;
|
int outer_corners = 0;
|
||||||
for (int i = 0; i < r->positions_count; i++) {
|
for (int i = 0; i < r->positions_count; i++) {
|
||||||
int position = r->positions[i];
|
int position = r->positions[i];
|
||||||
int fences = 0;
|
|
||||||
bool top_fence = false, left_fence = false, right_fence = false, bottom_fence = false;
|
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') {
|
if (position - r->map->width < 0 || r->map->data[position - r->map->width] != r->plant - 'A' + 'a') {
|
||||||
top_fence = true;
|
top_fence = true;
|
||||||
@ -211,7 +210,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("Part 1: %d\n", cost);
|
printf("Part 1: %d\n", cost);
|
||||||
printf("Part 2: %lld\n", cost_2);
|
printf("Part 2: %d\n", cost_2);
|
||||||
|
|
||||||
aoc_free();
|
aoc_free();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -37,12 +37,12 @@ Ratio intersect_x(Line l1, Line l2) {
|
|||||||
Ratio left_side = ratio_subtract(l1.slope, l2.slope);
|
Ratio left_side = ratio_subtract(l1.slope, l2.slope);
|
||||||
Ratio right_side = ratio_subtract(l2.offset, l1.offset);
|
Ratio right_side = ratio_subtract(l2.offset, l1.offset);
|
||||||
Ratio divided = ratio_divide(right_side, left_side);
|
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;
|
return divided;
|
||||||
}
|
}
|
||||||
|
|
||||||
long long li_cost(int ax, int ay, int bx, int by, long long px, long long py) {
|
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;
|
Line b_line;
|
||||||
b_line.slope.num = by;
|
b_line.slope.num = by;
|
||||||
b_line.slope.denom = bx;
|
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);
|
Ratio r = intersect_x(b_line, a_line);
|
||||||
|
|
||||||
if (!proper(r)) {
|
if (!proper(r)) {
|
||||||
printf("bzzt\n");
|
// printf("bzzt\n");
|
||||||
return 0;
|
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;
|
long long a_x_movement = px - b_x_movement;
|
||||||
if (a_x_movement % ax != 0) return 0; // Fuuuuuuuuuuuuuuck this
|
if (a_x_movement % ax != 0) return 0; // Fuuuuuuuuuuuuuuck this
|
||||||
long long a_cost = (a_x_movement / ax) * 3;
|
long long a_cost = (a_x_movement / ax) * 3;
|
||||||
printf("b: %lld, a: %lld\n", b_x_movement, a_x_movement);
|
// 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 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;
|
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;
|
return b_cost + a_cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int ax, ay, bx, by;
|
int ax, ay, bx, by;
|
||||||
long long px, py;
|
int px, py;
|
||||||
char *line;
|
char *line;
|
||||||
|
|
||||||
long long cost = 0;
|
long long cost = 0;
|
||||||
@ -90,7 +90,7 @@ int main() {
|
|||||||
|
|
||||||
cost += li_cost(ax, ay, bx, by, px, py);
|
cost += li_cost(ax, ay, bx, by, px, py);
|
||||||
cost_2 += li_cost(ax, ay, bx, by, px + 10000000000000, py + 10000000000000);
|
cost_2 += li_cost(ax, ay, bx, by, px + 10000000000000, py + 10000000000000);
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Part 1: %lld\n", cost);
|
printf("Part 1: %lld\n", cost);
|
||||||
|
@ -70,8 +70,8 @@ int main() {
|
|||||||
// Uncomment this to print the pretty christmas tree
|
// Uncomment this to print the pretty christmas tree
|
||||||
// printf("%s\n", map);
|
// printf("%s\n", map);
|
||||||
|
|
||||||
printf("Part 1: %lld\n", q1 * q2 * q3 * q4);
|
printf("Part 1: %d\n", q1 * q2 * q3 * q4);
|
||||||
printf("Part 2: %lld\n", i);
|
printf("Part 2: %d\n", i);
|
||||||
|
|
||||||
aoc_free();
|
aoc_free();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "../../lib/aoc.h"
|
#include "../../lib/aoc.h"
|
||||||
|
#include "../../lib/hashmap.h"
|
||||||
|
|
||||||
typedef char Towel[10];
|
typedef char Towel[10];
|
||||||
|
|
||||||
@ -25,20 +26,20 @@ bool towels_valid_design(Towels *t, char *design) {
|
|||||||
|
|
||||||
long towels_valid_designs(Towels *t, char *design) {
|
long towels_valid_designs(Towels *t, char *design) {
|
||||||
AocHashmapNode *memo = aoc_hashmap_get(t->map, 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;
|
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++) {
|
for (int i = 0; i < t->towels_count; i++) {
|
||||||
if (strstr(design, t->towels[i]) == design) {
|
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);
|
aoc_hashmap_put(t->map, design, valid_designs);
|
||||||
|
|
||||||
return valid_designs;
|
return *valid_designs;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -47,11 +48,11 @@ int main() {
|
|||||||
Towels towels;
|
Towels towels;
|
||||||
towels.towels_count = 0;
|
towels.towels_count = 0;
|
||||||
towels.map = malloc(sizeof(AocHashmap));
|
towels.map = malloc(sizeof(AocHashmap));
|
||||||
|
|
||||||
for (int i = 0; i < AOC_HASHMAP_RADIX; i++) {
|
for (int i = 0; i < AOC_HASHMAP_RADIX; i++) {
|
||||||
towels.map->buckets[i] = NULL;
|
towels.map->buckets[i] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
line = aoc_read_line();
|
line = aoc_read_line();
|
||||||
char *token = strtok(line, ", ");
|
char *token = strtok(line, ", ");
|
||||||
while (token != NULL) {
|
while (token != NULL) {
|
||||||
@ -71,6 +72,7 @@ int main() {
|
|||||||
printf("Part 1: %d\n", valid_single_designs);
|
printf("Part 1: %d\n", valid_single_designs);
|
||||||
printf("Part 2: %ld\n", valid_multi_designs);
|
printf("Part 2: %ld\n", valid_multi_designs);
|
||||||
|
|
||||||
|
aoc_hashmap_free(towels.map);
|
||||||
aoc_free();
|
aoc_free();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ int main() {
|
|||||||
Grove g;
|
Grove g;
|
||||||
|
|
||||||
char *line, *token;
|
char *line, *token;
|
||||||
char l[4], op[4], r[4], name[4];
|
char name[4];
|
||||||
int value;
|
int value;
|
||||||
|
|
||||||
bool done_with_init = false;
|
bool done_with_init = false;
|
||||||
|
@ -5,11 +5,9 @@
|
|||||||
|
|
||||||
#include "../../lib/aoc.h"
|
#include "../../lib/aoc.h"
|
||||||
|
|
||||||
enum State { M, U, L, OPEN, D1, COMMA, D2, CLOSE };
|
enum State { M, U, L, OPEN, D1, D2 };
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char *line;
|
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
int cond_sum = 0;
|
int cond_sum = 0;
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ int target_string_count(Puzzle *puzzle, int index) {
|
|||||||
char mas[4];
|
char mas[4];
|
||||||
int xmas_count(Puzzle *puzzle, int index) {
|
int xmas_count(Puzzle *puzzle, int index) {
|
||||||
if (puzzle->text[index] != 'A') return 0;
|
if (puzzle->text[index] != 'A') return 0;
|
||||||
int count = 0;
|
|
||||||
int row = index / puzzle->width;
|
int row = index / puzzle->width;
|
||||||
int column = index % puzzle->width;
|
int column = index % puzzle->width;
|
||||||
if (
|
if (
|
||||||
|
@ -67,7 +67,6 @@ int main() {
|
|||||||
int reorder_sum = 0;
|
int reorder_sum = 0;
|
||||||
|
|
||||||
Rules rules;
|
Rules rules;
|
||||||
int rule_index = 0;
|
|
||||||
int first, last;
|
int first, last;
|
||||||
|
|
||||||
Update update;
|
Update update;
|
||||||
|
@ -44,7 +44,7 @@ bool true_permutation_p2(Calibration *c, char *permutation) {
|
|||||||
value *= c->numbers[i];
|
value *= c->numbers[i];
|
||||||
} else {
|
} else {
|
||||||
char concat[100];
|
char concat[100];
|
||||||
sprintf(concat, "%ld%ld", value, c->numbers[i]);
|
sprintf(concat, "%ld%d", value, c->numbers[i]);
|
||||||
value = atol(concat);
|
value = atol(concat);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ void calibration_from_line(Calibration *c, char *line) {
|
|||||||
c->number_count = 0;
|
c->number_count = 0;
|
||||||
char *token = strtok(line, ":");
|
char *token = strtok(line, ":");
|
||||||
c->test_value = atol(token);
|
c->test_value = atol(token);
|
||||||
while(token = strtok(NULL, " ")) {
|
while((token = strtok(NULL, " "))) {
|
||||||
c->numbers[c->number_count++] = atoi(token);
|
c->numbers[c->number_count++] = atoi(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,7 @@ int main() {
|
|||||||
for (int i = 0; i < input_size; i++) {
|
for (int i = 0; i < input_size; i++) {
|
||||||
if (resonant_antinode_grid[i] == '#') resonant_antinode_count++;
|
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(empty_grid);
|
||||||
free(antinode_grid);
|
free(antinode_grid);
|
||||||
|
@ -11,5 +11,5 @@ if [[ -z $source_file ]] ; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
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)
|
time (cat ../data/$year/$day/input.txt | ./$year/$day/problem)
|
||||||
|
73
c/lib/aoc.c
Normal file
73
c/lib/aoc.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
79
c/lib/aoc.h
79
c/lib/aoc.h
@ -1,72 +1,11 @@
|
|||||||
#include <stdlib.h>
|
#ifndef AOC_H
|
||||||
#include <stdio.h>
|
#define AOC_H
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "hashmap.h"
|
char *aoc_read_input(void);
|
||||||
|
char *aoc_read_line(void);
|
||||||
#define INITIAL_BUFFER_SIZE 2
|
void aoc_reset_read_line(void);
|
||||||
|
void aoc_free(void);
|
||||||
char *input_buffer;
|
int aoc_line_count(void);
|
||||||
|
int aoc_sort_int(const void *a, const void *b);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
56
c/lib/hashmap.c
Normal file
56
c/lib/hashmap.c
Normal 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);
|
||||||
|
}
|
@ -1,22 +1,15 @@
|
|||||||
|
#ifndef AOC_HASHMAP
|
||||||
|
#define AOC_HASHMAP
|
||||||
|
|
||||||
#define AOC_HASHMAP_RADIX 1024
|
#define AOC_HASHMAP_RADIX 1024
|
||||||
|
|
||||||
int aoc_hash(const char *string) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct AocHashmapNode AocHashmapNode;
|
typedef struct AocHashmapNode AocHashmapNode;
|
||||||
|
|
||||||
typedef struct AocHashmapNode {
|
typedef struct AocHashmapNode {
|
||||||
char *key;
|
char *key;
|
||||||
long value;
|
void *value;
|
||||||
AocHashmapNode *next;
|
AocHashmapNode *next;
|
||||||
} AocHashmapNode;
|
} AocHashmapNode;
|
||||||
|
|
||||||
@ -24,21 +17,8 @@ typedef struct AocHashmap {
|
|||||||
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
|
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
|
||||||
} AocHashmap;
|
} AocHashmap;
|
||||||
|
|
||||||
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) {
|
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key);
|
||||||
int hash = aoc_hash(key);
|
void aoc_hashmap_put(AocHashmap *map, char *key, void *value);
|
||||||
AocHashmapNode *node = map->buckets[hash];
|
void aoc_hashmap_free(AocHashmap *map);
|
||||||
while (node != NULL && strcmp(node->key, key) != 0) node = node->next;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *aoc_hashmap_put(AocHashmap *map, char *key, long value) {
|
#endif
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
input = File.read("../data/2015/1/input.txt").split("")
|
input = STDIN.read.split("")
|
||||||
counts = input.tally
|
counts = input.tally
|
||||||
final_floor = counts["("] - counts[")"]
|
final_floor = counts["("] - counts[")"]
|
||||||
puts "Part 1: #{final_floor}"
|
puts "Part 1: #{final_floor}"
|
||||||
|
10
ruby/bin/run
10
ruby/bin/run
@ -3,4 +3,12 @@
|
|||||||
year=$1
|
year=$1
|
||||||
day=$2
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user