advent-of-code

Solutions for Advent of Code.
git clone git://git.amin.space/advent-of-code.git
Log | Files | Refs | LICENSE

commit 82496552846aff445e040d4d4624366b84b366ad
parent 70701b9b997344026cbbbc5ff2c14c1a513aa3d1
Author: amin <dev@aminmesbah.com>
Date:   Fri,  3 Dec 2021 22:53:44 +0000

Solve 2021 day 2 part 1

FossilOrigin-Name: 07ed5af19e88c861c798d772d59613b44f0f2d611d4408a32e8ce8b278545fa6
Diffstat:
A2021/day02_01.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day02_input.txt | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 1102 insertions(+), 0 deletions(-)

diff --git a/2021/day02_01.c b/2021/day02_01.c @@ -0,0 +1,102 @@ +#if 0 +# Self-building c file. Invoke like: `./file.c` +outdir=out +input=$(basename "$0") +output="$outdir"/$(basename "$0" .c) +if [ "$input" -nt "$output" ]; +then + mkdir --parents "$outdir" || exit + echo "Building ${output}." || exit + clang -std=c11 -Wall -Wextra -pedantic -Wno-unused-function "$input" -o "$output" || exit +fi +if [ "$1" = "-r" ]; +then + ./"$output" "$@" +fi +exit +#endif + +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> + +_Noreturn void assert_fail(const char *expr, const char *file, int line, const char *func) { + fprintf(stderr, "%s:%d: %s: Assertion failed: '%s'\n", file, line, func, expr); + abort(); +} + +#define AM_ENABLE_ASSERT 1 +#define AM_ASSERT_FAIL(expr, file, line, func) assert_fail(expr, file, line, func) +#include "am_base.h" +#include "am_memory.h" +#include "am_list.h" +#include "am_string.h" + +Str open_file(MemArena *arena, char *path) { + FILE *f = fopen(path, "r"); + assert(f); + + s32 error = fseek(f, 0L, SEEK_END); + assert(!error); + + s64 size = ftell(f); + assert(size >= 0); + rewind(f); + + u8 *buf = am_mem_arena_push(arena, size); + assert(buf); + + size_t items_read = fread(buf, 1, size, f); + assert(items_read == (size_t)size); + + error = fclose(f); + assert(!error); + + return am_str(buf, size); +} + +int main(void) { + MemArena a = am_mem_arena_create(am_mem_base_allocator_malloc()); + Str input = open_file(&a, "day02_input.txt"); + StrList tokens = am_str_split(&a, input, (u8 *)"\n ", 2); + + struct { + s64 horizontal; + s64 depth; + } position = {0}; + + s64 *dimension = NULL; + s64 sign = 0; + bool expect_command = true; + + for (StrListNode *token = tokens.first; token; token = token->next) { + if (expect_command) { + if (am_str_eq(token->s, AM_STR_LIT("forward"))) { + dimension = &position.horizontal; + sign = 1; + } else if (am_str_eq(token->s, AM_STR_LIT("up"))) { + dimension = &position.depth; + sign = -1; + } else if (am_str_eq(token->s, AM_STR_LIT("down"))) { + dimension = &position.depth; + sign = 1; + } else { + assert(false); + } + expect_command = false; + } else { + assert(dimension); + assert(sign == -1 || sign == 1); + assert(token->s.size == 1); + assert(token->s.str[0] >= '0' && token->s.str[0] <= '9'); + *dimension += sign * (token->s.str[0] - '0'); + expect_command = true; + } + } + + printf("Multiplied position dimensions: %" PRId64 "\n", position.horizontal * position.depth); + + am_mem_arena_release(&a); + return 0; +} diff --git a/2021/day02_input.txt b/2021/day02_input.txt @@ -0,0 +1,1000 @@ +forward 1 +forward 2 +down 5 +down 5 +down 4 +down 9 +up 6 +up 7 +down 2 +forward 9 +up 4 +forward 7 +forward 9 +up 3 +down 1 +forward 5 +down 3 +forward 3 +forward 3 +forward 3 +down 2 +down 5 +forward 7 +down 7 +up 7 +down 9 +down 1 +down 4 +down 9 +down 2 +forward 2 +forward 4 +up 7 +up 1 +forward 3 +forward 8 +forward 9 +forward 6 +forward 9 +forward 1 +forward 5 +down 9 +up 7 +down 9 +forward 2 +forward 9 +forward 1 +forward 5 +up 8 +down 5 +forward 4 +up 6 +up 9 +forward 2 +up 8 +down 1 +up 5 +forward 3 +down 1 +forward 6 +up 6 +forward 9 +forward 1 +forward 3 +down 4 +down 9 +down 8 +up 9 +down 9 +down 2 +down 4 +forward 2 +down 4 +down 2 +down 8 +up 3 +up 9 +forward 3 +down 5 +down 1 +up 6 +up 6 +down 4 +up 3 +forward 1 +down 2 +down 7 +forward 1 +down 4 +forward 5 +down 5 +forward 3 +forward 8 +down 4 +forward 3 +forward 2 +down 4 +forward 6 +forward 6 +down 9 +down 3 +up 7 +up 6 +down 8 +down 4 +down 4 +down 8 +down 4 +forward 5 +up 7 +down 8 +down 4 +down 5 +down 3 +forward 1 +up 1 +forward 9 +forward 4 +down 9 +forward 5 +up 4 +down 3 +up 7 +up 2 +down 5 +down 2 +forward 8 +up 1 +down 8 +forward 2 +forward 8 +down 9 +forward 3 +forward 7 +forward 1 +down 2 +down 8 +forward 1 +forward 9 +forward 9 +up 3 +up 7 +forward 9 +down 4 +up 6 +forward 2 +down 2 +down 4 +down 4 +down 6 +down 5 +forward 2 +down 8 +down 1 +up 6 +up 1 +up 4 +down 5 +forward 5 +forward 4 +forward 1 +forward 9 +up 9 +down 9 +down 5 +down 7 +up 6 +up 2 +forward 5 +down 5 +down 3 +down 8 +down 6 +forward 4 +down 9 +up 3 +forward 2 +forward 9 +forward 6 +forward 5 +down 5 +down 1 +down 2 +forward 9 +down 2 +down 2 +down 3 +forward 3 +forward 9 +forward 1 +down 3 +down 8 +forward 7 +down 9 +forward 4 +up 3 +up 7 +up 4 +down 5 +forward 9 +forward 2 +forward 2 +down 3 +up 5 +down 5 +down 4 +forward 2 +forward 7 +up 2 +down 8 +up 2 +up 2 +forward 2 +down 2 +forward 3 +down 3 +up 8 +forward 7 +up 5 +forward 4 +down 6 +down 8 +forward 4 +forward 3 +up 9 +up 2 +down 2 +down 5 +down 8 +down 1 +forward 9 +down 6 +forward 1 +down 9 +down 4 +up 6 +up 3 +forward 1 +down 8 +up 3 +down 6 +down 7 +down 2 +forward 5 +down 6 +forward 8 +up 7 +down 5 +down 3 +forward 4 +forward 5 +forward 3 +forward 4 +forward 6 +forward 2 +forward 1 +down 3 +down 5 +down 3 +down 5 +forward 4 +down 7 +forward 8 +forward 5 +up 7 +up 3 +forward 9 +up 1 +forward 9 +up 8 +down 3 +forward 1 +forward 6 +forward 9 +down 1 +up 9 +forward 5 +down 6 +forward 8 +down 7 +down 3 +up 4 +down 6 +forward 5 +forward 6 +forward 5 +up 2 +down 5 +up 7 +up 4 +up 5 +forward 3 +down 9 +up 4 +forward 9 +forward 8 +forward 6 +down 5 +down 4 +down 2 +up 5 +up 7 +up 2 +forward 9 +forward 9 +down 9 +down 4 +up 2 +forward 3 +up 3 +up 2 +down 9 +forward 8 +forward 6 +down 6 +forward 3 +down 1 +forward 4 +forward 9 +forward 5 +down 2 +down 7 +up 6 +down 3 +forward 7 +down 3 +up 3 +up 8 +forward 3 +up 7 +forward 5 +down 7 +forward 7 +forward 3 +down 6 +down 3 +forward 5 +forward 9 +up 8 +down 7 +down 3 +down 7 +down 4 +down 1 +down 7 +up 6 +forward 8 +up 7 +down 9 +forward 6 +down 4 +forward 6 +up 9 +forward 4 +down 5 +up 3 +forward 5 +forward 6 +down 8 +up 9 +forward 4 +up 5 +forward 4 +forward 2 +forward 8 +down 7 +forward 1 +down 8 +forward 8 +forward 4 +down 4 +forward 5 +down 2 +down 5 +forward 9 +down 7 +forward 1 +down 1 +forward 9 +forward 3 +forward 9 +forward 8 +down 5 +down 6 +forward 8 +up 9 +forward 7 +down 1 +forward 9 +up 7 +forward 2 +forward 6 +forward 1 +down 8 +down 6 +down 7 +down 6 +up 5 +down 5 +forward 9 +down 6 +down 9 +forward 9 +down 7 +up 7 +forward 1 +down 5 +down 8 +up 5 +down 6 +up 5 +up 7 +forward 3 +forward 2 +down 5 +down 6 +forward 3 +down 4 +down 5 +up 4 +forward 5 +down 4 +down 5 +forward 4 +down 1 +forward 1 +down 1 +forward 4 +forward 2 +down 3 +forward 1 +down 1 +forward 2 +forward 2 +forward 6 +up 5 +forward 5 +down 9 +down 1 +forward 7 +up 9 +down 2 +down 1 +forward 3 +up 5 +down 8 +forward 2 +forward 1 +down 7 +forward 5 +up 6 +forward 2 +up 5 +forward 8 +up 6 +down 6 +down 3 +forward 1 +forward 7 +down 7 +down 1 +up 7 +forward 2 +up 5 +forward 4 +forward 9 +forward 4 +forward 2 +forward 4 +down 3 +down 7 +forward 1 +up 9 +up 2 +forward 1 +down 5 +up 9 +forward 6 +down 7 +down 2 +down 7 +down 2 +down 1 +down 7 +down 6 +up 1 +up 4 +down 9 +up 3 +forward 1 +down 2 +forward 4 +up 4 +up 9 +down 4 +forward 6 +down 1 +down 1 +down 8 +up 5 +forward 1 +up 6 +down 5 +forward 4 +up 8 +down 4 +forward 4 +forward 3 +down 7 +down 1 +forward 3 +forward 1 +up 6 +down 1 +down 8 +forward 6 +down 4 +down 6 +forward 5 +forward 3 +forward 5 +down 4 +forward 7 +down 6 +down 6 +down 9 +up 9 +forward 5 +up 9 +up 4 +up 6 +down 4 +forward 3 +up 2 +down 7 +down 8 +forward 7 +down 4 +down 3 +down 5 +down 1 +forward 5 +up 4 +down 3 +down 3 +down 6 +forward 9 +down 1 +forward 4 +down 9 +forward 1 +forward 4 +down 1 +up 5 +down 6 +forward 5 +up 5 +forward 5 +down 8 +down 1 +down 8 +up 1 +down 1 +forward 8 +forward 3 +up 2 +forward 9 +forward 1 +forward 3 +down 2 +down 7 +down 2 +up 4 +up 3 +down 2 +forward 2 +forward 9 +forward 8 +down 8 +forward 3 +up 9 +up 6 +down 9 +down 1 +up 3 +up 2 +forward 2 +down 6 +up 2 +up 1 +down 9 +down 3 +down 6 +up 7 +up 5 +forward 8 +down 1 +forward 7 +down 6 +down 1 +up 9 +forward 9 +forward 8 +down 3 +down 9 +down 5 +forward 7 +up 1 +up 4 +up 4 +down 7 +down 1 +up 2 +down 2 +forward 8 +forward 7 +up 8 +down 1 +down 8 +up 7 +forward 1 +down 9 +forward 7 +forward 1 +down 4 +down 8 +down 1 +forward 5 +forward 8 +forward 5 +down 8 +down 7 +up 5 +forward 8 +down 5 +up 9 +down 5 +down 9 +forward 2 +forward 6 +forward 2 +up 1 +forward 4 +forward 9 +forward 7 +down 7 +down 3 +forward 9 +forward 6 +up 5 +forward 5 +forward 7 +down 9 +forward 6 +down 7 +forward 5 +forward 5 +forward 4 +forward 1 +forward 1 +up 7 +forward 3 +up 3 +forward 6 +up 3 +down 9 +forward 9 +up 6 +up 3 +forward 2 +down 2 +forward 9 +down 7 +up 7 +forward 6 +forward 2 +down 2 +down 4 +forward 1 +forward 4 +down 4 +up 9 +down 4 +down 4 +down 3 +forward 6 +forward 3 +down 3 +forward 5 +forward 7 +up 4 +forward 1 +forward 5 +forward 2 +forward 5 +forward 5 +forward 2 +up 8 +down 7 +up 7 +down 7 +forward 1 +forward 5 +forward 3 +forward 7 +forward 5 +forward 6 +up 7 +forward 3 +down 7 +down 2 +up 9 +forward 6 +down 7 +forward 9 +up 8 +down 1 +up 8 +up 2 +down 7 +down 6 +down 5 +up 7 +forward 6 +down 5 +forward 7 +down 6 +down 2 +up 3 +up 7 +up 5 +forward 9 +forward 2 +down 1 +down 5 +up 9 +forward 8 +down 7 +forward 1 +up 6 +down 1 +down 3 +forward 3 +forward 6 +down 4 +forward 8 +up 2 +down 8 +up 4 +up 9 +forward 8 +down 9 +forward 3 +forward 7 +down 5 +forward 4 +up 3 +up 1 +forward 7 +down 6 +up 4 +down 3 +forward 8 +down 9 +forward 2 +down 8 +forward 9 +up 7 +forward 2 +up 1 +down 7 +down 1 +forward 4 +forward 5 +down 4 +down 9 +forward 2 +down 9 +down 5 +up 2 +down 6 +forward 8 +up 6 +down 3 +down 5 +down 1 +up 7 +down 5 +forward 8 +up 4 +down 7 +down 4 +down 4 +down 2 +down 3 +down 1 +down 7 +forward 4 +down 4 +down 7 +down 7 +down 9 +up 1 +up 7 +forward 4 +up 7 +forward 6 +forward 8 +forward 2 +forward 8 +up 8 +down 3 +forward 7 +down 9 +forward 9 +forward 6 +up 3 +down 4 +down 3 +forward 6 +forward 2 +forward 3 +down 8 +forward 1 +forward 5 +up 3 +down 8 +forward 7 +forward 4 +down 3 +forward 2 +down 9 +down 9 +forward 9 +down 7 +forward 6 +forward 4 +down 5 +forward 9 +forward 3 +down 1 +down 1 +down 7 +forward 8 +down 3 +forward 7 +forward 8 +up 3 +forward 8 +forward 8 +up 6 +forward 2 +down 3 +down 4 +forward 9 +up 8 +down 9 +forward 5 +down 3 +up 7 +forward 5 +down 2 +forward 2 +forward 1 +down 6 +down 1 +up 4 +forward 4 +down 7 +up 3 +down 3 +forward 4 +forward 2 +forward 1 +forward 9 +forward 7 +forward 9 +down 1 +forward 6 +down 5 +up 7 +down 9 +down 2 +up 4 +down 2 +down 2 +down 1 +down 2 +down 5 +down 4 +down 6 +forward 4 +forward 9 +forward 6 +forward 7 +up 9 +up 2 +forward 7 +forward 9 +up 5 +forward 2 +up 5 +up 9 +forward 9 +down 8 +forward 6 +down 8 +forward 4 +down 1 +down 4 +forward 4 +down 3 +forward 4 +forward 3 +forward 9 +down 5 +forward 3 +forward 2 +forward 9 +forward 8 +down 2 +forward 5 +up 2 +forward 1 +down 3 +forward 9 +forward 8 +down 9 +forward 5 +forward 1 +down 1 +down 9 +down 6 +forward 8 +forward 1 +down 5 +up 5 +forward 8 +up 4 +down 6 +forward 1 +up 2 +down 3 +down 1 +down 8 +down 2 +up 6 +down 5 +down 2 +down 3 +forward 2 +up 7 +down 9 +up 1 +up 1 +forward 7 +forward 4 +down 7 +up 7 +down 7 +forward 5 +up 2 +down 7 +forward 9 +down 7 +up 4 +forward 2 +forward 1 +up 6 +down 8 +up 6 +down 2 +down 4 +up 8 +up 8 +up 5 +down 6 +up 6 +down 5 +up 1 +down 1 +forward 6 +up 7 +forward 8 +up 9 +down 8 +up 7 +forward 9 +up 4 +down 5 +forward 3 +forward 6 +forward 4 +forward 4 +down 7 +forward 9 +down 6 +down 2 +forward 9 +forward 3