diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-19 15:54:12 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-19 15:54:12 +0100 |
| commit | 1f75d7c9ebec207c8193754792076dc66eba9713 (patch) | |
| tree | 48556a419f050153513cd5a3b8a4a6e125ba2c45 /2023/15 | |
| parent | 83adb5e4215a25d17cfc11b4dbf10cf20c418c20 (diff) | |
| download | aoc-1f75d7c9ebec207c8193754792076dc66eba9713.tar.gz aoc-1f75d7c9ebec207c8193754792076dc66eba9713.zip | |
Added solutions for 15, 16, 17, 18 and 19
Diffstat (limited to '2023/15')
| -rw-r--r-- | 2023/15/15a.c | 21 | ||||
| -rw-r--r-- | 2023/15/15b.c | 48 |
2 files changed, 69 insertions, 0 deletions
diff --git a/2023/15/15a.c b/2023/15/15a.c new file mode 100644 index 0000000..16a8fec --- /dev/null +++ b/2023/15/15a.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | |||
| 3 | #define N 100000 | ||
| 4 | |||
| 5 | char *b, line[N]; | ||
| 6 | int c, s; | ||
| 7 | |||
| 8 | int main() { | ||
| 9 | fgets(line, N, stdin); | ||
| 10 | |||
| 11 | for (b = line, c = 0, s = 0; *b; b++) { | ||
| 12 | if (*b == ',' || *b == '\n') { | ||
| 13 | s += c; | ||
| 14 | c = 0; | ||
| 15 | } else | ||
| 16 | c = (c + (int)*b) * 17 % 256; | ||
| 17 | } | ||
| 18 | |||
| 19 | printf("%d\n", s); | ||
| 20 | return 0; | ||
| 21 | } | ||
diff --git a/2023/15/15b.c b/2023/15/15b.c new file mode 100644 index 0000000..15b97f9 --- /dev/null +++ b/2023/15/15b.c | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | #define N 100000 | ||
| 5 | |||
| 6 | char *b, line[N]; | ||
| 7 | int64_t c, l, s, n[256], box[256][N]; | ||
| 8 | |||
| 9 | int main() { | ||
| 10 | fgets(line, N, stdin); | ||
| 11 | |||
| 12 | for (b = line, c = 0; *b != '\n'; b++) { | ||
| 13 | switch (*b) { | ||
| 14 | case ',': | ||
| 15 | c = 0; | ||
| 16 | l = 0; | ||
| 17 | break; | ||
| 18 | case '=': | ||
| 19 | int64_t i; | ||
| 20 | for (i = 0; i < n[c]; i++) | ||
| 21 | if (box[c][i] / 10 == l) | ||
| 22 | break; | ||
| 23 | if (i == n[c]) n[c]++; | ||
| 24 | box[c][i] = l * 10 + (int)(*(++b)-'0'); | ||
| 25 | break; | ||
| 26 | case '-': | ||
| 27 | for (int64_t i = 0; i < n[c]; i++) { | ||
| 28 | if (box[c][i] / 10 == l) { | ||
| 29 | for (int j = i+1; j < n[c]; j++) | ||
| 30 | box[c][j-1] = box[c][j]; | ||
| 31 | n[c]--; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | break; | ||
| 35 | default: | ||
| 36 | c = (c + (int)*b) * 17 % 256; | ||
| 37 | l = l*256 + (int)*b; | ||
| 38 | break; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | for (int64_t i = 0; i < 256; i++) | ||
| 43 | for (int64_t j = 0; j < n[i]; j++) | ||
| 44 | s += (i+1) * (j+1) * (box[i][j]%10); | ||
| 45 | |||
| 46 | printf("%" PRId64 "\n", s); | ||
| 47 | return 0; | ||
| 48 | } | ||
