aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornytpu <alex@nytpu.com>2020-12-11 23:57:33 -0700
committernytpu <alex@nytpu.com>2020-12-11 23:57:33 -0700
commitfdcfe52d707413523c63d7ff741bc8f15458cbff (patch)
tree4dc2948760b15cc5ed23f5e11685ed9988de85b5
parentadded day 11 (diff)
downloadadvent_of_code_2020-fdcfe52d707413523c63d7ff741bc8f15458cbff.tar.bz2
advent_of_code_2020-fdcfe52d707413523c63d7ff741bc8f15458cbff.zip
added day 12
-rw-r--r--day12/input.txt748
-rw-r--r--day12/problem_1.c65
-rw-r--r--day12/problem_2.c78
-rw-r--r--day12/test_input.txt5
4 files changed, 896 insertions, 0 deletions
diff --git a/day12/input.txt b/day12/input.txt
new file mode 100644
index 0000000..6f2eec9
--- /dev/null
+++ b/day12/input.txt
@@ -0,0 +1,748 @@
+F75
+L90
+N5
+W2
+N5
+L90
+S1
+R90
+S4
+E2
+W1
+F100
+L180
+W3
+N1
+R180
+W1
+S4
+R270
+S4
+F23
+E2
+F17
+N5
+R90
+F83
+E2
+F34
+E3
+S4
+F19
+W4
+L90
+W2
+L90
+E2
+F79
+L90
+F80
+N1
+R90
+F35
+L90
+N5
+E3
+S5
+R90
+F71
+N1
+F19
+F49
+R90
+E4
+L90
+W1
+R90
+S2
+E1
+L180
+F79
+W4
+L180
+F74
+W5
+R90
+F30
+R90
+W2
+R90
+F35
+R90
+F12
+W2
+F7
+R90
+E5
+F68
+E3
+F21
+N4
+F22
+W4
+S2
+F89
+L90
+N5
+R90
+F8
+R90
+W2
+S3
+R180
+N2
+R90
+F49
+N4
+E5
+F31
+S3
+E4
+F61
+E1
+L90
+N1
+W5
+F15
+F13
+E4
+N2
+R180
+S2
+L90
+F92
+S4
+R180
+L90
+S1
+L90
+F98
+N4
+E3
+F69
+S3
+R180
+F59
+S5
+R90
+F32
+E5
+S4
+W4
+F9
+R180
+F44
+L90
+W1
+L180
+F74
+W1
+F55
+R270
+S3
+F62
+W3
+F61
+N2
+L90
+F43
+S3
+F86
+W1
+S3
+E3
+F34
+L90
+F77
+N1
+F27
+N5
+W5
+N3
+F80
+E2
+L180
+F59
+W1
+L90
+N4
+R90
+W5
+L180
+W5
+L90
+F17
+E5
+L180
+W4
+R180
+W4
+F70
+S5
+R90
+W4
+W4
+N3
+W1
+N2
+E5
+F53
+N5
+R90
+R90
+E2
+N5
+R270
+S5
+R90
+N5
+E1
+R90
+F76
+R90
+W3
+S2
+R90
+S5
+E1
+L90
+E2
+F58
+R90
+F15
+N2
+L90
+N5
+F65
+E5
+N2
+R90
+W1
+F40
+S4
+R90
+F23
+W4
+R90
+W4
+F17
+S2
+R90
+E4
+F58
+R90
+L90
+F64
+N4
+F63
+E5
+N1
+F38
+N3
+F19
+E5
+S4
+F14
+R90
+S3
+E2
+S4
+E1
+F12
+W3
+L90
+F33
+S4
+W3
+F29
+R90
+N3
+F44
+E5
+N1
+F49
+W5
+F52
+N5
+L90
+S4
+F31
+R90
+N3
+L270
+L270
+E4
+N4
+W1
+L90
+S5
+W4
+R180
+N3
+L180
+F2
+S1
+L180
+N3
+L90
+N1
+W4
+R180
+W5
+R270
+N4
+L180
+F51
+R180
+S5
+F35
+E3
+R180
+F1
+S3
+E2
+F47
+E4
+S5
+E1
+L90
+F16
+S4
+F17
+N1
+W2
+L90
+F99
+E1
+R180
+N1
+F78
+L90
+F18
+S2
+W3
+N2
+F56
+R270
+W1
+R90
+F53
+L180
+F6
+W5
+R90
+W4
+F27
+R90
+F73
+E5
+R90
+W4
+S4
+W3
+S2
+E5
+N5
+R270
+F43
+W1
+F77
+E4
+F85
+W2
+R90
+N1
+E1
+F39
+R90
+W2
+N4
+E2
+W4
+F81
+W4
+F3
+W4
+R180
+W2
+N5
+R90
+N5
+E2
+S5
+S3
+E1
+N4
+W1
+S3
+F48
+S2
+F23
+E2
+R180
+F89
+L180
+N3
+E1
+N5
+W2
+L90
+E1
+F80
+R90
+S1
+E3
+N5
+L90
+F11
+W5
+F70
+N3
+W1
+L90
+N5
+F99
+N5
+F24
+R180
+E4
+F57
+S1
+F9
+S5
+R90
+W4
+F16
+E1
+F18
+F17
+L180
+S1
+L90
+S1
+E2
+N1
+E4
+F84
+N1
+F6
+E1
+L180
+W3
+L180
+N3
+F60
+F43
+S3
+F48
+W2
+S1
+R180
+N1
+S2
+F78
+L180
+W4
+F51
+W5
+N2
+E2
+N1
+W4
+F65
+N3
+L90
+W3
+N2
+W2
+F46
+N4
+W4
+N2
+W2
+F74
+R180
+E4
+R90
+N1
+F33
+S4
+L180
+S1
+F15
+R180
+S5
+L180
+F21
+N4
+F87
+W4
+L90
+S4
+F40
+S5
+W3
+L180
+F78
+W2
+F40
+N3
+E3
+S4
+W1
+S4
+E1
+S4
+W5
+S2
+F82
+E5
+R180
+F7
+W3
+R90
+N2
+R90
+W5
+N5
+W5
+S3
+E2
+L90
+E1
+F77
+E4
+F70
+W5
+S2
+W2
+F7
+W4
+F8
+R180
+W5
+L90
+F49
+W2
+L90
+S5
+W1
+S3
+F74
+W3
+F76
+W3
+N1
+E4
+S2
+F65
+E1
+W1
+F85
+E5
+F67
+R90
+W1
+F8
+R180
+N2
+F94
+W4
+R90
+S4
+F81
+N5
+R90
+E4
+F9
+N3
+F49
+R180
+N3
+F94
+N4
+R90
+S2
+F34
+W2
+E4
+W5
+F84
+S4
+W1
+R180
+W5
+F13
+W3
+L90
+N1
+E1
+R90
+S2
+R180
+F81
+L90
+F36
+W3
+S4
+W1
+F3
+R90
+W3
+R90
+E5
+R90
+F46
+L90
+F63
+N1
+L90
+W4
+N1
+S2
+N3
+L90
+F64
+W5
+N4
+F45
+W4
+S4
+R90
+W3
+N5
+R90
+L180
+E4
+R270
+S1
+L180
+E1
+F22
+E4
+F11
+W3
+F12
+W3
+R90
+S5
+E3
+F4
+R90
+S3
+F3
+L180
+S2
+F16
+E5
+R90
+F75
+W4
+N1
+S4
+F81
+L90
+F69
+N5
+L90
+W5
+F63
+N3
+W2
+L90
+F39
+E5
+F81
+W3
+F1
+L90
+F31
+W4
+S4
+F32
+W3
+N4
+L180
+N5
+W2
+F20
+L90
+F42
+S3
+E5
+S4
+L90
+S2
+L90
+S3
+W5
+F54
+R90
+F85
+S4
+F22
+W3
+F21
+R90
+F66
+S1
+F22
+S4
+W5
+N5
+E3
+F52
+L180
+W3
+S1
+W5
+R180
+F43
+E5
+F16
+W4
+S5
+W5
+N4
+F97
+E5
+F4
+E2
+R180
+F99
+S5
+F44
+R90
+W5
+N2
+L90
+F46
+E4
+L180
+E4
+R90
+F45
+R90
+S4
+F30
+R90
+F23
+N5
+W2
+F45
+S3
+F70
+L90
+W2
+F90
+W5
+F81
+E2
+F15
+L90
+F90
+N2
+F78
+S4
+F3
+F37
diff --git a/day12/problem_1.c b/day12/problem_1.c
new file mode 100644
index 0000000..1bb3df7
--- /dev/null
+++ b/day12/problem_1.c
@@ -0,0 +1,65 @@
+/* advent of code day 12, problem 1
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <math.h>
+
+#define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
+
+int main (int argc, char *argv[])
+{
+ FILE *infile;
+ double x = 0, y = 0, heading = 0;
+ char action;
+ int value;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ printf("Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ while (fscanf(infile, "%c%d\n", &action, &value) > 0) {
+ switch (action) {
+ case 'N':
+ y += value;
+ break;
+ case 'S':
+ y -= value;
+ break;
+ case 'E':
+ x += value;
+ break;
+ case 'W':
+ x -= value;
+ break;
+ case 'L':
+ heading += value;
+ break;
+ case 'R':
+ heading -= value;
+ break;
+ case 'F':
+ x += value * cos(degToRad(heading));
+ y += value * sin(degToRad(heading));
+ break;
+ }
+ }
+
+ if (fclose(infile) != 0) {
+ fprintf(stderr, "Could not close file %s", argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ printf("Manhattan sum: %d\n", abs((int)x) + abs((int)y));
+
+ return EXIT_SUCCESS;
+}
diff --git a/day12/problem_2.c b/day12/problem_2.c
new file mode 100644
index 0000000..b726bbb
--- /dev/null
+++ b/day12/problem_2.c
@@ -0,0 +1,78 @@
+/* advent of code day 12, problem 2
+ * by nytpu
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <math.h>
+
+#define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
+
+int main (int argc, char *argv[])
+{
+ FILE *infile;
+ double x = 0, y = 0;
+ double way_x = 10, way_y = 1;
+ char action;
+ int value;
+
+ int i;
+ double theta, cost, sint, way_x_sav, way_y_sav;
+
+ if (argc != 2) {
+ printf("Usage: problem_[x] [input]");
+ }
+
+ infile = fopen(argv[1], "r");
+ if (infile == NULL) {
+ printf("Unable to open file %s\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+
+ while (fscanf(infile, "%c%d\n", &action, &value) > 0) {
+ switch (action) {
+ case 'N':
+ way_y += value;
+ break;
+ case 'S':
+ way_y -= value;
+ break;
+ case 'E':
+ way_x += value;
+ break;
+ case 'W':
+ way_x -= value;
+ break;
+ case 'L':
+ theta = degToRad(value);
+ goto rad_rotate;
+ case 'R':
+ theta = degToRad(-value);
+ rad_rotate:
+ cost = cos(theta);
+ sint = sin(theta);
+ way_x_sav = way_x;
+ way_y_sav = way_y;
+ way_x = (way_x_sav*cost) - (way_y_sav*sint);
+ way_y = (way_x_sav*sint) + (way_y_sav*cost);
+ break;
+ case 'F':
+ for (i=0; i < value; ++i) {
+ x += way_x;
+ y += way_y;
+ }
+ break;
+ }
+ }
+
+ if (fclose(infile) != 0) {
+ fprintf(stderr, "Could not close file %s", argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ printf("Manhattan sum: %d\n", abs((int)x) + abs((int)y));
+
+ return EXIT_SUCCESS;
+}
diff --git a/day12/test_input.txt b/day12/test_input.txt
new file mode 100644
index 0000000..d382291
--- /dev/null
+++ b/day12/test_input.txt
@@ -0,0 +1,5 @@
+F10
+N3
+F7
+R90
+F11