Compare commits

..

No commits in common. "809b25b1ac5a9e7b236b207d8d0e04579c2b055e" and "bd3ea98f7b772b28db6e12badb4c3876cd036f2b" have entirely different histories.

19 changed files with 140 additions and 215 deletions

41
bin/run
View File

@ -1,45 +1,21 @@
#!/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
shift
done
if [[ $# -ne 3 ]]
then
echo "usage: $0 year day"
exit 1
fi
#
# check year and day
#
year=$(($2))
if [[ $year -lt 2015 ]]
then
echo "invalid year: $2"
@ -47,6 +23,8 @@ then
exit 2
fi
day=$(($3))
if [[ $day -lt 1 ]] || [[ $day -gt 25 ]]
then
echo "invalid day: $3"
@ -77,6 +55,7 @@ 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: %d\n", score);
printf("Part 2: %d\n", big_score);
printf("Part 1: %ld\n", score);
printf("Part 2: %ld\n", big_score);
aoc_free();
return 0;

View File

@ -98,6 +98,7 @@ 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;
@ -210,7 +211,7 @@ int main() {
}
printf("Part 1: %d\n", cost);
printf("Part 2: %d\n", cost_2);
printf("Part 2: %lld\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;
int px, py;
long long 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: %d\n", q1 * q2 * q3 * q4);
printf("Part 2: %d\n", i);
printf("Part 1: %lld\n", q1 * q2 * q3 * q4);
printf("Part 2: %lld\n", i);
aoc_free();
return 0;

View File

@ -4,7 +4,6 @@
#include <stdbool.h>
#include "../../lib/aoc.h"
#include "../../lib/hashmap.h"
typedef char Towel[10];
@ -26,20 +25,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 *(long *)memo->value;
if (memo != NULL) return memo->value;
if (strlen(design) == 0) return 1;
long *valid_designs = malloc(sizeof(long));
long valid_designs = 0;
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() {
@ -48,11 +47,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) {
@ -72,7 +71,6 @@ 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 name[4];
char l[4], op[4], r[4], name[4];
int value;
bool done_with_init = false;

View File

@ -5,9 +5,11 @@
#include "../../lib/aoc.h"
enum State { M, U, L, OPEN, D1, D2 };
enum State { M, U, L, OPEN, D1, COMMA, D2, CLOSE };
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,6 +67,7 @@ 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%d", value, c->numbers[i]);
sprintf(concat, "%ld%ld", 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: %d\n", resonant_antinode_count);
printf("Part 2: %lld\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 -Wall -o $year/$day/problem $source_file lib/aoc.c -lm &&
gcc -o $year/$day/problem $source_file -lm &&
time (cat ../data/$year/$day/input.txt | ./$year/$day/problem)

View File

@ -1,73 +0,0 @@
#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,11 +1,72 @@
#ifndef AOC_H
#define AOC_H
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
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);
#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;
}
#endif

View File

@ -1,56 +0,0 @@
#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,15 +1,22 @@
#ifndef AOC_HASHMAP
#define AOC_HASHMAP
#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 {
char *key;
void *value;
long value;
AocHashmapNode *next;
} AocHashmapNode;
@ -17,8 +24,21 @@ typedef struct AocHashmap {
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
} AocHashmap;
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key);
void aoc_hashmap_put(AocHashmap *map, char *key, void *value);
void aoc_hashmap_free(AocHashmap *map);
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;
}
#endif
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;
}

View File

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

View File

@ -3,12 +3,4 @@
year=$1
day=$2
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)
time ruby $year/$day/problem.rb