Add C day 1

main
Bram 2023-12-27 21:46:21 +01:00
parent 455db932ad
commit 25bb949724
5 changed files with 1171 additions and 0 deletions

7
c/01/Makefile Normal file
View File

@ -0,0 +1,7 @@
both: p1 p2
p1:
gcc -o main1 part_1.c -lcs50
p2:
gcc -o main2 part_2.c -lcs50

1000
c/01/input.txt Normal file

File diff suppressed because it is too large Load Diff

43
c/01/part_1.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <cs50.h>
#include <string.h>
// The amount of input lines
const int lines = 1000;
int extract_number(char *text);
int main(void) {
int sum = 0;
for (int i = 0; i < lines; i++) {
sum += extract_number(get_string(""));
}
printf("%i\n", sum);
return 0;
}
/* Get the right number from both sides of the string
*/
int extract_number(char *text) {
int length = strlen(text);
int first;
for (int i = 0; i < length; i++) {
char c = text[i];
if (c >= '0' && c <= '9') {
first = c - '0';
break;
}
}
for (int i = 1; i <= length; i++) {
char c = text[length - i];
if (c >= '0' && c <= '9') {
return (first * 10) + (c - '0');
}
}
}

108
c/01/part_2.c Normal file
View File

@ -0,0 +1,108 @@
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// The amount of input lines
const int lines = 1000;
// The amount of numbers that can be found
const int number_size = 10;
const char numbers[10][6] =
{ "zero"
, "one"
, "two"
, "three"
, "four"
, "five"
, "six"
, "seven"
, "eight"
, "nine"
};
int extract_number(int *numbers, int size);
int *find_numbers(char *text);
int numbers_size(int *numbers);
int starts_with_number(char *text);
int main(void) {
int sum = 0;
for (int i = 0; i < lines; i++) {
// Instead of directly looping through the array,
// we extract a list of integers first
int *ns = find_numbers(get_string(""));
// Then use that list to increase the sum
sum += extract_number(ns, numbers_size(ns));
// Free all allocated information
free(ns);
}
printf("%i\n", sum);
return 0;
}
/* Given a list of integers, combines the first and the last digit to create a
single number between 0 and 100.
*/
int extract_number(int *numbers, int size) {
return (numbers[0] * 10) + numbers[size - 1];
}
// Convert a string to an array of integers
int *find_numbers(char *text) {
int length = strlen(text);
int *items = malloc(sizeof(int) * length);
int cursor = 0;
for (int i = 0; i < length; i++) {
char c = text[i];
if (c >= '0' && c <= '9') {
items[cursor] = c - '0';
cursor++;
}
else {
int n = starts_with_number(text + i);
if (n >= 0) {
items[cursor] = n;
cursor++;
}
}
}
items[cursor] = -1;
return items;
}
int numbers_size(int *numbers) {
int i = 0;
while (numbers[i] >= 0) {
i++;
}
return i;
}
/* Determine whether a (sub)string starts with a given number.
*/
int starts_with_number(char *text) {
for (int i = 0; i < number_size; i ++) {
if (strstr(text, numbers[i]) == text) {
return i;
}
}
// If not, return -1
return -1;
}

13
shell.nix Normal file
View File

@ -0,0 +1,13 @@
{ pkgs ? import <nixpkgs> {}
}:
pkgs.mkShell {
name="dev-environment";
buildInputs = [
pkgs.libgcc
pkgs.libcs50
pkgs.valgrind
];
shellHook = ''
echo "Start developing! :D"
'';
}