day 14 final (need some comments, will not remember :-)

This commit is contained in:
2021-12-17 16:30:21 +01:00
parent d0c6c6e4f7
commit ca6b612d0d
3 changed files with 24 additions and 279 deletions

View File

@@ -27,9 +27,6 @@ static int cindex[] = {
['B'] = 0, ['C'] = 1, ['F'] = 2, ['H'] = 3, ['K'] = 4,
['N'] = 5, ['O'] = 6, ['P'] = 7, ['S'] = 8, ['V'] = 9
};
static int rev_index[] = {
'B', 'C', 'F', 'H', 'K', 'N', 'O', 'P', 'S', 'V'
};
#define INDEX(c) cindex[(int)c]
@@ -37,120 +34,55 @@ static u64 polymers[NPOLYMERS][NPOLYMERS];
static u64 rules[NPOLYMERS][NPOLYMERS];
#define MAX_SIZE 10000
char seq[MAX_SIZE], seq2[MAX_SIZE];
int nseq;
//int rules[NPOLYMERS * NPOLYMERS];
int nrules;
u64 count[NPOLYMERS] = {0};
static void print_polymers(int step)
{
//log(3, " B C F C D E F G H I J K")
log_f(3, "polymers(%d):\n ", step);
for (int i = 0; i < NPOLYMERS; ++i) {
log(3, "%10c", rev_index[i]);
}
for (int i = 0; i < NPOLYMERS; ++i) {
log(3, "\n%c ", rev_index[i]);
for (int j = 0; j < NPOLYMERS; ++j) {
if (polymers[i][j])
log(3, "%10lu", polymers[i][j]);
else
log(3, "%10s", "");
}
}
log(3, "\n");
}
static void print_rules()
{
//log(3, " B C F C D E F G H I J K")
log_f(3, "rules:\n ");
for (int i = 0; i < NPOLYMERS; ++i) {
log(3, "%10c", rev_index[i]);
}
for (int i = 0; i < NPOLYMERS; ++i) {
log(3, "\n%c ", rev_index[i]);
for (int j = 0; j < NPOLYMERS; ++j) {
log(3, "%10c", rules[i][j]? rules[i][j]: ' ');
}
}
log(3, "\n");
}
static void print_count()
{
//log(3, " B C F C D E F G H I J K")
log_f(3, "count:\n ");
for (int i = 0; i < NPOLYMERS; ++i) {
log(3, "%1c=%d ", rev_index[i], count[i]);
}
log(3, "\n");
}
/* read data and create graph.
*/
static int read_input()
static void read_input()
{
ssize_t len;
size_t alloc = 0;
char *buf;
char x, y, z;
nseq = getline(&buf, &alloc, stdin) - 1;
print_polymers(0);
buf[nseq] = 0;
len = getline(&buf, &alloc, stdin) - 1;
buf[len] = 0;
count[INDEX(*buf)]++;
count[INDEX(*(buf + nseq - 1))]++;
print_count();
for (int i = 0; i < nseq - 1; ++i) {
count[INDEX(*(buf + len - 1))]++;
for (int i = 0; i < len - 1; ++i)
polymers[INDEX(buf[i])][INDEX(buf[i+1])]++;
}
log(3, "buf=[%s]\n", buf);
print_polymers(0);
strcpy(seq, buf);
printf("str = %d [%s]\n", nseq, seq);
printf("empt = %ld\n", getline(&buf, &alloc, stdin));
/* get rules */
while ((len = getline(&buf, &alloc, stdin)) >= 0) {
if (sscanf(buf, "%c%c -> %c", &x, &y, &z) == 3)
rules[INDEX(x)][INDEX(y)] = z;
nrules++;
}
free(buf);
print_rules();
return nrules;
}
static u64 diff()
{
u64 min = UINT64_MAX, max = 0;
//for (unsigned i = 0; i < NPOLYMERS; ++i)
// for (unsigned i = 0; i < NPOLYMERS; ++i)
// log(5, "count[%c%c]=%d\n", rev_index[i] i + 'A', count[i]);
for (int i = 0; i < NPOLYMERS; ++i) {
for (int j = 0; j < NPOLYMERS; ++j) {
count[i] += polymers[i][j];
count[j] += polymers[i][j];
}
}
print_count();
for (int i = 0; i < NPOLYMERS; ++i) {
if (count[i] && count[i] < min)
min = count[i];
if (count[i] > max)
max = count[i];
}
log(3, "min = %lu max=%lu\n", min, max);
return (max - min) / 2;
}
static u64 part1(int steps)
static u64 doit(int steps)
{
//int step;
read_input();
for (int step = 0; step < steps; ++step) {
int new;
@@ -165,17 +97,15 @@ static u64 part1(int steps)
}
}
memcpy(polymers, new_polymers, sizeof(polymers));
print_polymers(step);
}
return diff();
//print_polymers(step);
}
static u64 doit(int part)
{
//read_input();
return part == 1? part1(10): part1(40);
}
//static u64 doit(int part)
//{
// //read_input();
// return part == 1? part1(10): part1(40);
//}
static int usage(char *prg)
{
@@ -204,6 +134,6 @@ int main(int ac, char **av)
if (optind < ac)
return usage(*av);
printf("%s : res=%lu\n", *av, doit(part));
printf("%s : res=%lu\n", *av, doit(part == 1? 10: 40));
exit (0);
}

View File

@@ -1,197 +0,0 @@
/* aoc-c.c: Advent of Code 2021, day 14 parts 1 & 2
*
* Copyright (C) 2021 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include "debug.h"
#include "bits.h"
//#include "list.h"
/* possible 10 letters: B, C, F, H, K, N, O, P, S, V */
int weight[] = {
['B'] = 0, ['C'] = 1, ['F'] = 2,
['H'] = 3, ['K'] = 4, ['N'] = 5, ['O'] = 6,
['P'] = 7, ['S'] = 8, ['V'] = 9
};
int count[26];
/*struct weigth {
char key;
int val;
} weight1[] = {
{ 'A', -1 },
{ 'B', 0 },
{ 'C', 1 },
{ 'D', -1 },
{ 'E', -1 },
{ 'F', 2 },
{ 'G', -1 },
{ 'H', 3 },
{ 'I', -1 },
{ 'J', -1 },
{ 'K', 4 },
{ 'L', -1 },
{ 'M', -1 },
{ 'N', 5 },
{ 'O', 6 },
{ 'P', 7 },
{ 'Q', -1 },
{ 'R', -1 },
{ 'S', 8 },
{ 'T', -1 },
{ 'U', -1 },
{ 'V', 9 },
{ 'W', -1 },
{ 'X', -1 },
{ 'Y', -1 },
{ 'Z', -1 }
};
*/
#define MAX_RUN 10
#define MAX_SIZE 1024 * 10 * 2 /* max resulting string size */
char seq[MAX_SIZE], seq2[MAX_SIZE];
int nseq;
int rules[10 * 10];
int nrules;
/* read data and create graph.
*/
static int read_input()
{
ssize_t len;
size_t alloc = 0;
char *buf;
char val[10] = { 0 };
int pos;//, ins;
nseq = getline(&buf, &alloc, stdin) - 1;
buf[nseq] = 0;
strcpy(seq, buf);
printf("str = %d [%s]\n", nseq, seq);
printf("empt = %ld\n", getline(&buf, &alloc, stdin));
/* get rules */
while ((len = getline(&buf, &alloc, stdin)) > 1) {
//log(3, "len = %d [%s]\n", len, buf);
sscanf(buf, "%c%c -> %c", val, val+1, val+2);
//*val -= 'A';
//*(val + 1) -= 'A';
//*(val + 2) -= 'A';
pos = weight[(int)*val] * 10 + weight[(int)*(val + 1)];
//ins = weight[(int)*(val + 2)];
rules[pos] = *(val + 2);
//log(3, val);
//log(3, "%s : %c %d %c %d %d %d pos=%d\n", val, *val, (int) *val - 'A', val[1], val[1] - 'A',
// weight[(int)*val] * 10, weight[(int)*(val + 1)], pos);
log(3, "%.2s : array[%d]=%c\n", val, pos, rules[pos]);
nrules++;
}
free(buf);
return nrules;
}
static int part1()
{
char *src, *dst;
int key;//, last;
unsigned int count[26] = {0};
unsigned min, max;
read_input();
for (int i = 0; i < 10; ++i) {
int j, k;
src = i % 2? seq2: seq;
dst = i % 2? seq: seq2;
//savedst = dst;
//savesrc = dst;
//last = strlen(src) - 1;
for (j = 0, k = 0; src[j+1]; ++j, k += 2) {
key = weight[(int) src[j]] * 10 + weight[(int) src[j+1]];
//(*src - 'A') * 10 + *(src+1) -'A';
//log(3, "%d/%d: src=%s dst=%s s1=%c s2=%c key=%d\n", i, j, src, dst, *src, *(src + 1), key);
dst[k] = src[j];
dst[k+1] = rules[key];
dst[k+2] = src[j+1];
//src++;
//dst += 3;
//log_i(3, "j=%d k=%d dst=%s\n", j, k, dst);
}
log(3, "i = %d len=%d dst = %s\n", i, strlen(dst), dst);
//log(3, "%d: src=%p dst=%p seq=%p seq2=%p\n", src, dst, seq, seq2);
}
for (int i=0; dst[i]; ++i)
count[dst[i] - 'A']++;
min = ~0;
max = 0;
for (unsigned i = 0; i < sizeof(count)/sizeof(*count); ++i)
printf("count[%c]=%d\n", i + 'A', count[i]);
for (unsigned i = 0; i < sizeof(count)/sizeof(*count); ++i) {
if (count[i]) {
if (count[i] < min)
min = count[i];
if (count[i] > max)
max = count[i];
}
}
printf("min = %i max=%u\n", min, max);
return max - min;
}
static int part2()
{
return 2;
}
static int doit(int part)
{
//read_input();
return part == 1? part1(): part2();
}
static int usage(char *prg)
{
fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg);
return 1;
}
int main(int ac, char **av)
{
int opt, part = 1;
while ((opt = getopt(ac, av, "d:p:")) != -1) {
switch (opt) {
case 'd':
debug_level_set(atoi(optarg));
break;
case 'p': /* 1 or 2 */
part = atoi(optarg);
if (part < 1 || part > 2)
return usage(*av);
break;
default:
return usage(*av);
}
}
if (optind < ac)
return usage(*av);
printf("%s : res=%d\n", *av, doit(part));
exit (0);
}