summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2021/AOC-14
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-07-31 11:46:17 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-07-31 11:46:17 +0300
commitaec1c07260257ba7c28eff53f422ddb7daaf316a (patch)
tree88b279cb646bad60c3d55bdd01bb323065fca519 /Advent-of-Code-2021/AOC-14
Big Bang
Diffstat (limited to 'Advent-of-Code-2021/AOC-14')
-rwxr-xr-xAdvent-of-Code-2021/AOC-14/aoc-14bin0 -> 21216 bytes
-rw-r--r--Advent-of-Code-2021/AOC-14/aoc-14.c202
-rw-r--r--Advent-of-Code-2021/AOC-14/input.txt102
-rw-r--r--Advent-of-Code-2021/AOC-14/sample.txt18
4 files changed, 322 insertions, 0 deletions
diff --git a/Advent-of-Code-2021/AOC-14/aoc-14 b/Advent-of-Code-2021/AOC-14/aoc-14
new file mode 100755
index 0000000..630caae
--- /dev/null
+++ b/Advent-of-Code-2021/AOC-14/aoc-14
Binary files differ
diff --git a/Advent-of-Code-2021/AOC-14/aoc-14.c b/Advent-of-Code-2021/AOC-14/aoc-14.c
new file mode 100644
index 0000000..d23bfcb
--- /dev/null
+++ b/Advent-of-Code-2021/AOC-14/aoc-14.c
@@ -0,0 +1,202 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#if 0
+#define PFILE "sample.txt"
+#else
+#define PFILE "input.txt"
+#endif
+
+#define T_CAP 100
+char template[T_CAP];
+size_t tmpl_sz = 0;
+
+#define R_CAP 100
+typedef struct {
+ char inp[2];
+ char ch;
+} rule_t;
+rule_t rules[R_CAP];
+size_t rules_sz = 0;
+
+#define CAP 1000000
+typedef uint64_t lsize_t;
+char product[CAP];
+lsize_t prod_sz = 0;
+
+
+void Parse()
+{
+
+ // some init
+ memset(template, '\0', T_CAP);
+ memset(product, '\0', CAP);
+
+ char ch;
+ FILE *fp;
+ fp = fopen(PFILE, "r");
+
+ int mode = 0; // 0 - for template; 1 - for rules; 2 - intermid
+ int mode_s = 0; // 0 - has not encountered ' '; 1 - after ' '
+ while((ch = fgetc(fp)) != EOF)
+ {
+ if(ch == '\n' && mode == 0)
+ {
+ mode = 2;
+ }
+ else if(ch == '\n' && mode == 2)
+ {
+ mode = 1;
+ }
+ else if(ch == '\n' && mode == 1)
+ {
+ memset(rules[rules_sz].inp, '\0', 2);
+ rules[rules_sz].ch = '\0';
+ }
+ else if(ch != '\n' && mode == 0)
+ {
+ template[tmpl_sz] = ch;
+ tmpl_sz += 1;
+ }
+ else if(ch != '\n' && mode == 1)
+ {
+ switch(mode_s)
+ {
+ case 0:
+ if(rules[rules_sz].inp[0] == '\0')
+ {
+ rules[rules_sz].inp[0] = ch;
+ }
+ else
+ {
+ rules[rules_sz].inp[1] = ch;
+ mode_s = 1;
+ }
+ break;
+ case 1:
+ if(ch != ' ' && ch != '-' && ch != '>')
+ {
+ rules[rules_sz].ch = ch;
+ rules_sz += 1;
+ mode_s = 0;
+ }
+ }
+ }
+ }
+
+ fclose(fp);
+}
+
+void InsertAt(char ch, size_t pos)
+{
+ for(lsize_t i = prod_sz; i > pos; i--)
+ {
+ product[i] = product[i-1];
+ }
+ product[pos] = ch;
+
+ prod_sz += 1;
+}
+
+void Part1()
+{
+ Parse();
+ prod_sz = tmpl_sz;
+ memcpy(product, template, prod_sz);
+
+ char elements[CAP]; memset(elements, '\0', CAP);
+ lsize_t positions[CAP];
+ lsize_t inrt_sz = 0;
+
+ printf("%s\n", product);
+
+ for(int step = 0; step < 40; step++)
+ {
+ for(lsize_t i = 0; i < (prod_sz-1); i++)
+ {
+ for(int j = 0; j < rules_sz; j++)
+ {
+ if(product[i] == rules[j].inp[0] &&
+ product[i+1] == rules[j].inp[1])
+ {
+ elements[inrt_sz] = rules[j].ch;
+ positions[inrt_sz] = i + 1 + inrt_sz; //error potential
+ inrt_sz += 1;
+ break;
+ }
+ }
+ }
+ for(lsize_t i = 0; i < inrt_sz; i++)
+ {
+ InsertAt(elements[i], positions[i]);
+ }
+ inrt_sz = 0;
+ memset(elements, '\0', CAP);
+ }
+
+ // Get count and subtract
+ lsize_t el_occurence[10];
+ /* S - 0 */
+ /* H - 1 */
+ /* N - 2 */
+ /* C - 3 */
+ /* O - 4 */
+ /* P - 5 */
+ /* F - 6 */
+ /* B - 7 */
+ /* V - 8 */
+ /* K - 9 */
+
+ for(lsize_t i = 0; i < prod_sz; i++)
+ {
+ switch(product[i])
+ {
+ case 'S': el_occurence[0] += 1; break;
+ case 'H': el_occurence[1] += 1; break;
+ case 'N': el_occurence[2] += 1; break;
+ case 'C': el_occurence[3] += 1; break;
+ case 'O': el_occurence[4] += 1; break;
+ case 'P': el_occurence[5] += 1; break;
+ case 'F': el_occurence[6] += 1; break;
+ case 'B': el_occurence[7] += 1; break;
+ case 'V': el_occurence[8] += 1; break;
+ case 'K': el_occurence[9] += 1; break;
+ }
+ }
+
+ //get biggest
+ lsize_t biggest = 0;
+ for(int i = 0; i < 10; i++)
+ {
+ int counter = 0;
+ for(int j = 0; j < 10; j++)
+ if(el_occurence[i] >= el_occurence[j]) counter += 1;
+ if(counter == 10) { biggest = el_occurence[i]; break; }
+ }
+
+ //get smallest
+ lsize_t smallest = 0;
+ for(int i = 0; i < 10; i++)
+ {
+ int counter = 0;
+ for(int j = 0; j < 10; j++)
+ if(el_occurence[i] <= el_occurence[j]) counter += 1;
+ if(counter == 10) {smallest = el_occurence[i]; break; }
+ }
+
+ lsize_t result = biggest - smallest;
+ printf("%llu-%llu=%llu", biggest, smallest, result);
+}
+
+void Part2()
+{
+
+}
+
+int main(void)
+{
+ Part1();
+ return 0;
+}
diff --git a/Advent-of-Code-2021/AOC-14/input.txt b/Advent-of-Code-2021/AOC-14/input.txt
new file mode 100644
index 0000000..1380de1
--- /dev/null
+++ b/Advent-of-Code-2021/AOC-14/input.txt
@@ -0,0 +1,102 @@
+SHHNCOPHONHFBVNKCFFC
+
+HH -> K
+PS -> P
+BV -> H
+HB -> H
+CK -> F
+FN -> B
+PV -> S
+KK -> F
+OF -> C
+SF -> B
+KB -> S
+HO -> O
+NH -> N
+ON -> V
+VF -> K
+VP -> K
+PH -> K
+FF -> V
+OV -> N
+BO -> K
+PO -> S
+CH -> N
+FO -> V
+FB -> H
+FV -> N
+FK -> S
+VC -> V
+CP -> K
+CO -> K
+SV -> N
+PP -> B
+BS -> P
+VS -> C
+HV -> H
+NN -> F
+NK -> C
+PC -> V
+HS -> S
+FS -> S
+OB -> S
+VV -> N
+VO -> P
+KV -> F
+SK -> O
+BC -> P
+BP -> F
+NS -> P
+SN -> S
+NC -> N
+FC -> V
+CN -> S
+OK -> B
+SC -> N
+HN -> B
+HP -> B
+KP -> B
+CB -> K
+KF -> C
+OS -> B
+BH -> O
+PN -> K
+VN -> O
+KH -> F
+BF -> H
+HF -> K
+HC -> P
+NP -> H
+KO -> K
+CF -> H
+BK -> O
+OH -> P
+SO -> F
+BB -> F
+VB -> K
+SP -> O
+SH -> O
+PK -> O
+HK -> P
+CC -> V
+NB -> O
+NV -> F
+OO -> F
+VK -> V
+FH -> H
+SS -> C
+NO -> P
+CS -> H
+BN -> V
+FP -> N
+OP -> N
+PB -> P
+OC -> O
+SB -> V
+VH -> O
+KS -> B
+PF -> N
+KN -> H
+NF -> N
+CV -> K
+KC -> B
diff --git a/Advent-of-Code-2021/AOC-14/sample.txt b/Advent-of-Code-2021/AOC-14/sample.txt
new file mode 100644
index 0000000..b5594dd
--- /dev/null
+++ b/Advent-of-Code-2021/AOC-14/sample.txt
@@ -0,0 +1,18 @@
+NNCB
+
+CH -> B
+HH -> N
+CB -> H
+NH -> C
+HB -> C
+HC -> B
+HN -> C
+NN -> C
+BH -> H
+NC -> B
+NB -> B
+BN -> B
+BB -> N
+BC -> B
+CC -> N
+CN -> C