From 711b2028f367cc0033b41605c8522e5150625ce6 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 12 Dec 2018 01:05:33 -0500 Subject: Solve day 12 part 1 --- 2018/day12.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2018/day12.c (limited to '2018') diff --git a/2018/day12.c b/2018/day12.c new file mode 100644 index 0000000..5f2f04e --- /dev/null +++ b/2018/day12.c @@ -0,0 +1,50 @@ +#include +#include +#include + +typedef unsigned uint; + +enum { Zero = 128, Len = Zero * 2 }; +struct State { + char pots[Len]; +}; + +struct Rule { + char pots[5]; + char next; +}; + +int main() { + struct State state; + memset(state.pots, '.', Len); + scanf("initial state: %s\n\n", &state.pots[Zero]); + for (uint i = 0; i < Len; ++i) { + if (!state.pots[i]) state.pots[i] = '.'; + } + + uint rulen = 0; + struct Rule rules[32]; + while (!feof(stdin)) { + scanf("%5c => %c\n", rules[rulen].pots, &rules[rulen].next); + rulen++; + } + + for (uint g = 0; g < 20; ++g) { + struct State next; + memset(next.pots, '.', Len); + for (uint i = 2; i < Len - 2; ++i) { + for (uint r = 0; r < rulen; ++r) { + if (memcmp(&state.pots[i - 2], rules[r].pots, 5)) continue; + next.pots[i] = rules[r].next; + break; + } + } + state = next; + } + + int sum = 0; + for (int i = 0; i < Len; ++i) { + if (state.pots[i] == '#') sum += i - Zero; + } + printf("%d\n", sum); +} -- cgit 1.4.1