advent-of-code

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

commit d8a5a6058174fbd586c62ffeb1bff9463ec45dd1
parent eb315fd6ef44b8628a7ae4951bb73bc9df022052
Author: amin <dev@aminmesbah.com>
Date:   Fri,  3 Dec 2021 23:48:43 +0000

Solve 2021 day 3 part 1

FossilOrigin-Name: bf14727c78a0496fbbcd109ca60060c4eac1d91ad9441f952d54cb5623607b8d
Diffstat:
A2021/day03_01.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2021/day03_input.txt | 1000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 1090 insertions(+), 0 deletions(-)

diff --git a/2021/day03_01.c b/2021/day03_01.c @@ -0,0 +1,90 @@ +#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, "day03_input.txt"); + StrList lines = am_str_split(&a, input, (u8 *)"\n", 1); + + u64 num_columns = lines.first->s.size; + u64 *counts = AM_MEM_ARENA_PUSH_ARRAY(&a, u64, num_columns); + for (u64 i = 0; i < num_columns; i++) { + counts[i] = 0; + } + + for (StrListNode *number = lines.first; number; number = number->next) { + assert(number->s.size == num_columns); + for (u64 i = 0; i < num_columns; i++) { + counts[i] += number->s.str[i] == '1'; + } + } + + u64 num_numbers = lines.node_count; + assert(num_numbers % 2 == 0); + u64 gamma = 0; + for (u64 i = 0; i < num_columns; i++) { + assert(counts[i] != num_numbers / 2); + gamma |= (counts[i] > (num_numbers / 2)) << (num_columns - i - 1); + } + + u64 epsilon = (~gamma) & (0xFFFFFFFF >> (64 - num_columns)); + + printf("Power consumption: %" PRIu64 "\n", gamma * epsilon); + am_mem_arena_release(&a); + return 0; +} diff --git a/2021/day03_input.txt b/2021/day03_input.txt @@ -0,0 +1,1000 @@ +001110000001 +010100101000 +101101010010 +010111101010 +100011100110 +110100001011 +100010001100 +011110100110 +110011110000 +111010011001 +111100100011 +110101101010 +101001111100 +001110101110 +011100001110 +011000101101 +101100001000 +001010000111 +001100101001 +100001000011 +010010111011 +010101101010 +100111100001 +001011101000 +001110100100 +110001001011 +001101110111 +101100100010 +100110001111 +010111011111 +011111111101 +011111110011 +111011000001 +001000011110 +111001100001 +101111101100 +111000001011 +101110010111 +001111110001 +011110000010 +100101011101 +111000100010 +000110111100 +111010111100 +001110100011 +100111001100 +011111100101 +000010000110 +111111010011 +011000010011 +010101011011 +111110010000 +001100110110 +001111010100 +000100000100 +001100001011 +010010110010 +010010101101 +001111010101 +001111011000 +100101100000 +100101111100 +010010100011 +010001011101 +000101100010 +001101001100 +000001110111 +101101010000 +010100101101 +101011110100 +101010100111 +101001100100 +101001000010 +110110101110 +010010000111 +100001110110 +101011000010 +101000101000 +001101001011 +000011100010 +101111011101 +101100101110 +011001011101 +001101100110 +110101000011 +101001101100 +110000111111 +011000110010 +001101000011 +010001100111 +000101010110 +111100011100 +001011100011 +011110110100 +101000011101 +000010111010 +011011011101 +010100101110 +100110101011 +100110111001 +110000001010 +111000001110 +001110100110 +111010011011 +100000011000 +101010101110 +011000000001 +011011001011 +100110011011 +010101010000 +101111001101 +111100111010 +011110101111 +011100110000 +011010000011 +011010101011 +010110101011 +000000101010 +011101100101 +001001111111 +001000011000 +101001010101 +001010000000 +110110010101 +011100101101 +111010110011 +011000001101 +000000100000 +001110101111 +111010100011 +110101111101 +101001100101 +000111011011 +111011101011 +100110001101 +111100110000 +100000100110 +111100000111 +010011100101 +110011000000 +101001000100 +101001000110 +010111101000 +111100110100 +100111011100 +010000001011 +010100100001 +010100000100 +111000101100 +100100000101 +111111010000 +010101111010 +100000001011 +101101001000 +011100000101 +011001101011 +110100010101 +011010100010 +110110110010 +100001010000 +000100100111 +001111110011 +101111010000 +000110011101 +111110000101 +000101010011 +001011111101 +110111001111 +000011111110 +011111111000 +000100101100 +100100010100 +001011111110 +101000011000 +001011001000 +111110000111 +000100000011 +100001100001 +100001111010 +101001100111 +110010100110 +001111001001 +001111111011 +101100001111 +011100010111 +100001001001 +101101001011 +110110011010 +001110011110 +100100101000 +110110001111 +110011001101 +011011010100 +000011101010 +100000011010 +001010111000 +101001001100 +101100111101 +011010111000 +000100001001 +101101100001 +010010111101 +101110101001 +000100001101 +111101111000 +110110110111 +010101011010 +000010100100 +010000011100 +011100100110 +010001110000 +001010011000 +011101100111 +011001111001 +001101110000 +011010111010 +010100000101 +001011010110 +110011101111 +101010001100 +111111101000 +111001110000 +100010001000 +111101110001 +011001110100 +111010111000 +110010110010 +100100001001 +000000100100 +100010111111 +011001000010 +101110001001 +001101101011 +000111000101 +110000011000 +110010011111 +001010011110 +110111001110 +001111110010 +111011111110 +100010101101 +000010101000 +001100001100 +011011111010 +111110001010 +110101100111 +110111011011 +101110111000 +010001001011 +101011100101 +011100000000 +011110000110 +111001111001 +101111110111 +110110100110 +011111010111 +010110000100 +011011101100 +010011111001 +101001011100 +010000010101 +011110011001 +100011101111 +010000101111 +001101011001 +011100011100 +011111010101 +101011011001 +011110011011 +001010101111 +110000000100 +010010011010 +010110000110 +011111100001 +011011000110 +101011010011 +011001101001 +001100001001 +010011001011 +111111110110 +000111001011 +110011111100 +010010000001 +001110111000 +000110010101 +101011100111 +000101010111 +101101111011 +111100000010 +001110011111 +001100111010 +001010100011 +000000011000 +101100010101 +100011110111 +001000110011 +100010110010 +111100110101 +001010011010 +000000100011 +001001101010 +000010000001 +111001000010 +110111111101 +101001110011 +010001011000 +100001010101 +101111101111 +000011000111 +101110000101 +011000111110 +111010010000 +100111001001 +011111111100 +111111001011 +011101110101 +001100101010 +001111101000 +111101011111 +011001000111 +011101110010 +000011100000 +110101101011 +011110001010 +101001100001 +001101110110 +100101001100 +111011100101 +001101100100 +010010110001 +001000101111 +100110110001 +111001100110 +101110111101 +001010101100 +010011110111 +100000110011 +100101100100 +110111101011 +000111000111 +110100101000 +001110001001 +101110001010 +101110110010 +111110010100 +001110000110 +000011100100 +000101000010 +000011011100 +011111000110 +110011010111 +100101001111 +110101000010 +101011001101 +111110101001 +010011111000 +101111011011 +101111100100 +000110011001 +100011111001 +001110011100 +011100001001 +110111100001 +111011011010 +111001111011 +001100101000 +011000100000 +100011001110 +011110111001 +011011111110 +001000110010 +110000010001 +010001110110 +100110101100 +100111100101 +000111111000 +001101011011 +000111010110 +010100001101 +001010000101 +110010101101 +001111001111 +000001111001 +101010110111 +100110000100 +001100111011 +000010100001 +000100111111 +100001111111 +000011111000 +101110011010 +001011010001 +001011110001 +010011100110 +000010110010 +001010101001 +110110111100 +100111010110 +101001011001 +000100111110 +101111001010 +110100011111 +010100011100 +111110100011 +000010101100 +100000101010 +111111000000 +111111010110 +001100011011 +110001101001 +101010011110 +101010000010 +000000000111 +000100101110 +101000001011 +100011000001 +000100101101 +001000100000 +110111001011 +111001001111 +110011100110 +100001110100 +010111101100 +011000001010 +100101000000 +001101100001 +110001000111 +010111100011 +000100111101 +010111011101 +000001010111 +011001111110 +110101111001 +100010101001 +010110001010 +111010110001 +000000101001 +010100011110 +000010100010 +100011001000 +100110010111 +001011010100 +011010001010 +000001110101 +010010100000 +010111100101 +010101011101 +110100010010 +101101111100 +100001101101 +100011010110 +011011000101 +011100110101 +010010010100 +101001001110 +001110110010 +010110110101 +100100010101 +000011010100 +010000111000 +101110010011 +101101110111 +110000000010 +001011001010 +000111010011 +100110100010 +001110001111 +100110101110 +010001010010 +111001011001 +111000110111 +010111000001 +001111011010 +001000101010 +111011110001 +100001001110 +001010001011 +000011100001 +101010011101 +000110000110 +100111110010 +010010100001 +010101011000 +010001001100 +011100110100 +000010101010 +001110111001 +001100010011 +000101000101 +100010111000 +111011010011 +100011110100 +110100011000 +000010011111 +011111011001 +011011001111 +111101110000 +101111000110 +100111100000 +111100011101 +001100010000 +001100110101 +011010110100 +010000010100 +111010101111 +000010101101 +010110010110 +110011111110 +100101101001 +111100000000 +110111101100 +100100001000 +100100001011 +111001001011 +011010110011 +001101110011 +000100101001 +001111010011 +011000000110 +111100100110 +010010001001 +110101011000 +011001111111 +100111010101 +111010001000 +001111111001 +001100100110 +110010100010 +011101001000 +110111100000 +011010101000 +000101111010 +101010010110 +100111101010 +000010110000 +100000001100 +101000010000 +010101000011 +100111101100 +101000110110 +010010110110 +111011100011 +111100111011 +100010101111 +011011110111 +100001011110 +110000100101 +011000111101 +010110000001 +100010011110 +001111011100 +110001011110 +111110100101 +001110000111 +110111100011 +110100101010 +010101110001 +110101011101 +110110100101 +110010000100 +100110110100 +010100011101 +111101010101 +010101010011 +100100011000 +101100010110 +001000011111 +010011110001 +100101100001 +101100000101 +000100011010 +100100100011 +000101001111 +110100100101 +001111000010 +100000001110 +110100000001 +001010010000 +010001000110 +101010110001 +100010100001 +111011101101 +011011111011 +010011111010 +111100001001 +001101110010 +111110111011 +101110010100 +001110110111 +110000000110 +000011111100 +001110010000 +000010001001 +101101101110 +010101100110 +010000110000 +101111100011 +101000001111 +001100100111 +111111000010 +110000000101 +110001010100 +010011011000 +110100101111 +110100010100 +101000001000 +111010110111 +101000110001 +111110000110 +001111100000 +100100000110 +101011000100 +100100010110 +110001011001 +011011010010 +001110111011 +010100111111 +010000110100 +111011010001 +001011001110 +110110101011 +100110010000 +001111000100 +101101101011 +110110010110 +010011101110 +000110101000 +001010010111 +101111000010 +001101101111 +111001111100 +101010101011 +111101100010 +110101000110 +011110000111 +010101110100 +100110111100 +010001010101 +001101011110 +110100010001 +100100011001 +101111111111 +001001001111 +011011111000 +101010111100 +000011011110 +111010010001 +111111011011 +111101110111 +100011111000 +110001101101 +001011011000 +001000101011 +010000000101 +011011000100 +100001011011 +001001001101 +000110001101 +000011011000 +000011000001 +000010010111 +011011101001 +110101001111 +011000101010 +011110100101 +100000110101 +011011100110 +110010010010 +011110111011 +000101101010 +001010111110 +100101101111 +111110001100 +010100110000 +110111111100 +010010010000 +010011000001 +100101010011 +001110100010 +110101010010 +100000010110 +001101100111 +010110011011 +010111000101 +100110101101 +010010101010 +000101011100 +101000001101 +110111000010 +101000110010 +010111010001 +100011001011 +111111100010 +001101110101 +011100111111 +110001101111 +011101101010 +001000100011 +011001110111 +100111000110 +111011111111 +001000111001 +011100011110 +110010100011 +111101111101 +011011011110 +100000010000 +000001110100 +110110001000 +111111011010 +011110111010 +111000011100 +100101110010 +011100001100 +010011000111 +011110001101 +000101101000 +001011011010 +011011110000 +111100010010 +000010101111 +011101101111 +001001110000 +110111011100 +101000010001 +100110000110 +000010000101 +001000101110 +001001001001 +011111010000 +111100100101 +010101110111 +010110000011 +110000111011 +100100110111 +100111000001 +111100010011 +100010000100 +111011100000 +010111110100 +101100001011 +000010101001 +111110111101 +001011110010 +110000101110 +000010111110 +110111011001 +110111110100 +001110100001 +001101110001 +010001110011 +110101111000 +000110101100 +111011010100 +000100001100 +110001110101 +100000111110 +100010010111 +100101010001 +011011000111 +111011110110 +001001101001 +010011110101 +111110100111 +000110100001 +110011001110 +101001101110 +100100110100 +000001001001 +011111110100 +010001101110 +100101010111 +110011010100 +011101010010 +100010101011 +011111101101 +101100101000 +111110000001 +101010111010 +101110111001 +101011000111 +110100110010 +101011000001 +111001000001 +001011110000 +000110011010 +101111101001 +101110000000 +011101010001 +110110011101 +110101011111 +101110000001 +111100001111 +000111100101 +010001101101 +000011011010 +011010001111 +111010001101 +101011001001 +111001101001 +001100001101 +111110100010 +000111110101 +010111101110 +010101100000 +100010100011 +000001100001 +111111000110 +001111110100 +000001101100 +011011011100 +011110001011 +001010101000 +101111100110 +111000111011 +100101101000 +100110001010 +010111010110 +110110001101 +010001001111 +000011110011 +010000111110 +101000011001 +111000000100 +011010000111 +010010101110 +001100001111 +000010001000 +001101101001 +110101101110 +110010011011 +000101011000 +101011101000 +010000110101 +110110001010 +110110011110 +011111011100 +001001011011 +110000111100 +110101001100 +100100101110 +001000111100 +111101011001 +011111011010 +011101101100 +100011010000 +001100000001 +001011000111 +011011111100 +001100101101 +110010101111 +011101111100 +011110101101 +110100011010 +101100100001 +000111111100 +001010110010 +110101101100 +111100000101 +110011000011 +110100111010 +011000111010 +101000000101 +101010001000 +000100110111 +110101101000 +101001111101 +010010001010 +110111101001 +010111001000 +110101100011 +111111100000 +100010010010 +010011000100 +010100100110 +101101000001 +001010010001 +111100101110 +110011100001 +101000000111 +111010100110 +111111010111 +110010101011 +001011010000 +001001100110 +011111110110 +000010110100 +110010000111 +111001100011 +101101001110 +011001111101 +100111110111 +000100000111 +010000110011 +000110111111 +011011100001 +101001001011 +110011101001 +011110010010 +001010101011 +110000111001 +010100111110 +011110101110 +011001111100 +111111001100 +011000001111 +000001100100 +101010001011 +000010111001 +000101110111 +101100011111 +101001000111 +111001011100 +111010100111 +111011010010 +101101111010 +111100011001 +101111101101 +001010000110 +100001010111 +011101001101 +010000101001 +011110101011 +011101000111 +000101100001 +010011111011 +101101111110 +001000110100 +011100000110 +100010000110 +011110010011 +000011110000 +100000010011 +011100011010 +111110011110 +000111011000 +000000001000 +001010110101 +110011111010 +001101100000 +001000000110 +110111111011 +001000010110 +001010000011 +101101011010 +011001100101 +001000010111 +100010111010 +011101101001 +101010000111 +001100111111 +111000000111 +001111011111 +000100001010 +101111111000 +010111100001 +010110010111 +111001010011 +001111001011 +000001010101 +100111111101 +010110100100 +011101010110 +110001010010 +010110010101 +010101000101 +100010111011 +001011101010 +011011110001 +100111110001 +000001111101 +111111111011 +000010010110 +101011010000 +010011010011 +110000001111 +010010001100 +100101001011 +101111001100 +111111001110 +010100101001 +010001111110 +011010011100 +111100011110 +010110100010 +100011011001 +001110000100 +011001111000 +110010010001 +010011001110 +011010100111 +010100101100 +101001011011 +110011110001 +100011000000 +000011111101 +000010110110 +001011011110 +010001011110 +001101010010