summary refs log tree commit diff homepage
path: root/2018/day05.c
blob: 1a80e2a59385dc1b84a38734f88b056deeb70dc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static size_t react(char *buf, size_t len) {
	for (size_t i = 0; i < len - 1; ++i) {
		if (tolower(buf[i]) != tolower(buf[i + 1])) continue;
		if (islower(buf[i]) == islower(buf[i + 1])) continue;
		len -= 2;
		memmove(&buf[i], &buf[i + 2], len - i);
		i = (size_t)-1;
	}
	return len;
}

int main() {
	char buf[50000];
	size_t len = fread(buf, 1, sizeof(buf), stdin);

	char buf1[50000];
	memcpy(buf1, buf, len);
	printf("%zu\n", react(buf1, len));

	size_t min = len;
	for (char x = 'a'; x <= 'z'; ++x) {
		char buf2[50000];
		size_t len2 = 0;
		for (size_t i = 0; i < len; ++i) {
			if (tolower(buf[i]) == x) continue;
			buf2[len2++] = buf[i];
		}
		len2 = react(buf2, len2);
		if (len2 < min) min = len2;
	}
	printf("%zu\n", min);
}
18-09-21Add scheme -i to swap white and blackJune McEnroe 2018-09-21Map caps lock to escape on Linux consoleJune McEnroe 2018-09-19Fix README mandoc lintsJune McEnroe 2018-09-19Un-NOT trans.alpha values in pngoJune McEnroe 2018-09-18Refactor reads in pngo and clear palette between filesJune McEnroe 2018-09-17Add tRNS support to pngoJune McEnroe 2018-09-11Move gfx man pages to gfx/manJune McEnroe 2018-09-11Move bin man pages to bin/manJune McEnroe 2018-09-11Rewrite gfx.7 and render plaintext READMEJune McEnroe 2018-09-11Remove GAMES from BINSJune McEnroe 2018-09-11Rewrite bin.7 and render to plaintext READMEJune McEnroe 2018-09-11Add "blank" lines to man pagesJune McEnroe 2018-09-10Add mdoc syntax fileJune McEnroe 2018-09-08Fix Nm usage in multi-name man pagesJune McEnroe 2018-09-08Put real dates on man pagesJune McEnroe 2018-09-08Replace gfx README with REAMDE.7June McEnroe 2018-09-08Link gfx man pages in ~/.localJune McEnroe