From 5d3c554668054f08497c5ad6cbcb582b960b66ed Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 14 Dec 2021 12:55:44 -0500 Subject: Solve day 14 part 1 --- 2021/day14.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2021/day14.c (limited to '2021') diff --git a/2021/day14.c b/2021/day14.c new file mode 100644 index 0000000..7525bd8 --- /dev/null +++ b/2021/day14.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +static struct { + char a, b, c; +} pairs[128]; +static size_t len; +static char *step(const char *prev) { + char *next = malloc(1 + strlen(prev)*2); + char *ptr = next; + size_t i; + for (i = 0; prev[i+1]; ++i) { + *ptr++ = prev[i]; + for (size_t j = 0; j < len; ++j) { + if (prev[i] != pairs[j].a || prev[i+1] != pairs[j].b) continue; + *ptr++ = pairs[j].c; + break; + } + } + *ptr++ = prev[i]; + *ptr = '\0'; + return next; +} +int main(void) { + size_t cap = 0; + char *polymer = NULL; + ssize_t n = getline(&polymer, &cap, stdin); + if (polymer[n-1] == '\n') polymer[n-1] = '\0'; + while (EOF != scanf(" %c%c -> %c\n", &pairs[len].a, &pairs[len].b, &pairs[len].c)) { + len++; + } + for (int i = 0; i < 10; ++i) { + char *next = step(polymer); + free(polymer); + polymer = next; + } + int freq[128] = {0}; + for (size_t i = 0; polymer[i]; ++i) { + freq[polymer[i]]++; + } + int min = INT_MAX, max = INT_MIN; + for (int i = 'A'; i <= 'Z'; ++i) { + if (freq[i] && freq[i] < min) min = freq[i]; + if (freq[i] > max) max = freq[i]; + } + printf("%d\n", max - min); +} -- cgit 1.4.1 >
Commit message (Collapse)Author
2018-04-08Commit with june@causal.agencyJune McEnroe
2018-04-03Add Anthems for a Seventeen Year-Old Girl to new musicJune McEnroe
Another one from a while ago that I forgot about.
2018-04-03Add Yuugure no tori to new musicJune McEnroe
This was in my likes from a while ago but I never added it. It's the credits music from Attack on Titan season 2.
2018-04-03Add The Flute to new musicJune McEnroe
2018-04-03Add Flying Microtonal BananaJune McEnroe
2018-04-03Add DAYDREAMJune McEnroe
2018-04-03Add Floating in SpaceJune McEnroe
2018-04-03Add End PositionJune McEnroe
2018-04-03Add The Dusk in UsJune McEnroe
2018-04-03Add MASSEDUCTIONJune McEnroe
2018-04-03Add Luciferian Towers trackJune McEnroe
2018-04-03Indicate all Tiny Desk ConcertsJune McEnroe
2018-04-03Add FloristJune McEnroe
2018-04-03Add CHVRCHES tiny desk concertJune McEnroe
2018-04-03Add AuroraJune McEnroe
2018-04-03Add AtmosphereJune McEnroe
I know I've already put Joy Division in here. But this is also Trainspotting and I managed to miss / not appreciate this one on my first go through JD.
2018-04-03Add Born SlippyJune McEnroe
I watched Trainspotting last night.
2018-04-03Add I Can Tell You About PainJune McEnroe
2018-04-03Reverse new music orderJune McEnroe
2018-04-03Add FAUVE, CocoRosieJune McEnroe
2018-04-03Add House of LeavesJune McEnroe
2018-04-03Add ChromaticsJune McEnroe
2018-04-03Add those new Arcade Fire singlesJune McEnroe
2018-04-03Add SermonJune McEnroe
2018-04-03Add Pools of LightJune McEnroe
2018-04-03Add SteroidsJune McEnroe
2018-04-03Add A Common TruthJune McEnroe
2018-04-03Add new Colin Stetson albumJune McEnroe
2018-04-03Add ForgetJune McEnroe
2018-04-03Add ArcaJune McEnroe
2018-04-03Add new DMST and Joni VoidJune McEnroe
2018-04-03Add The Body LoversJune McEnroe
2018-04-03Add some albums from recentlyJune McEnroe
2018-04-03Add Saltland Light of MercyJune McEnroe
2018-04-03Add Jessica Moss GlaciersJune McEnroe
2018-04-03Add BNNY RBBTJune McEnroe
2018-04-03Add DepthsJune McEnroe
2018-04-03Add new Saltland previewJune McEnroe
2018-04-03Add BenjiJune McEnroe
2018-04-03Add Mouth MoodsJune McEnroe
2018-04-03Add Those Who Walk AwayJune McEnroe
2018-04-03Add Some TunesJune McEnroe
2018-04-03Add Pas pire popJune McEnroe
2018-04-03Add I Give You PowerJune McEnroe
2018-04-03Add Jenny GoGoJune McEnroe
2018-04-03Add KKB Fish BowlJune McEnroe
2018-04-03Add new Avec le soleil album previewJune McEnroe
2018-04-03Add Julien Baker as recent new musicJune McEnroe
2018-04-03Add recent new musicJune McEnroe