m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/03
diff options
context:
space:
mode:
Diffstat (limited to '03')
-rw-r--r--03/a.c69
-rw-r--r--03/b.c106
-rw-r--r--03/input.txt323
3 files changed, 498 insertions, 0 deletions
diff --git a/03/a.c b/03/a.c
new file mode 100644
index 0000000..caa1d60
--- /dev/null
+++ b/03/a.c
@@ -0,0 +1,69 @@
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define BUF_SIZE 8192
+
+char buffer[BUF_SIZE];
+
+ssize_t read_to_buffer() {
+ ssize_t bytes = read(0, buffer, BUF_SIZE);
+ if (bytes < 0) {
+ printf("failed to read\n");
+ exit(1);
+ }
+ return bytes;
+}
+
+int tree(int position) {
+ if (buffer[position] == '.') {
+ return 0;
+ } else if (buffer[position] == '#') {
+ return 1;
+ } else {
+ printf("unknown character\n");
+ exit(1);
+ }
+}
+
+int file_position(int x, int y, int line_length) {
+ return (line_length + 2) * y + x;
+}
+
+int buffer_position(int x, int y, int line_length, int buffer_start) {
+ return file_position(x % line_length, y, line_length) - buffer_start;
+}
+
+int find_trees() {
+ int buffer_start = 0;
+ int line_length = 0;
+ int x = 0;
+ int y = 0;
+ ssize_t bytes = read_to_buffer();
+ int trees_hit = tree(0);
+
+ while (buffer[line_length] != '\r') {
+ line_length++;
+ }
+
+ int i = 2;
+ while (bytes) {
+ while (buffer_position(x + 3, y + 1, line_length, buffer_start) < bytes) {
+ y += 1;
+ x += 3;
+
+ trees_hit += tree(buffer_position(x, y, line_length, buffer_start));
+ }
+
+ buffer_start += bytes;
+ bytes = read_to_buffer();
+ }
+
+ return trees_hit;
+}
+
+int main() {
+ printf("%d\n", find_trees());
+}
diff --git a/03/b.c b/03/b.c
new file mode 100644
index 0000000..2f01601
--- /dev/null
+++ b/03/b.c
@@ -0,0 +1,106 @@
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define BUF_SIZE 8192
+#define NUM_SLOPES 5
+
+char buffer[BUF_SIZE];
+
+struct Pair {
+ int x;
+ int y;
+};
+
+struct Pair slopes[NUM_SLOPES];
+
+ssize_t read_to_buffer() {
+ ssize_t bytes = read(0, buffer, BUF_SIZE);
+ return bytes;
+}
+
+int tree(int position) {
+ if (buffer[position] == '.') {
+ return 0;
+ } else if (buffer[position] == '#') {
+ return 1;
+ }
+}
+
+int file_position(int x, int y, int line_length) {
+ return (line_length + 2) * y + x;
+}
+
+int buffer_position(struct Pair pos, int line_length, int buffer_start) {
+ return file_position(pos.x % line_length, pos.y, line_length) - buffer_start;
+}
+
+long long int find_trees() {
+ int trees_hit[NUM_SLOPES];
+ struct Pair next_check[NUM_SLOPES];
+ for (int i = 0; i < NUM_SLOPES; i++) {
+ trees_hit[i] = 0;
+ next_check[i].x = slopes[i].x;
+ next_check[i].y = slopes[i].y;
+ }
+
+ int y = 0;
+
+ ssize_t bytes = read_to_buffer();
+ int buffer_start = 0;
+ int line_length = 0;
+ while (buffer[line_length] != '\r') {
+ line_length++;
+ }
+
+ while (bytes) {
+ int checked = 5;
+ while (checked == 5) {
+ checked = 0;
+ for (int i = 0; i < NUM_SLOPES; i++) {
+ int position = buffer_position(next_check[i], line_length, buffer_start);
+ if (position < bytes && position >= 0 && y == next_check[i].y) {
+ trees_hit[i] += tree(position);
+
+ next_check[i].x += slopes[i].x;
+ next_check[i].x %= line_length;
+ next_check[i].y += slopes[i].y;
+
+ checked++;
+ } else if (position < 0 || y < next_check[i].y) {
+ checked++;
+ }
+ }
+
+ if (checked == NUM_SLOPES) {
+ y++;
+ }
+ }
+
+ buffer_start += bytes;
+ bytes = read_to_buffer();
+ }
+
+ long long int res = 1;
+ for (int i = 0; i < NUM_SLOPES; i++) {
+ res *= trees_hit[i];
+ }
+
+ return res;
+}
+
+int main() {
+ slopes[0].x = 1;
+ slopes[0].y = 1;
+ slopes[1].x = 3;
+ slopes[1].y = 1;
+ slopes[2].x = 5;
+ slopes[2].y = 1;
+ slopes[3].x = 7;
+ slopes[3].y = 1;
+ slopes[4].x = 1;
+ slopes[4].y = 2;
+ printf("%lld\n", find_trees());
+}
diff --git a/03/input.txt b/03/input.txt
new file mode 100644
index 0000000..104bc3d
--- /dev/null
+++ b/03/input.txt
@@ -0,0 +1,323 @@
+.........#..##.##..............
+#...#.#..#.....................
+.#...#..#...................#..
+........##..#...#..............
+.#.#.....#..#..##......#.......
+....#..#...#..##........#..##..
+...#....##........#.......#.#..
+....#................#...###..#
+...#...#.#..#....#.......####.#
+.....#...#..........#...#..#.#.
+....#..#............#.#.#.#..#.
+..#....#.###..#............#...
+.....#.............#.#.........
+.#.##............##.........#..
+...##...#..#....#.##..#.....#..
+..............#.#.........#.##.
+...........#.....##....##......
+.......#............#...#......
+............#.#....#....#..#..#
+....#................####......
+...#.........................##
+..........#........#.#.........
+....#.#....#...........#......#
+..#.#..##......##..##..#..#.#..
+...#.....##......#.#.#.........
+.........#.#....#...#.#.#......
+.......#.......###.#.......#...
+..#............##..#.......#...
+...#....#......#...#...#...#...
+......#..#.#.....##......#.....
+...........##...##...#....#.##.
+#.##..#.....##..#.#............
+.#.#.....##......#.##........#.
+..#...#...#...#..........#...##
+...##.........................#
+.....#......#.....##....#.....#
+..#........#...................
+#......#..#.#..#..#.#..#...#...
+...............#..........#....
+.....#...........#......#....#.
+........#..#...............#...
+.........#...#.......#.#..#...#
+..#..#......#.##..........#....
+.#...#....#.....#.............#
+.##.....#.........#......#..#..
+........#..##.......#......#...
+.......#.....###......#..#.....
+.......#.#.......#.............
+...#................##.#.......
+..##..#...#.#...#.#..#.#.#.##..
+.......#.#............#...#....
+#...#.....#......#..........##.
+.#.......#......#.......#.#.#..
+.#.##.#.#...........#..........
+.......#.....#....#.#.##......#
+.###..#...#.............##.....
+......#......#.................
+##...#.#...##...#.#.....#....#.
+#.............#....##...#....#.
+#.#...#....#........#.###..##..
+......#.........#......#.#.#.#.
+..#.#.#.....#........#..#...#..
+#.##....#.#......#...........#.
+.#.#.####.........#..#.##....##
+......##...............#......#
+.......#.........#........#.#..
+....#....#..#.##.........#..#..
+.#..........#.##....#.#..#.....
+#..#.#............#..#....#.#.#
+..................#..#.........
+##..##.#....#.................#
+..................#........#..#
+.....#.........#.......##......
+.....................#.#..#...#
+.....#.........#..........#.#..
+...#.#..#..#.#.#.......#.......
+.....#.....#.#.........#.....##
+.............##....#....##.#...
+.#......#........##..#...###...
+........#.......##.##.#......#.
+..#....................#.##..#.
+......#.......#..#....##.#.....
+...#....#.......##...#.......#.
+.#.#..#.#..........##..........
+....#.......##...........#.....
+###....#.......#..#...#.....##.
+...#......#.........#..#.#..#.#
+#.........#..##.#..............
+.#.....#..##.#..#..###.....##.#
+..........#..#....##.......#...
+.#..#.#...#...##.#..#.##.#.....
+#....#...#........#......##....
+..#.####....#.#........#....#..
+#......#............#.#........
+...#..#.......##...........#...
+.........#..#.#..#.###.#...#..#
+..#....##.......#.............#
+............#..#......#........
+........#......#..............#
+..#.#.#...........#............
+...........#......##.#.#.......
+.#..........#...........#..#...
+.....#...#..#.........##...#...
+.......#....##....#.#.........#
+..#.#......#.......#...##.#....
+.....#..........#........#.....
+#.......#.......#............#.
+..##.#.....#.##.#.#.#..#.......
+..#...#.......#.###............
+.#...#..#....#...#...#..#....##
+.....#.....#...................
+.......................#......#
+......#...##.........#...#..#..
+.....#..#.....#..............#.
+.#.##..#..#....................
+....#..#...#....#.............#
+..###..#...#......#.....#......
+..#......###....#.....#.....###
+...#.##..#...#.....#........#..
+.#.#...........##....#...#.##..
+.......#...##......#..#..#.....
+#.............#..#...##.#..#..#
+..........#......#.......#.....
+...............#.#.#....#...#..
+#.......#.#..#.....#........#..
+.#.#.#.......#..#.........##...
+......#.....#.#....#...........
+..#.....##.#........##.#......#
+...###...#..#.........#........
+....#...................#..#...
+.##........#...................
+....#..#...........#.#.........
+.....#.......#...#....#.#......
+.........#...#.......#.#...#...
+.......#.#..#....#....#.......#
+..#.............#..............
+.#...#..#.#.#..#............#..
+...#.##.##..#..#...........##..
+...........#...#..#.#........#.
+....#...#.....#...#.#....#...#.
+.......#.#...##..#.............
+.......................#....#..
+..#..#.....#...........#....#..
+.#..#...#.##........##....#....
+#.....##.#.#.......#.....#...#.
+.#....#.......................#
+#..##..###...#.........#.......
+..##...#...#..........#....#...
+......#..##......##.#.........#
+................#........#..#..
+.....#.#..#.....#.......#..#...
+..#..#.....#.......#..#..#...#.
+.#....#...#...#......##.....#..
+....#........#...#......##....#
+..#..........##......#......#..
+#.#.....#.....#.......#........
+...#...#......#....##.#..#...##
+...#....#...#.#...........##...
+#....##...#...#....#...........
+...#.#..#...#..............##..
+#..#..........##.#.#.....#.....
+..#...#.........#.#..........#.
+....#.....#..........#.........
+........................#......
+.#.....#.#...###...#....#......
+....##....#....#..#.##........#
+..#........#.........#.......#.
+.....#.#......#...#...#........
+........#..#.....#....###....#.
+...........#..#.#....#.#....##.
+.......#.....##.#............#.
+...............#........##.##..
+.............#...##......#...#.
+#...##.#.......#......###.....#
+..........#...#........#..#....
+....#....................#...#.
+.#......#...#.......#....#.#...
+....#.......................#..
+#...#...#...#.##....##.........
+..........#.#...##.#...#.......
+..#...............#....#..#...#
+#..#..#...#..#.........#...#...
+.....#..#..........#.##.#..##..
+........#......##.....#........
+.#....#.#.........#...#..#.#...
+....#..............##..........
+#...............#..............
+..###.........#....##.........#
+.........#.#....##........#...#
+....#.#..#......#...#..........
+...#.#.....#....#..#....#.#..#.
+............#..#.....#...##....
+...........#....#.#.#...#......
+#...............#....###.......
+.........#.....##.#..#..#......
+...#...##...###...............#
+.#......#.#.#.................#
+.........##..#............#....
+..#..#.....#.....#.#...........
+.#......##............#.#....#.
+.#.##..##.##..#.........#.....#
+...##.##......##.##......#.....
+##.....#.#...#...#...#..#......
+....................#......#...
+.....#.................#...###.
+...........#..#.........#.#....
+...#........#..#........#....#.
+#................#......###...#
+.............##.#.....#.#......
+...#...#.##..#........##.......
+#..#.##...#....#.#.............
+.#.........#.#..#...........#..
+....#...#.....#.#..........#...
+.#.#....###.......##.....#.##..
+.##....##......#......#.#....#.
+..#...#.#........#...#..##.....
+..............###..........##..
+#....#..##.....#.....#.....#...
+...#...#....................#..
+.#....#.#.....#.#..#..##.......
+#...##..###......#.............
+..........#.#....##.#........##
+..#..#.....#...#....#.#.#......
+#.....#........#..##.#.........
+....#.....#..........##......#.
+......#..#.....#........#.....#
+.....#..#....#...........#.##..
+.#....................#....#..#
+........#..#...........#.......
+#....#.#.......#........#.#..#.
+........#.....#...#............
+..#........#........#....#...#.
+.....##.......#..#..........#..
+......#.#......###...#....##..#
+.#..#.............#.#..........
+#.....##.#.#.#.#.#.....#.....#.
+.#..#.....#.......#.#.....#....
+###....##...#.#...#..#......###
+.#................#.....#.##...
+....##....#.#........###.#.#...
+#.#....#........#.....#.......#
+..........#..........#.##...#..
+....#....#..##......#..#.......
+.....#..........#.##...........
+##......#.#......#.##..........
+##..........##.......##........
+..#.....#....#.##..#..#..#.....
+......###...#...........#...###
+#..#.............##............
+...#.###.....#..#.........#.#..
+......#...............#...#.#..
+.....#...##.#...#.....#.#..#...
+..#..#.#....#.#................
+...............##.....#........
+......#.#.....#...#.........#..
+........#..#...#.#...#......#..
+#...........#.......#...##...#.
+........#.#...#..##..#.#...#...
+..#....#...#......#..........##
+..#..............##...##.#.....
+...#....#..#....##.........#.#.
+.#.#....#..........#.......#...
+...##....#.#....#....#.#...#...
+..............#..##........#..#
+..........#.#...##......#..#.#.
+#...##..#......................
+.......#........##.#.#.#.......
+.........##..#.#.......####....
+..#.............#..#........##.
+##..#..#...#....#.....#...#..#.
+..#.#...#.#.....#..............
+..#....#....#..##...#.#........
+##.....#..#...#................
+#....#.....................#...
+..............###.....#.#.#....
+..#......##.#....#.#...##......
+#...#.#......#...#.#......#....
+....#...................##.#...
+.........##......#.....#.####..
+##..#........#.....#......##..#
+...#..#...#...#.............#..
+#..#..#.#......###...#.........
+.......#.#..#........#....##..#
+............#..##.....#.#.#....
+#..#.....#.....#....##........#
+......#..........##............
+.....#...#...........#.........
+...........#....#...#....#.#...
+....#.........##.##.......#....
+......#....#...........#.##...#
+.##.#.#..##...#.....##.#...#...
+.......#.#....#...#...#....#...
+.#...##.#.#.....#..#....#......
+.#....###..#.......#......#...#
+..#.#.........#.........#.....#
+.......#.#.##..#.#.......##..#.
+.##............#.........#....#
+.#...##.###..#........##.#..#..
+..#........#.#.....##..##.#....
+...........#...........#.....#.
+.#...######..##...#.....#......
+.#.##.#.......#......#......#..
+.#.....#.....#........#........
+...#..#...#.##...#...........#.
+.......#.....#.......#.........
+............#...###...........#
+...#.......#.......##....#..#..
+##.......#....#....####........
+.......#.#......#..........#..#
+#.....##..#..#.....#....#...#..
+#............#........##.......
+.#.#...#.............#..##.....
+.#....#..#.#......#.##.......##
+...................##...##..###
+..#.....#...#................#.
+..#...#....#...#.#.#...#.....#.
+.....#............#....#...#..#
+.#.....#....#..#......#.#.....#
+............#.#.....####.##....
+....#......###....#...#....#...
+#.....#..#.....#..#...#.......#
+..#.#...#....#....##..#...##...
+.##..#..#..##....##...#........