Browse Source

initial commit

master
red 4 years ago
commit
12d61e5873
4 changed files with 99 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +7
    -0
      CMakeLists.txt
  3. +17
    -0
      day1/day1.c
  4. +72
    -0
      day2/day2.c

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
# ignore input files and objects
*.o
*.input

+ 7
- 0
CMakeLists.txt View File

@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.12)
project(aoc2019 C)

set(CMAKE_C_STANDARD 99)

add_executable(day1 day1/day1.c)
add_executable(day2 day2/day2.c)

+ 17
- 0
day1/day1.c View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>

int main() {
unsigned sum = 0;
while (1) {
int a = 0;
scanf("%d\n", &a);
if(!a) { break; }
while (a > 6) {
a = (a / 3) - 2;
sum += a;
}
}
printf("Sum: %u\n", sum);
return EXIT_SUCCESS;
}

+ 72
- 0
day2/day2.c View File

@@ -0,0 +1,72 @@
//
// 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("");
}

Loading…
Cancel
Save