From 555091332de32b1d3b36c888ed21898aae540750 Mon Sep 17 00:00:00 2001 From: kartofen Date: Fri, 10 Feb 2023 23:53:15 +0200 Subject: add a couple of days --- Advent-of-Code-2022/aoc-25/build.sh | 5 ++ Advent-of-Code-2022/aoc-25/input.txt | 113 ++++++++++++++++++++++++++++++++++ Advent-of-Code-2022/aoc-25/main.c | 89 ++++++++++++++++++++++++++ Advent-of-Code-2022/aoc-25/sample.txt | 13 ++++ 4 files changed, 220 insertions(+) create mode 100755 Advent-of-Code-2022/aoc-25/build.sh create mode 100644 Advent-of-Code-2022/aoc-25/input.txt create mode 100644 Advent-of-Code-2022/aoc-25/main.c create mode 100644 Advent-of-Code-2022/aoc-25/sample.txt (limited to 'Advent-of-Code-2022/aoc-25') diff --git a/Advent-of-Code-2022/aoc-25/build.sh b/Advent-of-Code-2022/aoc-25/build.sh new file mode 100755 index 0000000..30f495e --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic diff --git a/Advent-of-Code-2022/aoc-25/input.txt b/Advent-of-Code-2022/aoc-25/input.txt new file mode 100644 index 0000000..8e7a4d1 --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/input.txt @@ -0,0 +1,113 @@ +1=0-12100-== +12=2=21=2-1=1122 +1=-1112=-12 +1--2210 +2-1110- +1=-1- +1112 +220--0 +20=--==0 +12-00- +21=21001-0 +1=101-=111-0==-- +2 +10-=1==-102-020== +2=12 +11=02=11-=2=2 +1=--2111010- +12210=1-=- +221=-=210-2=0=00- +11012 +22002====21 +1-202202--2-2 +1-=110-- +1021- +10-1-- +100-11=--=020 +2=01-=2-200- +10--11201011 +1-=- +10=-122-00=-0110 +122-2 +120-01--012-1202- +22=2=1=10221-=2 +1-=1 +1122-=- +1=1-02101- +1-=1=2-211 +12221-1-00 +2--10=-0-1===2 +120120=2110121020- +22=2-2=0 +1=2=0=201221101-== +20=-122 +211=1122=21=101 +1=0-2020==102=== +10-1-00 +1=221202=22 +1=20-0=12==- +1-2=0120=2001-0101 +11--000- +11 +21-00221020-12 +1--0=2==10=-=00 +121001=2000 +2=20--2=-0=-=2-11-0 +1=0=1120= +1=-=0 +10-2 +21-0=0 +2=-=-0 +1=-0-2= +1=2--112=10--1=2 +2---20=-10-00 +101020110 +102=12-1022==01-0 +10=---11 +2-=--00100=- +122=-=--- +1---==0012=110= +12= +10-20020-==0=1 +1022==2=2- +1212-02--=2=-0-210 +1121201=01-==2 +1=1==20=-1= +1=- +212=0=21110 +21==20- +1=-=-111020---21--= +111=0---=2=2 +112 +1--=11101==022202-1 +1=1=-=0-1-1=00-2 +1=01020=1 +102-220-2 +1--1=022020 +21-01= +1-0--=011 +2010 +10==1 +12=222-22--2021-21 +1=1121-2-1-201= +2211 +2=220-=2=-2=-1 +1=10-1 +1-12-022101=11-22=-2 +11200==1 +1=1-0100=--21201-0 +2=2=2-2==-- +112-= +120 +212222-0=1111=21-= +100-02---0110 +2202212-02-10 +1-=0101-1=2 +2101110111-- +1=11-21122==12- +2-0=200=011==-12= +2==-0022-20 +1==001211=22201== +12211=10-0= +1-00-101-==- +1-=1=222000=2== diff --git a/Advent-of-Code-2022/aoc-25/main.c b/Advent-of-Code-2022/aoc-25/main.c new file mode 100644 index 0000000..c88d918 --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/main.c @@ -0,0 +1,89 @@ +#include +#include +#include + +#if 0 + #define FILENAME "sample.txt" +#else + #define FILENAME "input.txt" +#endif + +int char_to_int[256] = { + ['1'] = 1, + ['2'] = 2, + ['0'] = 0, + ['-'] = -1, + ['='] = -2 +}; + +char int_to_char[5] = { + '=', '-', '0', '1', '2' +}; + +long long sum = 0; + +void encode() +{ + signed char num[1000] = {0}; + size_t num_sz = 0; + + for(long long n = sum; n > 0; n /= 5, num_sz++) { + num[num_sz] += n % 5; + num[num_sz+1] += num[num_sz]/5; + num[num_sz] %= 5; + + if(num[num_sz] == 3) { + num[num_sz] = -2; + num[num_sz+1] += 1; + } else if(num[num_sz] == 4) { + num[num_sz] = -1; + num[num_sz+1] += 1; + } + } + + for(int i = num_sz-1; i >= 0; i--) { + printf("%c", int_to_char[num[i]+2]); + } + + printf("\n"); +} + +long long decode(char *num) +{ + long long n = 0; + + size_t len = strlen(num); + long long power_of_5 = 1; + + for(int i = len-1; i >= 0; i--) { + n += power_of_5 * char_to_int[num[i]]; + power_of_5 *= 5; + } + + return n; +} + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file %s\n", FILENAME); + exit(1); + } + + char line[256]; + while(fgets(line, sizeof(line), fp) != NULL) { + if(line[strlen(line)-1] == '\n') + line[strlen(line)-1] = '\0'; + sum += decode(line); + } + + fclose(fp); +} + +int main(void) +{ + parse(); + encode(); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-25/sample.txt b/Advent-of-Code-2022/aoc-25/sample.txt new file mode 100644 index 0000000..027aeec --- /dev/null +++ b/Advent-of-Code-2022/aoc-25/sample.txt @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 -- cgit v1.2.3