diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-20 16:32:41 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-20 16:32:41 +0100 |
| commit | 477ed667b1a9bc7f73850f45449666a8907ecaed (patch) | |
| tree | 1b867729d2f5527c98b8143f730d21cd7f39b007 /2023/20/20b.c | |
| parent | 1f75d7c9ebec207c8193754792076dc66eba9713 (diff) | |
| download | aoc-477ed667b1a9bc7f73850f45449666a8907ecaed.tar.gz aoc-477ed667b1a9bc7f73850f45449666a8907ecaed.zip | |
Added solution for 20
Diffstat (limited to '2023/20/20b.c')
| -rw-r--r-- | 2023/20/20b.c | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/2023/20/20b.c b/2023/20/20b.c new file mode 100644 index 0000000..dc0d6b3 --- /dev/null +++ b/2023/20/20b.c | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | /* | ||
| 2 | This one is a bit weird. This program works only for my specific input | ||
| 3 | (included in this folder). Similarly to number 8, this program outputs | ||
| 4 | 4 numbers and you have to take the lcm of them. | ||
| 5 | |||
| 6 | I solved it this way: | ||
| 7 | 1. First, using a modified version of the code for part one (graph.c), | ||
| 8 | I printed the graph as a list of adjacency lists. | ||
| 9 | 2. I then went to https://csacademy.com/app/graph_editor and observed | ||
| 10 | the graph. I noticed that after the button is pressed, the signal | ||
| 11 | is sent to 4 independent parts of the graph. Each of these parts has | ||
| 12 | only one entry point and one exit point. The entry points were not | ||
| 13 | flip-flop nodes. | ||
| 14 | 3. Finally, I implemented the code to check how long it takes for the | ||
| 15 | graph to go back to the initial state, and I simulated sending a | ||
| 16 | signal to each of the 4 parts independently. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <inttypes.h> | ||
| 20 | #include <stdbool.h> | ||
| 21 | #include <stdio.h> | ||
| 22 | #include <stdlib.h> | ||
| 23 | #include <string.h> | ||
| 24 | |||
| 25 | #define N 100 | ||
| 26 | #define ischar(c) (c >= 'a' && c <= 'z') | ||
| 27 | |||
| 28 | typedef struct { | ||
| 29 | int nin, nout, in[N], out[N]; | ||
| 30 | char name[20], outc[N][20]; | ||
| 31 | bool isff, ison, reg[N]; | ||
| 32 | } node_t; | ||
| 33 | |||
| 34 | typedef struct { | ||
| 35 | int i, n; | ||
| 36 | struct {int node; bool hi;} elem[500]; | ||
| 37 | } queue_t; | ||
| 38 | |||
| 39 | char *buf, line[N][N]; | ||
| 40 | bool rx; | ||
| 41 | int b, r, n; | ||
| 42 | node_t m[N]; | ||
| 43 | queue_t q; | ||
| 44 | |||
| 45 | void add(int v, bool hi) { | ||
| 46 | q.elem[q.n].node = v; | ||
| 47 | q.elem[q.n].hi = hi; | ||
| 48 | q.n++; | ||
| 49 | } | ||
| 50 | |||
| 51 | void send(int v, bool hi) { | ||
| 52 | for (int j = 0; j < m[v].nout; j++) { | ||
| 53 | add(m[v].out[j], hi); | ||
| 54 | m[m[v].out[j]].reg[v] = hi; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | int findm(char *name) { | ||
| 59 | for (int k = 0; k < n; k++) | ||
| 60 | if (!strcmp(m[k].name, name)) | ||
| 61 | return k; | ||
| 62 | return n; | ||
| 63 | } | ||
| 64 | |||
| 65 | void sig(int node, bool high) { | ||
| 66 | q.n = q.i = 0; | ||
| 67 | add(node, high); | ||
| 68 | while (q.i < q.n) { | ||
| 69 | int v = q.elem[q.i].node; | ||
| 70 | bool hi = q.elem[q.i].hi; | ||
| 71 | q.i++; | ||
| 72 | |||
| 73 | if (v == b) { | ||
| 74 | send(v, hi); | ||
| 75 | } else if (m[v].isff) { | ||
| 76 | if (!hi) | ||
| 77 | send(v, m[v].ison = !m[v].ison); | ||
| 78 | } else { | ||
| 79 | bool allhi = true; | ||
| 80 | for (int j = 0; j < m[v].nin; j++) | ||
| 81 | allhi = allhi && m[v].reg[m[v].in[j]]; | ||
| 82 | send(v, !allhi); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | bool isclean(void) { | ||
| 88 | for (int i = 0; i < n; i++) { | ||
| 89 | if (m[i].isff && m[i].ison) | ||
| 90 | return false; | ||
| 91 | else | ||
| 92 | for (int j = 0; j < m[i].nin; j++) | ||
| 93 | if (m[i].reg[j]) | ||
| 94 | return false; | ||
| 95 | } | ||
| 96 | return true; | ||
| 97 | } | ||
| 98 | |||
| 99 | int64_t period(int node) { | ||
| 100 | int64_t npush = 0; | ||
| 101 | do { | ||
| 102 | sig(node, false); | ||
| 103 | npush++; | ||
| 104 | } while (!isclean()); | ||
| 105 | return npush; | ||
| 106 | } | ||
| 107 | |||
| 108 | int main() { | ||
| 109 | for (n = 0; (buf = fgets(line[n], N, stdin)) != NULL; n++) { | ||
| 110 | if (ischar(*buf)) b = n; | ||
| 111 | m[n].isff = *buf == '%'; | ||
| 112 | if (!ischar(*buf)) buf++; | ||
| 113 | for (int i = 0; ischar(*buf); m[n].name[i++] = *(buf++)) ; | ||
| 114 | buf += 4; | ||
| 115 | for (int i = 0; *buf != '\n'; i++) { | ||
| 116 | while (!ischar(*buf)) buf++; | ||
| 117 | for (int j = 0; ischar(*buf); m[n].outc[i][j++] = *(buf++)) ; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | for (int i = 0; i < n; i++) { | ||
| 122 | for (int j = 0; m[i].outc[j][0]; j++) { | ||
| 123 | int k = findm(m[i].outc[j]); | ||
| 124 | m[i].out[m[i].nout++] = k; | ||
| 125 | m[k].in[m[k].nin++] = i; | ||
| 126 | if (k == n) { | ||
| 127 | for (int k = 0; m[i].outc[j][k]; k++) | ||
| 128 | m[n].name[k] = m[i].outc[j][k]; | ||
| 129 | n++; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | for (int i = 0; i < n; i++) | ||
| 135 | if (!strcmp(m[i].name, "rx")) | ||
| 136 | r = i; | ||
| 137 | |||
| 138 | printf("%" PRId64 "\n", period(19)); | ||
| 139 | printf("%" PRId64 "\n", period(33)); | ||
| 140 | printf("%" PRId64 "\n", period(39)); | ||
| 141 | printf("%" PRId64 "\n", period(57)); | ||
| 142 | return 0; | ||
| 143 | } | ||
