generated from blizzardfinnegan/advent-of-code
Complete day3 part2
This was easier to test by having several different executables rather than a single monolith
This commit is contained in:
parent
ee20801ce9
commit
ae0e56923f
6 changed files with 581 additions and 2 deletions
|
@ -10,5 +10,11 @@ PROJECT(day03 C)
|
|||
|
||||
#Part 1 Executable
|
||||
ADD_EXECUTABLE(Part1 src/part1.c)
|
||||
#Part 2 Executable
|
||||
#ADD_EXECUTABLE(Part2 src/part2.c)
|
||||
#Part 2a Executable
|
||||
ADD_EXECUTABLE(Part2a src/part2a.c)
|
||||
#Part 2b Executable
|
||||
ADD_EXECUTABLE(Part2b src/part2b.c)
|
||||
#Part 2c Executable
|
||||
ADD_EXECUTABLE(Part2c src/part2c.c)
|
||||
#Part 2d Executable
|
||||
ADD_EXECUTABLE(Part2d src/part2d.c)
|
||||
|
|
19
day03/part2Instructions.txt
Normal file
19
day03/part2Instructions.txt
Normal file
|
@ -0,0 +1,19 @@
|
|||
--- Part Two ---
|
||||
|
||||
Time to check the rest of the slopes - you need to minimize the
|
||||
probability of a sudden arboreal stop, after all.
|
||||
|
||||
Determine the number of trees you would encounter if, for each of the
|
||||
following slopes, you start at the top-left corner and traverse the map
|
||||
all the way to the bottom:
|
||||
* Right 1, down 1.
|
||||
* Right 3, down 1. (This is the slope you already checked.)
|
||||
* Right 5, down 1.
|
||||
* Right 7, down 1.
|
||||
* Right 1, down 2.
|
||||
|
||||
In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s)
|
||||
respectively; multiplied together, these produce the answer 336.
|
||||
|
||||
What do you get if you multiply together the number of trees
|
||||
encountered on each of the listed slopes?
|
139
day03/src/part2a.c
Normal file
139
day03/src/part2a.c
Normal file
|
@ -0,0 +1,139 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TREE '#'
|
||||
#define NO_TREE '.'
|
||||
|
||||
|
||||
const int ITERATE_HORIZ = 1;
|
||||
const int ITERATE_VERT = 1;
|
||||
|
||||
/* Array size for quick-resize */
|
||||
static int array_size = 10;
|
||||
static int ARRAY_ROW_COUNT = 0;
|
||||
static int ARRAY_COL_COUNT = 0;
|
||||
|
||||
char* create_array(){
|
||||
/* Generate expandable array */
|
||||
char* array_pointer = (char*)calloc(array_size, sizeof(char));
|
||||
|
||||
/* If array not allocated, fail safely */
|
||||
if (array_pointer == NULL){
|
||||
printf("Dynamic array allocation failed.\n");
|
||||
exit(1);
|
||||
}
|
||||
return array_pointer;
|
||||
}
|
||||
|
||||
char* append_to_array(char* array, char value_to_append, int array_index){
|
||||
char* internal_array = array;
|
||||
/* resize the array if necessary to continue adding values */
|
||||
if (array_index + 2 == array_size){
|
||||
array_size = array_size * 2;
|
||||
internal_array = realloc(array, array_size * sizeof(char));
|
||||
/* Exit prematurely if reallocation fails */
|
||||
if(NULL == array){
|
||||
printf("Reallocation failed! Exiting.\n");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
if(value_to_append == TREE || value_to_append == NO_TREE){
|
||||
array[array_index] = value_to_append;
|
||||
}
|
||||
return internal_array;
|
||||
}
|
||||
|
||||
/* Convert file into C structs for easier parsing */
|
||||
char* file_import(char * filename, char* array){
|
||||
/* pointer to file */
|
||||
FILE * file_pointer;
|
||||
char intermediate_string[40];
|
||||
char second_string[40];
|
||||
char * line_get = second_string;
|
||||
|
||||
/* Open file based on filename variable */
|
||||
file_pointer = fopen(filename, "r");
|
||||
|
||||
/* Fail safely if invalid file name */
|
||||
if (file_pointer == NULL) {
|
||||
printf("no such file.");
|
||||
exit(2);
|
||||
} else {
|
||||
printf("File successfully loaded.\n");
|
||||
}
|
||||
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_COL_COUNT = strlen(line_get)-1;
|
||||
/* Read until the end of the file */
|
||||
while( !(feof(file_pointer)) ){
|
||||
int index;
|
||||
/* Append each individual character to the correct index
|
||||
* in the array */
|
||||
for(index = 0; index < ARRAY_COL_COUNT; ++index){
|
||||
array = append_to_array(array, intermediate_string[index],(index+(ARRAY_ROW_COUNT * ARRAY_COL_COUNT)));
|
||||
}
|
||||
|
||||
/* Loop back */
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_ROW_COUNT++;
|
||||
}
|
||||
|
||||
/* Close file when complete */
|
||||
fclose(file_pointer);
|
||||
return array;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
/* Final return value */
|
||||
int tree_count = 0;
|
||||
/* Array index value */
|
||||
int array_index = 0;
|
||||
/* Indexes for array */
|
||||
int x_index = ITERATE_HORIZ;
|
||||
int y_index = ITERATE_VERT;
|
||||
/* File name importing data from */
|
||||
char filename_array[30];
|
||||
char* filename = filename_array;
|
||||
|
||||
char* array_pointer = create_array();
|
||||
|
||||
/* Check argument values, look for filename as first argument
|
||||
* Fallback to test input if argument not provided */
|
||||
if(argc >= 2){
|
||||
filename = argv[1];
|
||||
} else {
|
||||
filename = "../data/testInput.txt";
|
||||
}
|
||||
|
||||
/* Behaviour */
|
||||
/* 1. read line from string; save length of string to variable for
|
||||
* later use.
|
||||
* 2. Append characters to string; allowlist . and # chars only.
|
||||
* increment counter (for total line length).
|
||||
* 3. Starting at the beginning of the array, until end of array:
|
||||
* 1. increment tree count (if exists)
|
||||
* 2. retrieve character from position (x,y), where
|
||||
* x=(x+3) mod (line length)
|
||||
* y = y + 1
|
||||
* Calc x and y for next line. Pointer location is:
|
||||
* (x+(y*line length))
|
||||
* 3. if y < line count, continue loop
|
||||
* 4. print tree count
|
||||
* */
|
||||
array_pointer = file_import(filename,array_pointer);
|
||||
|
||||
printf("column count: %d, row count %d\n",ARRAY_COL_COUNT,ARRAY_ROW_COUNT);
|
||||
|
||||
for(; y_index < ARRAY_ROW_COUNT; ++y_index){
|
||||
char next_char = array_pointer[x_index % ARRAY_COL_COUNT + y_index * ARRAY_COL_COUNT];
|
||||
if(next_char == TREE){
|
||||
tree_count++;
|
||||
}
|
||||
x_index += ITERATE_HORIZ;
|
||||
}
|
||||
|
||||
/* Print final calculation */
|
||||
printf("Final value: %d\n",tree_count);
|
||||
exit(0);
|
||||
}
|
139
day03/src/part2b.c
Normal file
139
day03/src/part2b.c
Normal file
|
@ -0,0 +1,139 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TREE '#'
|
||||
#define NO_TREE '.'
|
||||
|
||||
|
||||
const int ITERATE_HORIZ = 5;
|
||||
const int ITERATE_VERT = 1;
|
||||
|
||||
/* Array size for quick-resize */
|
||||
static int array_size = 10;
|
||||
static int ARRAY_ROW_COUNT = 0;
|
||||
static int ARRAY_COL_COUNT = 0;
|
||||
|
||||
char* create_array(){
|
||||
/* Generate expandable array */
|
||||
char* array_pointer = (char*)calloc(array_size, sizeof(char));
|
||||
|
||||
/* If array not allocated, fail safely */
|
||||
if (array_pointer == NULL){
|
||||
printf("Dynamic array allocation failed.\n");
|
||||
exit(1);
|
||||
}
|
||||
return array_pointer;
|
||||
}
|
||||
|
||||
char* append_to_array(char* array, char value_to_append, int array_index){
|
||||
char* internal_array = array;
|
||||
/* resize the array if necessary to continue adding values */
|
||||
if (array_index + 2 == array_size){
|
||||
array_size = array_size * 2;
|
||||
internal_array = realloc(array, array_size * sizeof(char));
|
||||
/* Exit prematurely if reallocation fails */
|
||||
if(NULL == array){
|
||||
printf("Reallocation failed! Exiting.\n");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
if(value_to_append == TREE || value_to_append == NO_TREE){
|
||||
array[array_index] = value_to_append;
|
||||
}
|
||||
return internal_array;
|
||||
}
|
||||
|
||||
/* Convert file into C structs for easier parsing */
|
||||
char* file_import(char * filename, char* array){
|
||||
/* pointer to file */
|
||||
FILE * file_pointer;
|
||||
char intermediate_string[40];
|
||||
char second_string[40];
|
||||
char * line_get = second_string;
|
||||
|
||||
/* Open file based on filename variable */
|
||||
file_pointer = fopen(filename, "r");
|
||||
|
||||
/* Fail safely if invalid file name */
|
||||
if (file_pointer == NULL) {
|
||||
printf("no such file.");
|
||||
exit(2);
|
||||
} else {
|
||||
printf("File successfully loaded.\n");
|
||||
}
|
||||
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_COL_COUNT = strlen(line_get)-1;
|
||||
/* Read until the end of the file */
|
||||
while( !(feof(file_pointer)) ){
|
||||
int index;
|
||||
/* Append each individual character to the correct index
|
||||
* in the array */
|
||||
for(index = 0; index < ARRAY_COL_COUNT; ++index){
|
||||
array = append_to_array(array, intermediate_string[index],(index+(ARRAY_ROW_COUNT * ARRAY_COL_COUNT)));
|
||||
}
|
||||
|
||||
/* Loop back */
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_ROW_COUNT++;
|
||||
}
|
||||
|
||||
/* Close file when complete */
|
||||
fclose(file_pointer);
|
||||
return array;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
/* Final return value */
|
||||
int tree_count = 0;
|
||||
/* Array index value */
|
||||
int array_index = 0;
|
||||
/* Indexes for array */
|
||||
int x_index = ITERATE_HORIZ;
|
||||
int y_index = ITERATE_VERT;
|
||||
/* File name importing data from */
|
||||
char filename_array[30];
|
||||
char* filename = filename_array;
|
||||
|
||||
char* array_pointer = create_array();
|
||||
|
||||
/* Check argument values, look for filename as first argument
|
||||
* Fallback to test input if argument not provided */
|
||||
if(argc >= 2){
|
||||
filename = argv[1];
|
||||
} else {
|
||||
filename = "../data/testInput.txt";
|
||||
}
|
||||
|
||||
/* Behaviour */
|
||||
/* 1. read line from string; save length of string to variable for
|
||||
* later use.
|
||||
* 2. Append characters to string; allowlist . and # chars only.
|
||||
* increment counter (for total line length).
|
||||
* 3. Starting at the beginning of the array, until end of array:
|
||||
* 1. increment tree count (if exists)
|
||||
* 2. retrieve character from position (x,y), where
|
||||
* x=(x+3) mod (line length)
|
||||
* y = y + 1
|
||||
* Calc x and y for next line. Pointer location is:
|
||||
* (x+(y*line length))
|
||||
* 3. if y < line count, continue loop
|
||||
* 4. print tree count
|
||||
* */
|
||||
array_pointer = file_import(filename,array_pointer);
|
||||
|
||||
printf("column count: %d, row count %d\n",ARRAY_COL_COUNT,ARRAY_ROW_COUNT);
|
||||
|
||||
for(; y_index < ARRAY_ROW_COUNT; ++y_index){
|
||||
char next_char = array_pointer[x_index % ARRAY_COL_COUNT + y_index * ARRAY_COL_COUNT];
|
||||
if(next_char == TREE){
|
||||
tree_count++;
|
||||
}
|
||||
x_index += ITERATE_HORIZ;
|
||||
}
|
||||
|
||||
/* Print final calculation */
|
||||
printf("Final value: %d\n",tree_count);
|
||||
exit(0);
|
||||
}
|
139
day03/src/part2c.c
Normal file
139
day03/src/part2c.c
Normal file
|
@ -0,0 +1,139 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TREE '#'
|
||||
#define NO_TREE '.'
|
||||
|
||||
|
||||
const int ITERATE_HORIZ = 7;
|
||||
const int ITERATE_VERT = 1;
|
||||
|
||||
/* Array size for quick-resize */
|
||||
static int array_size = 10;
|
||||
static int ARRAY_ROW_COUNT = 0;
|
||||
static int ARRAY_COL_COUNT = 0;
|
||||
|
||||
char* create_array(){
|
||||
/* Generate expandable array */
|
||||
char* array_pointer = (char*)calloc(array_size, sizeof(char));
|
||||
|
||||
/* If array not allocated, fail safely */
|
||||
if (array_pointer == NULL){
|
||||
printf("Dynamic array allocation failed.\n");
|
||||
exit(1);
|
||||
}
|
||||
return array_pointer;
|
||||
}
|
||||
|
||||
char* append_to_array(char* array, char value_to_append, int array_index){
|
||||
char* internal_array = array;
|
||||
/* resize the array if necessary to continue adding values */
|
||||
if (array_index + 2 == array_size){
|
||||
array_size = array_size * 2;
|
||||
internal_array = realloc(array, array_size * sizeof(char));
|
||||
/* Exit prematurely if reallocation fails */
|
||||
if(NULL == array){
|
||||
printf("Reallocation failed! Exiting.\n");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
if(value_to_append == TREE || value_to_append == NO_TREE){
|
||||
array[array_index] = value_to_append;
|
||||
}
|
||||
return internal_array;
|
||||
}
|
||||
|
||||
/* Convert file into C structs for easier parsing */
|
||||
char* file_import(char * filename, char* array){
|
||||
/* pointer to file */
|
||||
FILE * file_pointer;
|
||||
char intermediate_string[40];
|
||||
char second_string[40];
|
||||
char * line_get = second_string;
|
||||
|
||||
/* Open file based on filename variable */
|
||||
file_pointer = fopen(filename, "r");
|
||||
|
||||
/* Fail safely if invalid file name */
|
||||
if (file_pointer == NULL) {
|
||||
printf("no such file.");
|
||||
exit(2);
|
||||
} else {
|
||||
printf("File successfully loaded.\n");
|
||||
}
|
||||
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_COL_COUNT = strlen(line_get)-1;
|
||||
/* Read until the end of the file */
|
||||
while( !(feof(file_pointer)) ){
|
||||
int index;
|
||||
/* Append each individual character to the correct index
|
||||
* in the array */
|
||||
for(index = 0; index < ARRAY_COL_COUNT; ++index){
|
||||
array = append_to_array(array, intermediate_string[index],(index+(ARRAY_ROW_COUNT * ARRAY_COL_COUNT)));
|
||||
}
|
||||
|
||||
/* Loop back */
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_ROW_COUNT++;
|
||||
}
|
||||
|
||||
/* Close file when complete */
|
||||
fclose(file_pointer);
|
||||
return array;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
/* Final return value */
|
||||
int tree_count = 0;
|
||||
/* Array index value */
|
||||
int array_index = 0;
|
||||
/* Indexes for array */
|
||||
int x_index = ITERATE_HORIZ;
|
||||
int y_index = ITERATE_VERT;
|
||||
/* File name importing data from */
|
||||
char filename_array[30];
|
||||
char* filename = filename_array;
|
||||
|
||||
char* array_pointer = create_array();
|
||||
|
||||
/* Check argument values, look for filename as first argument
|
||||
* Fallback to test input if argument not provided */
|
||||
if(argc >= 2){
|
||||
filename = argv[1];
|
||||
} else {
|
||||
filename = "../data/testInput.txt";
|
||||
}
|
||||
|
||||
/* Behaviour */
|
||||
/* 1. read line from string; save length of string to variable for
|
||||
* later use.
|
||||
* 2. Append characters to string; allowlist . and # chars only.
|
||||
* increment counter (for total line length).
|
||||
* 3. Starting at the beginning of the array, until end of array:
|
||||
* 1. increment tree count (if exists)
|
||||
* 2. retrieve character from position (x,y), where
|
||||
* x=(x+3) mod (line length)
|
||||
* y = y + 1
|
||||
* Calc x and y for next line. Pointer location is:
|
||||
* (x+(y*line length))
|
||||
* 3. if y < line count, continue loop
|
||||
* 4. print tree count
|
||||
* */
|
||||
array_pointer = file_import(filename,array_pointer);
|
||||
|
||||
printf("column count: %d, row count %d\n",ARRAY_COL_COUNT,ARRAY_ROW_COUNT);
|
||||
|
||||
for(; y_index < ARRAY_ROW_COUNT; ++y_index){
|
||||
char next_char = array_pointer[x_index % ARRAY_COL_COUNT + y_index * ARRAY_COL_COUNT];
|
||||
if(next_char == TREE){
|
||||
tree_count++;
|
||||
}
|
||||
x_index += ITERATE_HORIZ;
|
||||
}
|
||||
|
||||
/* Print final calculation */
|
||||
printf("Final value: %d\n",tree_count);
|
||||
exit(0);
|
||||
}
|
137
day03/src/part2d.c
Normal file
137
day03/src/part2d.c
Normal file
|
@ -0,0 +1,137 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TREE '#'
|
||||
#define NO_TREE '.'
|
||||
|
||||
|
||||
const int ITERATE_HORIZ = 1;
|
||||
const int ITERATE_VERT = 2;
|
||||
|
||||
/* Array size for quick-resize */
|
||||
static int array_size = 10;
|
||||
static int ARRAY_ROW_COUNT = 0;
|
||||
static int ARRAY_COL_COUNT = 0;
|
||||
|
||||
char* create_array(){
|
||||
/* Generate expandable array */
|
||||
char* array_pointer = (char*)calloc(array_size, sizeof(char));
|
||||
|
||||
/* If array not allocated, fail safely */
|
||||
if (array_pointer == NULL){
|
||||
printf("Dynamic array allocation failed.\n");
|
||||
exit(1);
|
||||
}
|
||||
return array_pointer;
|
||||
}
|
||||
|
||||
char* append_to_array(char* array, char value_to_append, int array_index){
|
||||
char* internal_array = array;
|
||||
/* resize the array if necessary to continue adding values */
|
||||
if (array_index + 2 == array_size){
|
||||
array_size = array_size * 2;
|
||||
internal_array = realloc(array, array_size * sizeof(char));
|
||||
/* Exit prematurely if reallocation fails */
|
||||
if(NULL == array){
|
||||
printf("Reallocation failed! Exiting.\n");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
if(value_to_append == TREE || value_to_append == NO_TREE){
|
||||
array[array_index] = value_to_append;
|
||||
}
|
||||
return internal_array;
|
||||
}
|
||||
|
||||
/* Convert file into C structs for easier parsing */
|
||||
char* file_import(char * filename, char* array){
|
||||
/* pointer to file */
|
||||
FILE * file_pointer;
|
||||
char intermediate_string[40];
|
||||
char second_string[40];
|
||||
char * line_get = second_string;
|
||||
|
||||
/* Open file based on filename variable */
|
||||
file_pointer = fopen(filename, "r");
|
||||
|
||||
/* Fail safely if invalid file name */
|
||||
if (file_pointer == NULL) {
|
||||
printf("no such file.");
|
||||
exit(2);
|
||||
} else {
|
||||
printf("File successfully loaded.\n");
|
||||
}
|
||||
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_COL_COUNT = strlen(line_get)-1;
|
||||
/* Read until the end of the file */
|
||||
while( !(feof(file_pointer)) ){
|
||||
int index;
|
||||
/* Append each individual character to the correct index
|
||||
* in the array */
|
||||
for(index = 0; index < ARRAY_COL_COUNT; ++index){
|
||||
array = append_to_array(array, intermediate_string[index],(index+(ARRAY_ROW_COUNT * ARRAY_COL_COUNT)));
|
||||
}
|
||||
|
||||
/* Loop back */
|
||||
line_get = fgets(intermediate_string, sizeof(intermediate_string), file_pointer);
|
||||
ARRAY_ROW_COUNT++;
|
||||
}
|
||||
|
||||
/* Close file when complete */
|
||||
fclose(file_pointer);
|
||||
return array;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
/* Final return value */
|
||||
int tree_count = 0;
|
||||
/* Array index value */
|
||||
int array_index = 0;
|
||||
/* Indexes for array */
|
||||
int x_index = ITERATE_HORIZ;
|
||||
int y_index = ITERATE_VERT;
|
||||
/* File name importing data from */
|
||||
char filename_array[30];
|
||||
char* filename = filename_array;
|
||||
|
||||
char* array_pointer = create_array();
|
||||
|
||||
/* Check argument values, look for filename as first argument
|
||||
* Fallback to test input if argument not provided */
|
||||
if(argc >= 2){
|
||||
filename = argv[1];
|
||||
} else {
|
||||
filename = "../data/testInput.txt";
|
||||
}
|
||||
|
||||
/* Behaviour */
|
||||
/* 1. read line from string; save length of string to variable for
|
||||
* later use.
|
||||
* 2. Append characters to string; allowlist . and # chars only.
|
||||
* increment counter (for total line length).
|
||||
* 3. Starting at the beginning of the array, until end of array:
|
||||
* 1. increment tree count (if exists)
|
||||
* 2. retrieve character from position (x,y), where
|
||||
* x=(x+3) mod (line length)
|
||||
* y = y + 1
|
||||
* Calc x and y for next line. Pointer location is:
|
||||
* (x+(y*line length))
|
||||
* 3. if y < line count, continue loop
|
||||
* 4. print tree count
|
||||
* */
|
||||
array_pointer = file_import(filename,array_pointer);
|
||||
|
||||
for(; y_index < ARRAY_ROW_COUNT; y_index+=ITERATE_VERT){
|
||||
char next_char = array_pointer[x_index % ARRAY_COL_COUNT + y_index * ARRAY_COL_COUNT];
|
||||
if(next_char == TREE){
|
||||
tree_count++;
|
||||
}
|
||||
x_index += ITERATE_HORIZ;
|
||||
}
|
||||
|
||||
/* Print final calculation */
|
||||
printf("Final value: %d\n",tree_count);
|
||||
exit(0);
|
||||
}
|
Loading…
Add table
Reference in a new issue