advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

commit ebc174add5feef05162d239ae56e69392aa91edf
parent 6bd8091226007557b581f55137cdd17f2e494aea
Author: aabacchus <ben@bvnf.space>
Date:   Sat, 10 Dec 2022 11:46:22 +0000

22.10 in C

Diffstat:
A2022/10/a.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/10/input | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 220 insertions(+), 0 deletions(-)

diff --git a/2022/10/a.c b/2022/10/a.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> + +int +check_clock(int c, int X) { + switch (c+1) { + case 20: + case 60: + case 100: + case 140: + case 180: + case 220: + return (c+1) * X; + default: + return 0; + } +} + +void +draw(int c, int X) { + int p = (c) % 40; + if (p == 0 && c != 0) + printf("\n"); + if ((X-1) % 40 == p || + (X) % 40 == p || + (X+1) % 40 == p) + printf("#"); + else + printf("."); +} + +int +main(int argc, char **argv) { + char *buf = NULL; + size_t buflen = 0; + ssize_t n; + FILE *f; + + int X = 1; + int clock = 0; + int total = 0; + + if (argc != 2) { + fprintf(stderr, "usage: %s input\n", argv[0]); + return 1; + } + + f = fopen(argv[1], "r"); + if (f == NULL) { + perror(argv[1]); + return 1; + } + + draw(clock, X); + while ((n = getline(&buf, &buflen, f)) != -1) { + if (buf[n - 1] == '\n') { + buf[n - 1] = '\0'; + n--; + } + if (buf[0] == 'a') { + int a; + if (sscanf(buf+5, "%d", &a) != 1) { + perror("sscanf"); + return 1; + } + clock++; + total += check_clock(clock, X); + draw(clock, X); + X += a; + } + clock++; + total += check_clock(clock, X); + draw(clock, X); + } + + printf("Part A: %d\n", total); + + free(buf); + fclose(f); + + return 0; +} diff --git a/2022/10/input b/2022/10/input @@ -0,0 +1,138 @@ +noop +noop +addx 5 +addx 29 +addx -28 +addx 5 +addx -1 +noop +noop +addx 5 +addx 12 +addx -6 +noop +addx 4 +addx -1 +addx 1 +addx 5 +addx -31 +addx 32 +addx 4 +addx 1 +noop +addx -38 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx -32 +addx 33 +addx -20 +addx 27 +addx -39 +addx 1 +noop +addx 5 +addx 3 +noop +addx 2 +addx 5 +noop +noop +addx -2 +addx 5 +addx 2 +addx -16 +addx 21 +addx -1 +addx 1 +noop +addx 3 +addx 5 +addx -22 +addx 26 +addx -39 +noop +addx 5 +addx -2 +addx 2 +addx 5 +addx 2 +addx 23 +noop +addx -18 +addx 1 +noop +noop +addx 2 +noop +noop +addx 7 +addx 3 +noop +addx 2 +addx -27 +addx 28 +addx 5 +addx -11 +addx -27 +noop +noop +addx 3 +addx 2 +addx 5 +addx 2 +addx 27 +addx -26 +addx 2 +addx 5 +addx 2 +addx 4 +addx -3 +addx 2 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +noop +addx -33 +noop +noop +noop +noop +addx 31 +addx -26 +addx 6 +noop +noop +addx -1 +noop +addx 3 +addx 5 +addx 3 +noop +addx -1 +addx 5 +addx 1 +addx -12 +addx 17 +addx -1 +addx 5 +noop +noop +addx 1 +noop +noop