2022-04
This commit is contained in:
parent
0ad14354a6
commit
77592d090e
@ -1,10 +1,49 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "lfinput.h"
|
#include "lfinput.h"
|
||||||
|
#include "advent_utility.h"
|
||||||
|
|
||||||
|
struct Point parse_floors(char *input) {
|
||||||
|
size_t sz = 0;
|
||||||
|
char **sp = split(input, &sz, "-");
|
||||||
|
const char *errstr;
|
||||||
|
struct Point p;
|
||||||
|
p.x = strtonum(sp[0], INT_MIN, INT_MAX, &errstr);
|
||||||
|
p.y = strtonum(sp[1], INT_MIN, INT_MAX, &errstr);
|
||||||
|
free(sp);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
void advent2022day04(void) {
|
void advent2022day04(void) {
|
||||||
char *input = get_input("input/2022/04");
|
size_t sz = 0;
|
||||||
printf("Solution for Day 04 of 2022 is not completed yet\n");
|
char **lines = get_lines("input/2022/04", &sz);
|
||||||
free(input);
|
int p1 = 0, p2 = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < sz; ++i) {
|
||||||
|
size_t sp_sz = 0;
|
||||||
|
char **sp = split(lines[i], &sp_sz, ",");
|
||||||
|
struct Point e1 = parse_floors(sp[0]);
|
||||||
|
struct Point e2 = parse_floors(sp[1]);
|
||||||
|
|
||||||
|
if (e1.x <= e2.x && e1.y >= e2.y) {
|
||||||
|
++p1;
|
||||||
|
} else if (e2.x <= e1.x && e2.y >= e1.y) {
|
||||||
|
++p1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e1.x >= e2.x && e1.x <= e2.y) || (e1.y >= e2.x && e1.y <= e2.y)) {
|
||||||
|
++p2;
|
||||||
|
} else if ((e2.x >= e1.x && e2.x <= e1.y) || (e2.y >= e1.x && e2.y <= e1.y)) {
|
||||||
|
++p2;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(sp);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d\n", p1);
|
||||||
|
printf("%d\n", p2);
|
||||||
|
|
||||||
|
del_lines(lines);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user