advent-of-code

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

day03_01.c (2395B)


      1 #if 0
      2 # Self-building c file. Invoke like: `./file.c`
      3 outdir=out
      4 input=$(basename "$0")
      5 output="$outdir"/$(basename "$0" .c)
      6 if [ "$input" -nt "$output" ];
      7 then
      8     mkdir --parents "$outdir" || exit
      9     echo "Building ${output}." || exit
     10     clang -std=c11 -Wall -Wextra -pedantic -Wno-unused-function -I../../amlibs/ "$input" -o "$output" || exit
     11 fi
     12 if [ "$1" = "-r" ];
     13 then
     14     ./"$output" "$@"
     15 fi
     16 exit
     17 #endif
     18 
     19 #include <errno.h>
     20 #include <inttypes.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 
     24 _Noreturn void assert_fail(const char *expr, const char *file, int line, const char *func) {
     25     fprintf(stderr, "%s:%d: %s: Assertion failed: '%s'\n", file, line, func, expr);
     26     abort();
     27 }
     28 
     29 #define AM_ENABLE_ASSERT 1
     30 #define AM_ASSERT_FAIL(expr, file, line, func) assert_fail(expr, file, line, func)
     31 #include "am_base.h"
     32 #include "am_memory.h"
     33 #include "am_list.h"
     34 #include "am_string.h"
     35 
     36 Str open_file(MemArena *arena, char *path) {
     37     FILE *f = fopen(path, "r");
     38     assert(f);
     39 
     40     s32 error = fseek(f, 0L, SEEK_END);
     41     assert(!error);
     42 
     43     s64 size = ftell(f);
     44     assert(size >= 0);
     45     rewind(f);
     46 
     47     u8 *buf = am_mem_arena_push(arena, size);
     48     assert(buf);
     49 
     50     size_t items_read = fread(buf, 1, size, f);
     51     assert(items_read == (size_t)size);
     52 
     53     error = fclose(f);
     54     assert(!error);
     55 
     56     return am_str(buf, size);
     57 }
     58 
     59 int main(void) {
     60     MemArena a = am_mem_arena_create(am_mem_base_allocator_malloc());
     61     Str input = open_file(&a, "day03_input.txt");
     62     StrList lines = am_str_split(&a, input, (u8 *)"\n", 1);
     63 
     64     u64 num_columns = lines.first->s.size;
     65     u64 *counts = AM_MEM_ARENA_PUSH_ARRAY(&a, u64, num_columns);
     66     for (u64 i = 0; i < num_columns; i++) {
     67         counts[i] = 0;
     68     }
     69 
     70     for (StrListNode *number = lines.first; number; number = number->next) {
     71         assert(number->s.size == num_columns);
     72         for (u64 i = 0; i < num_columns; i++) {
     73             counts[i] += number->s.str[i] == '1';
     74         }
     75     }
     76 
     77     u64 num_numbers = lines.node_count;
     78     assert(num_numbers % 2 == 0);
     79     u64 gamma = 0;
     80     for (u64 i = 0; i < num_columns; i++) {
     81         assert(counts[i] != num_numbers / 2);
     82         gamma |= (counts[i] > (num_numbers / 2)) << (num_columns - i - 1);
     83     }
     84 
     85     u64 epsilon = (~gamma) & (0xFFFFFFFF >> (64 - num_columns));
     86 
     87     printf("Power consumption: %" PRIu64 "\n", gamma * epsilon);
     88     am_mem_arena_release(&a);
     89     return 0;
     90 }