Compare commits
5 Commits
2ae96c9a04
...
17df598e16
Author | SHA1 | Date |
---|---|---|
Alexander Andreev | 17df598e16 | |
Alexander Andreev | 2f2b4194c6 | |
Alexander Andreev | b19aa5edca | |
Alexander Andreev | 83944d8d4c | |
Alexander Andreev | 443576621d |
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
uint64_t parse_number(char *end);
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
uint64_t parse_number(char *end);
|
||||
|
|
|
@ -3,10 +3,11 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FILE *input;
|
||||
char *line = NULL;
|
||||
|
@ -57,9 +58,8 @@ int main(int argc, char **argv) {
|
|||
((i+1 <= engine_lines-1 && j+1 <= engine_line_size-1) && !ISDIGIT(engine[i+1][j+1]) && engine[i+1][j+1] != '.') ||
|
||||
((i+1 <= engine_lines-1 && j-1 >= 0) && !ISDIGIT(engine[i+1][j-1]) && engine[i+1][j-1] != '.') ||
|
||||
((i-1 >= 0 && j+1 <= engine_line_size-1) && !ISDIGIT(engine[i-1][j+1]) && engine[i-1][j+1] != '.') ||
|
||||
((i-1 >= 0 && j-1 >= 0) && !ISDIGIT(engine[i-1][j-1]) && engine[i-1][j-1] != '.'))) {
|
||||
((i-1 >= 0 && j-1 >= 0) && !ISDIGIT(engine[i-1][j-1]) && engine[i-1][j-1] != '.')))
|
||||
adjacent = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ((!ISDIGIT(engine[i][j]) || j == 0) && n != 0) {
|
||||
|
|
|
@ -3,12 +3,13 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
||||
|
||||
uint64_t parse_number(char *line, ssize_t j);
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FILE *input;
|
||||
char *line = NULL;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
uint64_t parse_number(char *end);
|
||||
|
@ -43,12 +42,14 @@ int main(int argc, char **argv) {
|
|||
|
||||
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
||||
uint8_t n = parse_number(n_pos-1);
|
||||
|
||||
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
||||
if (n != winning_numbers[i])
|
||||
continue;
|
||||
points = (points == 0) ? 1 : points * 2;
|
||||
break;
|
||||
}
|
||||
|
||||
if (*n_pos == '\n')
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#define STACK_SZ UINT8_MAX+1
|
||||
|
@ -42,24 +41,25 @@ int main(int argc, char **argv) {
|
|||
winning_numbers = (uint8_t *) calloc(winning_numbers_len, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) {
|
||||
for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3)
|
||||
winning_numbers[i] = parse_number((char *)(wl_pos-1));
|
||||
}
|
||||
|
||||
uint64_t card_copies = 1 + stack[stack_pos++];
|
||||
|
||||
total_copies += card_copies;
|
||||
|
||||
size_t old_sp = stack_pos;
|
||||
stackpointer_t old_sp = stack_pos;
|
||||
|
||||
for (char *n_pos = pipe_pos + 4;; n_pos += 3) {
|
||||
uint8_t n = parse_number(n_pos-1);
|
||||
|
||||
for (size_t i = 0; i < winning_numbers_len; ++i) {
|
||||
if (n != winning_numbers[i])
|
||||
continue;
|
||||
stack[stack_pos++] += card_copies;
|
||||
break;
|
||||
}
|
||||
|
||||
if (*n_pos == '\n')
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
int64_t parse_number(char *end);
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
uint64_t parse_number(char *end);
|
||||
|
@ -63,8 +62,7 @@ int main(int argc, char **argv) {
|
|||
} else if (line[0]>>4 != 0x3)
|
||||
continue;
|
||||
|
||||
map_lengths[maps_cur]++;
|
||||
maps[maps_cur] = (map_t *) realloc(maps[maps_cur], map_lengths[maps_cur] * sizeof(map_t));
|
||||
maps[maps_cur] = (map_t *) realloc(maps[maps_cur], ++map_lengths[maps_cur] * sizeof(map_t));
|
||||
|
||||
map_t new_range_map = {0};
|
||||
sscanf(line, "%lu %lu %lu\n", &new_range_map.dst, &new_range_map.src, &new_range_map.rng);
|
||||
|
|
13
day6/p1.c
13
day6/p1.c
|
@ -3,7 +3,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
||||
|
@ -41,10 +40,9 @@ int main(int argc, char **argv) {
|
|||
ssize_t times_length = 2, distances_length = 2;
|
||||
|
||||
for (ssize_t i = time_line_length-2, j = 0;;) {
|
||||
while (!ISDIGIT(time_line[i])) {
|
||||
while (!ISDIGIT(time_line[i]))
|
||||
if (time_line[--i] == ':')
|
||||
goto tl_end;
|
||||
}
|
||||
|
||||
if (j == times_length)
|
||||
times = (uint64_t *) realloc(times, ++times_length * sizeof(uint64_t));
|
||||
|
@ -56,10 +54,9 @@ int main(int argc, char **argv) {
|
|||
tl_end:
|
||||
|
||||
for (ssize_t i = distance_line_length-2, j = 0;;) {
|
||||
while (!ISDIGIT(distance_line[i])) {
|
||||
while (!ISDIGIT(distance_line[i]))
|
||||
if (distance_line[--i] == ':')
|
||||
goto dl_end;
|
||||
}
|
||||
|
||||
if (j == distances_length)
|
||||
distances = (uint64_t *) realloc(distances, ++distances_length * sizeof(uint64_t));
|
||||
|
@ -76,19 +73,17 @@ int main(int argc, char **argv) {
|
|||
uint64_t tmid = times[i] / 2;
|
||||
uint64_t ways = 0;
|
||||
|
||||
for (uint64_t ht = tmid; ht <= times[i]; ++ht) {
|
||||
for (uint64_t ht = tmid; ht <= times[i]; ++ht)
|
||||
if (ht * (times[i] - ht) > distances[i])
|
||||
++ways;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
for (uint64_t ht = tmid-1; ht != 0; --ht) {
|
||||
for (uint64_t ht = tmid-1; ht != 0; --ht)
|
||||
if (ht * (times[i] - ht) > distances[i])
|
||||
++ways;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
total_ways *= ways;
|
||||
}
|
||||
|
|
14
day6/p2.c
14
day6/p2.c
|
@ -3,11 +3,11 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FILE *input;
|
||||
size_t line_buf_length = 0;
|
||||
|
@ -35,10 +35,9 @@ int main(int argc, char **argv) {
|
|||
uint64_t time = 0, distance = 0;
|
||||
|
||||
for (ssize_t i = time_line_length-2, m = 1;; --i) {
|
||||
while (!ISDIGIT(time_line[i])) {
|
||||
while (!ISDIGIT(time_line[i]))
|
||||
if (time_line[--i] == ':')
|
||||
goto tl_end;
|
||||
}
|
||||
|
||||
while (ISDIGIT(time_line[i])) {
|
||||
time += (time_line[i--]&0xf) * m;
|
||||
|
@ -48,10 +47,9 @@ int main(int argc, char **argv) {
|
|||
tl_end:
|
||||
|
||||
for (ssize_t i = distance_line_length-2, m = 1;; --i) {
|
||||
while (!ISDIGIT(distance_line[i])) {
|
||||
while (!ISDIGIT(distance_line[i]))
|
||||
if (distance_line[--i] == ':')
|
||||
goto dl_end;
|
||||
}
|
||||
|
||||
while (ISDIGIT(distance_line[i])) {
|
||||
distance += (distance_line[i--]&0xf) * m;
|
||||
|
@ -64,19 +62,17 @@ int main(int argc, char **argv) {
|
|||
|
||||
uint64_t tmid = time / 2;
|
||||
|
||||
for (uint64_t ht = tmid; ht <= time; ++ht) {
|
||||
for (uint64_t ht = tmid; ht <= time; ++ht)
|
||||
if (ht * (time - ht) > distance)
|
||||
++total_ways;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
for (uint64_t ht = tmid-1; ht != 0; --ht) {
|
||||
for (uint64_t ht = tmid-1; ht != 0; --ht)
|
||||
if (ht * (time - ht) > distance)
|
||||
++total_ways;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
printf("%lu\n", total_ways);
|
||||
|
||||
|
|
Loading…
Reference in New Issue