commit 9695f4ac3efce74cd6473751dc766081acd8e8ef
parent 82e914344d52f9b4c56f83742a0d9dfa495a6e75
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Sun, 11 Dec 2022 23:23:10 -0500
Day 10 challenge complete
Diffstat:
A | day10/input.txt | | | 146 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | day10/main.c | | | 78 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 224 insertions(+), 0 deletions(-)
diff --git a/day10/input.txt b/day10/input.txt
@@ -0,0 +1,146 @@
+noop
+noop
+addx 15
+addx -10
+noop
+noop
+addx 3
+noop
+noop
+addx 7
+addx 1
+addx 4
+addx -1
+addx 1
+addx 5
+addx 1
+noop
+noop
+addx 5
+addx -1
+noop
+addx 3
+noop
+addx 3
+addx -38
+noop
+addx 3
+addx 2
+addx 5
+addx 2
+addx 26
+addx -21
+addx -2
+addx 5
+addx 2
+addx -14
+addx 15
+noop
+addx 7
+noop
+addx 2
+addx -22
+addx 23
+addx 2
+addx 5
+addx -40
+noop
+noop
+addx 3
+addx 2
+noop
+addx 24
+addx -19
+noop
+noop
+noop
+addx 5
+addx 5
+addx 2
+noop
+noop
+noop
+noop
+addx 7
+noop
+addx 3
+noop
+addx 3
+addx -2
+addx 2
+addx 5
+addx -38
+noop
+noop
+noop
+addx 5
+addx 2
+addx -1
+addx 2
+addx 30
+addx -23
+noop
+noop
+noop
+noop
+addx 3
+addx 5
+addx -11
+addx 12
+noop
+addx 6
+addx 1
+noop
+addx 4
+addx 3
+noop
+addx -40
+addx 4
+addx 28
+addx -27
+addx 5
+addx 2
+addx 5
+noop
+noop
+addx -2
+addx 2
+addx 5
+addx 3
+noop
+addx 2
+addx -25
+addx 30
+noop
+addx 3
+addx -2
+addx 2
+addx 5
+addx -39
+addx 29
+addx -27
+addx 5
+noop
+noop
+noop
+addx 4
+noop
+addx 1
+addx 2
+addx 5
+addx 2
+noop
+noop
+noop
+noop
+addx 5
+addx 1
+noop
+addx 2
+addx 5
+addx -32
+addx 34
+noop
+noop
+noop
+noop
diff --git a/day10/main.c b/day10/main.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <err.h>
+
+int
+sampleValues(int x, int cycle)
+{
+ // Could compact this with cycle - 20 % 40
+ switch (cycle)
+ {
+ case 20:
+ case 60:
+ case 100:
+ case 140:
+ case 180:
+ case 220: return x * cycle;
+ }
+ return 0;
+}
+
+void
+drawPixel(int x, int cycle)
+{
+ if ((cycle - 1) % 40 >= x - 1 && (cycle - 1) % 40 <= x + 1)
+ printf("#");
+ else
+ printf(" ");
+
+ if (cycle % 40 == 0)
+ printf("\n");
+}
+
+int
+main (void)
+{
+ FILE *fp = fopen("input.txt", "r");
+ if (fp == NULL)
+ {
+ err(1, "Failed to open input.txt");
+ exit(EXIT_FAILURE);
+ }
+
+
+ int x = 1;
+ int cycle = 0;
+ int change = 0;
+ int sum = 0;
+
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen = 0;
+ while ((linelen = getline(&line, &linesize, fp)) != -1)
+ {
+ line[--linelen] = '\0'; // cut off the newline
+ change = atoi(&line[5]);
+
+ cycle++;
+ drawPixel(x, cycle);
+ sum += sampleValues(x, cycle);
+
+ if (line[0] == 'a')
+ {
+ cycle++;
+ drawPixel(x, cycle);
+ sum += sampleValues(x, cycle);
+ x += change;
+ }
+ }
+ free(line);
+ if (ferror(fp))
+ err(1, "getline");
+ fclose(fp);
+
+ printf("PART 1: %d\n", sum);
+
+ exit(EXIT_SUCCESS);
+}