solved day 1 and 2
This commit is contained in:
commit
aa5470f8e1
15 changed files with 2354 additions and 0 deletions
BIN
1/day1
Executable file
BIN
1/day1
Executable file
Binary file not shown.
BIN
1/day1B
Executable file
BIN
1/day1B
Executable file
Binary file not shown.
6
1/inputDemo
Normal file
6
1/inputDemo
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
49
1/main.c
Normal file
49
1/main.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LINES 1000
|
||||
|
||||
int compare(const void *a, const void *b);
|
||||
|
||||
int main()
|
||||
{
|
||||
// populate arrays from file
|
||||
FILE *input;
|
||||
input = fopen("./input", "r");
|
||||
unsigned int left[LINES];
|
||||
unsigned int right[LINES];
|
||||
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
fscanf(input, "%d", &left[i]);
|
||||
// printf("%d left: %d ", i, left[i]);
|
||||
fscanf(input, "%d", &right[i]);
|
||||
// printf("%d right: %d\n", i, right[i]);
|
||||
}
|
||||
|
||||
// sort arrays
|
||||
qsort(left, LINES, sizeof(int), compare);
|
||||
qsort(right, LINES, sizeof(int), compare);
|
||||
/*
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
printf("%d left: %d ", i, left[i]);
|
||||
printf("%d right: %d\n", i, right[i]);
|
||||
}
|
||||
*/
|
||||
|
||||
// compare each element n, add its difference to sum-var
|
||||
unsigned long int distance = 0;
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
distance += abs(left[i] - right[i]);
|
||||
}
|
||||
printf("distance: %d\n", distance);
|
||||
}
|
||||
|
||||
int compare(const void *a, const void *b)
|
||||
{
|
||||
int int_a = * ((int*) a);
|
||||
int int_b = * ((int*) b);
|
||||
|
||||
if (int_a == int_b) return 0;
|
||||
else if (int_a < int_b) return -1;
|
||||
else return 1;
|
||||
}
|
51
1/mainB.c
Normal file
51
1/mainB.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LINES 1000
|
||||
|
||||
int compare(const void *a, const void *b);
|
||||
|
||||
int main()
|
||||
{
|
||||
// populate arrays from file
|
||||
FILE *input;
|
||||
input = fopen("./input", "r");
|
||||
unsigned int left[LINES];
|
||||
unsigned int right[LINES];
|
||||
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
fscanf(input, "%d", &left[i]);
|
||||
// printf("%d left: %d ", i, left[i]);
|
||||
fscanf(input, "%d", &right[i]);
|
||||
// printf("%d right: %d\n", i, right[i]);
|
||||
}
|
||||
/*
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
printf("%d left: %d ", i, left[i]);
|
||||
printf("%d right: %d\n", i, right[i]);
|
||||
}
|
||||
*/
|
||||
|
||||
// check how many times left[i] is in right[]. multiply left[i] by this.
|
||||
// then sum it
|
||||
unsigned long int sum = 0;
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
int numRight = 0;
|
||||
for (int j = 0; j < LINES; j++) {
|
||||
numRight += left[i] == right[j];
|
||||
}
|
||||
sum += left[i] * numRight;
|
||||
}
|
||||
|
||||
printf("sum: %d\n", sum);
|
||||
}
|
||||
|
||||
int compare(const void *a, const void *b)
|
||||
{
|
||||
int int_a = * ((int*) a);
|
||||
int int_b = * ((int*) b);
|
||||
|
||||
if (int_a == int_b) return 0;
|
||||
else if (int_a < int_b) return -1;
|
||||
else return 1;
|
||||
}
|
86
2/'
Normal file
86
2/'
Normal file
|
@ -0,0 +1,86 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LINES 1000
|
||||
|
||||
// power! x to the y!
|
||||
int pow(int x, int y)
|
||||
{
|
||||
int ans = 1;
|
||||
for (int i = 0; i < y; i++) ans *= x;
|
||||
return ans;
|
||||
}
|
||||
// parse string into array of numbers
|
||||
int parse_numbers(char *str_row, int *row, int n)
|
||||
{
|
||||
int i = 0;
|
||||
for (; str_row[i] != '\n'; i++);
|
||||
for (; i >= 0; i--) {
|
||||
int pos = 0;
|
||||
for (; str_row[i] >= '0' && str_row[i] <= '9' && i >= 0; i--) {
|
||||
row[n] += (str_row[i] - '0') * pow(10, pos);
|
||||
pos++;
|
||||
}
|
||||
n--;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
// check if specific report is safe
|
||||
int check_report(int *row, int len)
|
||||
{
|
||||
// read number. Compare next number (higher, lower, +-1-2)
|
||||
// if you get to row[i + 1]line, ++
|
||||
int rising = 0;
|
||||
int first = 1;
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
// if difference isnt 1-3
|
||||
if (abs(row[i + 1] - row[i]) > 3 || abs(row[i + 1] - row[i]) < 1)
|
||||
return 0;
|
||||
// only for first comparison of row. Set direction.
|
||||
if (first && ((row[i + 1] - row[i]) < 0))
|
||||
rising = 0;
|
||||
else if (first)
|
||||
rising = 1;
|
||||
// for other comparisons, compare current direction to the previous.
|
||||
if (!first && (rising != !((row[i + 1] - row[i]) < 0))) {
|
||||
// printf("rising: %d while %d - %d is %d\n", rising, row[i + 1], row[i], row[i + 1] - row[i]);
|
||||
return 0;
|
||||
}
|
||||
// get ready for the next comparisons!
|
||||
first = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dampener(int *row, int len)
|
||||
{
|
||||
if (check_report(row, len)) return 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int safe = 0;
|
||||
// read input row into array, count elements
|
||||
FILE *input;
|
||||
input = fopen("./input", "r");
|
||||
char line[32];
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
// get string (line), count numbers, put into int array (row)
|
||||
int numbers = 1;
|
||||
fgets(line, sizeof(line), input);
|
||||
// count number of numbers, allocate memory
|
||||
for (int c = 0; line[c]; c++) numbers += (line[c] == ' ');
|
||||
int *row = (int *)calloc(numbers, sizeof(int));
|
||||
// parse numbers into array
|
||||
parse_numbers(line, row, numbers);
|
||||
// for (int j = 0; j < numbers; j++) printf("%d ", row[j]);
|
||||
// printf("\n\n");
|
||||
|
||||
// send elements and length to check_report, add result to sum
|
||||
safe += check_report(row, numbers);
|
||||
free(row);
|
||||
}
|
||||
|
||||
printf("%d\n", safe);
|
||||
}
|
BIN
2/B
Executable file
BIN
2/B
Executable file
Binary file not shown.
BIN
2/a.out
Executable file
BIN
2/a.out
Executable file
Binary file not shown.
6
2/inputDemo
Normal file
6
2/inputDemo
Normal file
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
BIN
2/main
Executable file
BIN
2/main
Executable file
Binary file not shown.
45
2/main.c
Normal file
45
2/main.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LINES 1000
|
||||
|
||||
int compare(const void *a, const void *b);
|
||||
|
||||
int main()
|
||||
{
|
||||
int new;
|
||||
int old;
|
||||
int safe = 0;
|
||||
FILE *input;
|
||||
input = fopen("./input", "r");
|
||||
// read number. Compare next number (higher, lower, +-1-2)
|
||||
// if you get to newline, ++
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
int rising = 0;
|
||||
int first = 1;
|
||||
fscanf(input, "%d", &old);
|
||||
char nextChar = 0;
|
||||
while ((nextChar = fgetc(input)) && nextChar != '\n') {
|
||||
fscanf(input, "%d", &new);
|
||||
// if difference isnt 1-3
|
||||
if (abs(new - old) > 3 || abs(new - old) < 1) {
|
||||
while(getc(input) != '\n');
|
||||
goto loopEnd;
|
||||
}
|
||||
// only for first comparison of row. Set direction.
|
||||
if (first && ((new - old) < 0)) rising = 0; else if (first) rising = 1;
|
||||
// for other comparisons, compare current direction to the previous.
|
||||
if (!first && (rising != !((new - old) < 0))) {
|
||||
// printf("rising: %d while %d - %d is %d\n", rising, new, old, new - old);
|
||||
while(getc(input) != '\n');
|
||||
goto loopEnd;
|
||||
}
|
||||
// get ready for the next comparisons!
|
||||
first = 0;
|
||||
old = new;
|
||||
}
|
||||
safe++;
|
||||
loopEnd:
|
||||
}
|
||||
printf("%d\n", safe);
|
||||
}
|
110
2/mainB.c
Normal file
110
2/mainB.c
Normal file
|
@ -0,0 +1,110 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define LINES 1000
|
||||
|
||||
// power! x to the y!
|
||||
int pow(int x, int y)
|
||||
{
|
||||
int ans = 1;
|
||||
for (int i = 0; i < y; i++) ans *= x;
|
||||
return ans;
|
||||
}
|
||||
// parse string into array of numbers
|
||||
int parse_numbers(char *str_row, int *row, int n)
|
||||
{
|
||||
int i = 0;
|
||||
for (; str_row[i] != '\n'; i++);
|
||||
for (; i >= 0; i--) {
|
||||
int pos = 0;
|
||||
for (; str_row[i] >= '0' && str_row[i] <= '9' && i >= 0; i--) {
|
||||
row[n] += (str_row[i] - '0') * pow(10, pos);
|
||||
pos++;
|
||||
}
|
||||
n--;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
// check if specific report is safe
|
||||
int check_report(int *row, int len)
|
||||
{
|
||||
// read number. Compare next number (higher, lower, +-1-2)
|
||||
// if you get to row[i + 1]line, ++
|
||||
bool rising = 0;
|
||||
bool first = 1;
|
||||
for (int i = 0; i < len - 1; i++) {
|
||||
// if difference isnt 1-3
|
||||
if (abs(row[i + 1] - row[i]) > 3 || abs(row[i + 1] - row[i]) < 1)
|
||||
return 0;
|
||||
// only for first comparison of row. Set direction.
|
||||
if (first && ((row[i + 1] - row[i]) < 0))
|
||||
rising = 0;
|
||||
else if (first)
|
||||
rising = 1;
|
||||
// for other comparisons, compare current direction to the previous.
|
||||
if (!first && (rising != !((row[i + 1] - row[i]) < 0))) {
|
||||
// printf("rising: %d while %d - %d is %d\n", rising, row[i + 1], row[i], row[i + 1] - row[i]);
|
||||
return 0;
|
||||
}
|
||||
// get ready for the next comparisons!
|
||||
first = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dampener(int *row, int len)
|
||||
{
|
||||
if (check_report(row, len))
|
||||
return 1;
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
int *dampened_row = malloc((len - 1) * sizeof(int));
|
||||
// initialize shorter array, but skip element i
|
||||
bool skipped = 0;
|
||||
for (int j = 0; j < len - 1; j++) {
|
||||
if (j != i && !skipped) {
|
||||
dampened_row[j] = row[j];
|
||||
}
|
||||
else {
|
||||
dampened_row[j] = row[j + 1];
|
||||
skipped = 1;
|
||||
}
|
||||
}
|
||||
if (check_report(dampened_row, len - 1)) {
|
||||
free(dampened_row);
|
||||
return 1;
|
||||
}
|
||||
free(dampened_row);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int safe = 0;
|
||||
// read input row into array, count elements
|
||||
FILE *input;
|
||||
input = fopen("./input", "r");
|
||||
char line[32];
|
||||
for (int i = 0; i < LINES; i++) {
|
||||
// get string (line), count numbers, put into int array (row)
|
||||
int numbers = 1;
|
||||
fgets(line, sizeof(line), input);
|
||||
// count number of numbers, allocate memory
|
||||
for (int c = 0; line[c]; c++) numbers += (line[c] == ' ');
|
||||
int *row = (int *)calloc(numbers, sizeof(int));
|
||||
// parse numbers into array
|
||||
parse_numbers(line, row, numbers);
|
||||
// for (int j = 0; j < numbers; j++) printf("%d ", row[j]);
|
||||
// printf("\n\n");
|
||||
|
||||
// send elements and length to check_report, add result to sum
|
||||
safe += dampener(row, numbers);
|
||||
free(row);
|
||||
}
|
||||
|
||||
printf("%d\n", safe);
|
||||
}
|
1
README.md
Normal file
1
README.md
Normal file
|
@ -0,0 +1 @@
|
|||
plz be nice
|
Loading…
Reference in a new issue