summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-12-10 10:37:32 +0200
committerkartofen <mladenovnasko0@gmail.com>2022-12-10 10:37:32 +0200
commit9f57e15a054b16d335e9d5c49ddd7be829b4e272 (patch)
treed9f2bf025a58e262d65ffa28b173a06aee74b5a8
parentb284257038b6d4777de7c6ef5499e8b7c64b53b5 (diff)
day 10
-rwxr-xr-xAdvent-of-Code-2022/aoc-10/build.sh5
-rw-r--r--Advent-of-Code-2022/aoc-10/input.txt140
-rw-r--r--Advent-of-Code-2022/aoc-10/main.c86
-rw-r--r--Advent-of-Code-2022/aoc-10/sample.txt146
4 files changed, 377 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-10/build.sh b/Advent-of-Code-2022/aoc-10/build.sh
new file mode 100755
index 0000000..30f495e
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-10/build.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+set -xe
+
+gcc -o main main.c -Wall -Wextra -pedantic
diff --git a/Advent-of-Code-2022/aoc-10/input.txt b/Advent-of-Code-2022/aoc-10/input.txt
new file mode 100644
index 0000000..5796ef3
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-10/input.txt
@@ -0,0 +1,140 @@
+addx 2
+addx 15
+addx -11
+addx 6
+noop
+noop
+noop
+addx -1
+addx 5
+addx -1
+addx 5
+noop
+noop
+noop
+noop
+noop
+addx 7
+addx -1
+addx 3
+addx 1
+addx 5
+addx 1
+noop
+addx -38
+noop
+addx 1
+addx 6
+addx 3
+noop
+addx -8
+noop
+addx 13
+addx 2
+addx 3
+addx -2
+addx 2
+noop
+addx 3
+addx 9
+addx -2
+addx 2
+addx -10
+addx 11
+addx 2
+addx -14
+addx -21
+addx 2
+noop
+addx 5
+addx 29
+addx -2
+noop
+addx -19
+noop
+addx 2
+addx 11
+addx -10
+addx 2
+addx 5
+addx -9
+noop
+addx 14
+addx 2
+addx 3
+addx -2
+addx 3
+addx 1
+noop
+addx -37
+noop
+addx 13
+addx -8
+noop
+noop
+noop
+noop
+addx 13
+addx -5
+addx 3
+addx 3
+addx 3
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+addx 6
+addx 3
+addx 1
+addx 5
+addx -15
+addx 5
+addx -27
+addx 30
+addx -23
+addx 33
+addx -32
+addx 2
+addx 5
+addx 2
+addx -16
+addx 17
+addx 2
+addx -10
+addx 17
+addx 10
+addx -9
+addx 2
+addx 2
+addx 5
+addx -29
+addx -8
+noop
+noop
+noop
+addx 19
+addx -11
+addx -1
+addx 6
+noop
+noop
+addx -1
+addx 3
+noop
+addx 3
+addx 2
+addx -3
+addx 11
+addx -1
+addx 5
+addx -2
+addx 5
+addx 2
+noop
+noop
+addx 1
+noop
+noop
diff --git a/Advent-of-Code-2022/aoc-10/main.c b/Advent-of-Code-2022/aoc-10/main.c
new file mode 100644
index 0000000..526d5d0
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-10/main.c
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#if 0
+ #define PART part1
+#else
+ #define PART part2
+#endif
+
+#if 0
+ #define FILENAME "sample.txt"
+#else
+ #define FILENAME "input.txt"
+#endif
+
+#define CMDS 2
+char commands[CMDS][5] = {"noop", "addx"};
+char cmdcycles[CMDS] = {1, 2};
+
+int x = 1;
+int cycles = 0;
+int sum = 0;
+
+void part1()
+{
+ cycles++;
+
+ if((cycles+20) % 40 == 0) sum += cycles * x;
+}
+
+void part2()
+{
+ int i = cycles % 40;
+ printf("%c", (x == i || x == i+1 || x == i-1) ? '#' : '.');
+ if(i == 39) printf("\n");
+
+ cycles++;
+}
+
+int parse_cmd(char *cmdstr)
+{
+ for(int i = 0; i < CMDS; i++)
+ if(strncmp(commands[i], cmdstr, 4) == 0) return i;
+ return -1;
+}
+
+void parse()
+{
+ FILE *fp = fopen(FILENAME, "r");
+ if(!fp) {
+ fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME);
+ exit(1);
+ }
+
+ char line[256];
+ while(fgets(line, sizeof(line), fp))
+ {
+ int cmd = parse_cmd(strtok(line, " "));
+ assert(cmd >= 0);
+
+ for(int i = 0; i < cmdcycles[cmd]; i++)
+ {
+ PART();
+
+ switch(cmd) {
+ case 0:
+ break;
+ case 1:
+ if(i == 1) x += atoi(strtok(NULL, " "));
+ break;
+ }
+
+ }
+ }
+
+ fclose(fp);
+}
+
+int main(void)
+{
+ parse();
+ printf("%d\n", sum);
+ return 0;
+}
diff --git a/Advent-of-Code-2022/aoc-10/sample.txt b/Advent-of-Code-2022/aoc-10/sample.txt
new file mode 100644
index 0000000..37ee8ee
--- /dev/null
+++ b/Advent-of-Code-2022/aoc-10/sample.txt
@@ -0,0 +1,146 @@
+addx 15
+addx -11
+addx 6
+addx -3
+addx 5
+addx -1
+addx -8
+addx 13
+addx 4
+noop
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx -35
+addx 1
+addx 24
+addx -19
+addx 1
+addx 16
+addx -11
+noop
+noop
+addx 21
+addx -15
+noop
+noop
+addx -3
+addx 9
+addx 1
+addx -3
+addx 8
+addx 1
+addx 5
+noop
+noop
+noop
+noop
+noop
+addx -36
+noop
+addx 1
+addx 7
+noop
+noop
+noop
+addx 2
+addx 6
+noop
+noop
+noop
+noop
+noop
+addx 1
+noop
+noop
+addx 7
+addx 1
+noop
+addx -13
+addx 13
+addx 7
+noop
+addx 1
+addx -33
+noop
+noop
+noop
+addx 2
+noop
+noop
+noop
+addx 8
+noop
+addx -1
+addx 2
+addx 1
+noop
+addx 17
+addx -9
+addx 1
+addx 1
+addx -3
+addx 11
+noop
+noop
+addx 1
+noop
+addx 1
+noop
+noop
+addx -13
+addx -19
+addx 1
+addx 3
+addx 26
+addx -30
+addx 12
+addx -1
+addx 3
+addx 1
+noop
+noop
+noop
+addx -9
+addx 18
+addx 1
+addx 2
+noop
+noop
+addx 9
+noop
+noop
+noop
+addx -1
+addx 2
+addx -37
+addx 1
+addx 3
+noop
+addx 15
+addx -21
+addx 22
+addx -6
+addx 1
+noop
+addx 2
+addx 1
+noop
+addx -10
+noop
+noop
+addx 20
+addx 1
+addx 2
+addx 2
+addx -6
+addx -11
+noop
+noop
+noop