Add C day 1
parent
455db932ad
commit
25bb949724
|
@ -0,0 +1,7 @@
|
|||
both: p1 p2
|
||||
|
||||
p1:
|
||||
gcc -o main1 part_1.c -lcs50
|
||||
|
||||
p2:
|
||||
gcc -o main2 part_2.c -lcs50
|
File diff suppressed because it is too large
Load Diff
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue