summary refs log tree commit diff homepage
path: root/2019/day02.c
blob: c574b574300604801ed332ca79df154d4368a080 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void run(int *v) {
	for (int i = 0; v[i] != 99; i += 4) {
		switch (v[i]) {
			break; case 1: v[v[i + 3]] = v[v[i + 1]] + v[v[i + 2]];
			break; case 2: v[v[i + 3]] = v[v[i + 1]] * v[v[i + 2]];
			break; default: abort();
		}
	}
}
int main(void) {
	int a[1024] = {0};
	for (int i = 0; EOF != scanf("%d,", &a[i]); ++i);
	int v[1024];
	memcpy(v, a, sizeof(v));
	v[1] = 12;
	v[2] = 2;
	run(v);
	printf("%d\n", v[0]);
	for (int noun = 0; noun < 100; ++noun) {
		for (int verb = 0; verb < 100; ++verb) {
			memcpy(v, a, sizeof(v));
			v[1] = noun;
			v[2] = verb;
			run(v);
			if (v[0] != 19690720) continue;
			printf("%d\n", 100 * noun + verb);
			return 0;
		}
	}
}