diff options
-rwxr-xr-x | aoc/2022/10/part-one.awk | 8 | ||||
-rwxr-xr-x | aoc/2022/10/part-two.awk | 10 | ||||
-rw-r--r-- | aoc/2024/01/part-one.py | 4 | ||||
-rw-r--r-- | aoc/2024/01/part-two.py | 6 | ||||
-rw-r--r-- | aoc/2024/02/part-one.py | 6 | ||||
-rw-r--r-- | aoc/2024/02/part-two.py | 21 | ||||
-rw-r--r-- | aoc/2024/03/part-one.c | 11 | ||||
-rw-r--r-- | aoc/2024/03/part-two.c | 27 | ||||
-rw-r--r-- | aoc/2024/04/part-one.py | 23 | ||||
-rw-r--r-- | aoc/2024/04/part-two.py | 10 | ||||
-rw-r--r-- | aoc/2024/05/part-one.c | 27 | ||||
-rw-r--r-- | aoc/2024/05/part-two.c | 35 |
12 files changed, 188 insertions, 0 deletions
diff --git a/aoc/2022/10/part-one.awk b/aoc/2022/10/part-one.awk new file mode 100755 index 0000000..756464e --- /dev/null +++ b/aoc/2022/10/part-one.awk @@ -0,0 +1,8 @@ +#!/usr/bin/env -S awk -f +BEGIN { cycle = x = 1 } +{ cycle++ } +cycle % 40 == 20 { strength += cycle * x } +{ x += $2 } +/^addx/ { cycle++ } +/^addx/ && cycle % 40 == 20 { strength += cycle * x } +END { print strength } diff --git a/aoc/2022/10/part-two.awk b/aoc/2022/10/part-two.awk new file mode 100755 index 0000000..9126306 --- /dev/null +++ b/aoc/2022/10/part-two.awk @@ -0,0 +1,10 @@ +#!/usr/bin/env -S awk -f +function cycle (c, x) { + printf x - 2 < c && c < x + 2 ? "#" : "." + printf c == 39 ? "\n" : "" + return (c + 1) % 40 +} +BEGIN { x = 1 } +{ c = cycle(c, x) } +/^addx/ { c = cycle(c, x) } +{ x += $2 } diff --git a/aoc/2024/01/part-one.py b/aoc/2024/01/part-one.py new file mode 100644 index 0000000..2a2421e --- /dev/null +++ b/aoc/2024/01/part-one.py @@ -0,0 +1,4 @@ +from sys import stdin + +left, right = zip(*((int(a), int(b)) for a, b in map(str.split, stdin))) +print(sum(map(abs, map(int.__sub__, sorted(left), sorted(right))))) diff --git a/aoc/2024/01/part-two.py b/aoc/2024/01/part-two.py new file mode 100644 index 0000000..5000c9c --- /dev/null +++ b/aoc/2024/01/part-two.py @@ -0,0 +1,6 @@ +from sys import stdin +from collections import Counter + +left, right = zip(*((int(a), int(b)) for a, b in map(str.split, stdin))) +counter = Counter(right) +print(sum(counter[n]*n for n in left)) diff --git a/aoc/2024/02/part-one.py b/aoc/2024/02/part-one.py new file mode 100644 index 0000000..e0d74db --- /dev/null +++ b/aoc/2024/02/part-one.py @@ -0,0 +1,6 @@ +from sys import stdin + +print(sum(all(0 < d < 4 for d in diffs) or all(-4 < d < 0 for d in diffs) + for diffs in (tuple(map(int.__sub__, levels, levels[1:])) + for levels in (tuple(map(int, line)) + for line in map(str.split, stdin))))) diff --git a/aoc/2024/02/part-two.py b/aoc/2024/02/part-two.py new file mode 100644 index 0000000..ad5f5a6 --- /dev/null +++ b/aoc/2024/02/part-two.py @@ -0,0 +1,21 @@ +from collections import Counter +from itertools import chain, count +from sys import stdin + + +def safe(levels, dampened=False): + head = levels[0 if 0 < levels[1] - levels[0] < 4 else 1] - 1, + tail = levels[-1 if 0 < levels[-1] - levels[-2] < 4 else -2] + 1, + dampenable = tuple((not dampened and 0 < a - c < 4 + and safe(levels[:i]+levels[i+1:], True)) + for i, a, b, c in zip(count(), + chain(levels[1:], tail), + levels, + chain(head, levels)) + if not (0 < a - b < 4 and 0 < b - c < 4)) + return not dampenable or any(dampenable) + + +print(sum(safe(levels) or safe(tuple(map(int.__neg__, levels))) + for levels in (tuple(map(int, line)) + for line in map(str.split, stdin)))) diff --git a/aoc/2024/03/part-one.c b/aoc/2024/03/part-one.c new file mode 100644 index 0000000..0438304 --- /dev/null +++ b/aoc/2024/03/part-one.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +int main() +{ + size_t x, y, s = 0; + while (scanf("%*[^m]") != EOF) + if (scanf("mul(%3zu,%3zu", &x, &y) == 2 && getchar() == ')') + s += x * y; + printf("%zu\n", s); + return 0; +} diff --git a/aoc/2024/03/part-two.c b/aoc/2024/03/part-two.c new file mode 100644 index 0000000..4ae337f --- /dev/null +++ b/aoc/2024/03/part-two.c @@ -0,0 +1,27 @@ +#include <stdbool.h> +#include <stdio.h> + +int main() +{ + bool d = true; + size_t x, y, s = 0; + while (scanf("%*[^dm]") != EOF) { + if (getchar() == 'd') { + int c; +#define unless(i) if ((c = getchar()) != i && ungetc(c, stdin) == c) + unless ('o') continue; + unless ('(') { + if (scanf("n't()") != EOF) + d = false; + continue; + } + unless (')') continue; +#undef unless + d = true; + } + if (scanf("ul(%3zu,%3zu", &x, &y) == 2 && getchar() == ')' && d) + s += x * y; + } + printf("%zu\n", s); + return 0; +} diff --git a/aoc/2024/04/part-one.py b/aoc/2024/04/part-one.py new file mode 100644 index 0000000..ba25336 --- /dev/null +++ b/aoc/2024/04/part-one.py @@ -0,0 +1,23 @@ +from sys import stdin + + +def index(m, n, i, j, c): + return (m[i][j], m[n-j-1][i], m[n-i-1][n-j-1], m[j][n-i-1])[c] + + +def rot(m, c): + n = len(m) + if n != len(m[0]): raise ValueError + for i in range(n): + yield ''.join(index(m, n, i-j, j, c) for j in range(i+1)) + for i in range(1, n): + yield ''.join(index(m, n, n-j-1, i+j, c) for j in range(n-i)) + + +reverse_all = lambda strings: (s[::-1] for s in strings) +count = lambda strings: sum(s.count('XMAS') for s in strings) +lines = tuple(map(str.strip, stdin.readlines())) +columns = tuple(''.join(column) for column in zip(*lines)) +print((count(lines) + count(reverse_all(lines)) + + count(columns) + count(reverse_all(columns)) + + sum(count(rot(lines, c)) for c in range(4)))) diff --git a/aoc/2024/04/part-two.py b/aoc/2024/04/part-two.py new file mode 100644 index 0000000..a3e7c4a --- /dev/null +++ b/aoc/2024/04/part-two.py @@ -0,0 +1,10 @@ +from itertools import islice +from sys import stdin + +mat = tuple(map(str.strip, stdin.readlines())) +m = len(mat) +n = len(mat[0]) +print(sum(c == 'A' and ({mat[i-1][j-1], mat[i+1][j+1]} + == {mat[i-1][j+1], mat[i+1][j-1]} == {'M', 'S'}) + for i, line in islice(enumerate(mat), 1, m-1) + for j, c in islice(enumerate(line), 1, n-1))) diff --git a/aoc/2024/05/part-one.c b/aoc/2024/05/part-one.c new file mode 100644 index 0000000..4d2b560 --- /dev/null +++ b/aoc/2024/05/part-one.c @@ -0,0 +1,27 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +int main() +{ + bool after[100][100] = {false}; + unsigned char x, y; + while (scanf("%hhu|%hhu\n", &x, &y) == 2) + after[y][x] = true; + + unsigned char line[90]; + unsigned sum = y = 0; + do { + bool right = true; + do { + line[y] = x; + for (unsigned char i = 0; right && i < y; ++i) + if (after[line[i]][line[y]]) + right = false; + } while (scanf(",%hhu", &x) == 1 && ++y); + if (right) + sum += line[y + 1 >> 1]; + } while (y = 0, scanf("%hhu", &x) == 1); + printf("%u\n", sum); + return 0; +} diff --git a/aoc/2024/05/part-two.c b/aoc/2024/05/part-two.c new file mode 100644 index 0000000..443e8b0 --- /dev/null +++ b/aoc/2024/05/part-two.c @@ -0,0 +1,35 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +static bool after[100][100]; + +int compare(const void *x, const void *y) +{ + return after[*(const unsigned char *) x][*(const unsigned char *) y]; +} + +int main() +{ + unsigned char x, y; + while (scanf("%hhu|%hhu\n", &x, &y) == 2) + after[y][x] = true; + + unsigned char line[90]; + unsigned sum = y = 0; + do { + bool right = true; + do { + line[y] = x; + for (unsigned char i = 0; right && i < y; ++i) + if (after[line[i]][line[y]]) + right = false; + } while (scanf(",%hhu", &x) == 1 && ++y); + if (right) + continue; + qsort(line, ++y, 1, compare); + sum += line[y >> 1]; + } while (y = 0, scanf("%hhu", &x) == 1); + printf("%u\n", sum); + return 0; +} |