advent-of-code

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

day02.c (2445B)


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