|
- //
- // Created by red on 02/12/19.
- //
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdint.h>
-
- void print_elems(long* arr, size_t n);
- long run(const long* input, size_t n, long noun, long verb);
-
- int main() {
- char input_str[350];
- memset(input_str, 0, 350*sizeof(char));
- long input[140];
- char* ptr = input_str;
- size_t i = 0;
- scanf("%s", input_str);
- while (*ptr != 0 && input_str - ptr < 350) {
- input[i] = strtol(ptr, &ptr, 10);
- i++;
- ptr++;
- }
- size_t size = i;
- long out = run(input, size, 12, 2);
- printf("Value of mem[0] with noun=12, verb=2: %ld\n", out);
- for (i = 0; i < 100; i++) {
- for (long j = 0; j < 100; j++) {
- long out = run(input, size, i, j);
- if (out == 19690720) {
- printf("Noun+verb found! n: %ld, v: %ld\n", i, j);
- return EXIT_SUCCESS;
- }
- }
- }
- }
-
- long run(const long* input, size_t n, long noun, long verb) {
- long mem[n];
- memcpy((void*) mem, (const void*) input, n*sizeof(long));
- size_t ptr = 0;
- mem[1] = noun;
- mem[2] = verb;
- while (ptr < n) {
- long opcode = mem[ptr];
- if (opcode == 1) {
- long a = mem[ptr+1];
- long b = mem[ptr+2];
- long c = mem[ptr+3];
- mem[c] = mem[a] + mem[b];
- ptr += 4;
- } else if (opcode == 2) {
- long a = mem[ptr+1];
- long b = mem[ptr+2];
- long c = mem[ptr+3];
- mem[c] = mem[a] * mem[b];
- ptr += 4;
- } else if (opcode == 99) {
- return mem[0];
- } else break;
- }
- return -1;
- }
-
- void print_elems(long* arr, size_t n) {
- puts("Printing elements of array");
- for (size_t i = 0; i < n; i++) {
- printf("%ld ", arr[i]);
- }
- puts("");
- }
|