aboutsummaryrefslogtreecommitdiff
path: root/2020/12
diff options
context:
space:
mode:
Diffstat (limited to '2020/12')
-rw-r--r--2020/12/.gitignore1
-rw-r--r--2020/12/Makefile7
-rw-r--r--2020/12/input778
-rw-r--r--2020/12/puzzle-1.c73
-rw-r--r--2020/12/puzzle-2.c79
5 files changed, 938 insertions, 0 deletions
diff --git a/2020/12/.gitignore b/2020/12/.gitignore
new file mode 100644
index 0000000..60d075d
--- /dev/null
+++ b/2020/12/.gitignore
@@ -0,0 +1 @@
+puzzle-[12]
diff --git a/2020/12/Makefile b/2020/12/Makefile
new file mode 100644
index 0000000..cf466dc
--- /dev/null
+++ b/2020/12/Makefile
@@ -0,0 +1,7 @@
+all:
+ ${CC} ${CFLAGS} -o puzzle-1 puzzle-1.c
+ ${CC} ${CFLAGS} -o puzzle-2 puzzle-2.c
+
+.PHONY: clean
+clean:
+ rm -f puzzle-[12]
diff --git a/2020/12/input b/2020/12/input
new file mode 100644
index 0000000..5e5e7be
--- /dev/null
+++ b/2020/12/input
@@ -0,0 +1,778 @@
+R90
+N2
+N2
+F94
+R90
+E3
+F2
+L90
+L90
+F66
+S5
+L90
+W3
+R90
+N3
+W2
+N2
+W1
+R90
+E3
+F100
+S4
+F66
+R180
+F96
+R90
+N2
+R90
+E2
+S3
+E1
+F65
+W4
+F6
+L180
+W2
+L90
+F95
+L90
+F97
+E2
+F36
+E2
+R90
+E4
+F98
+N4
+F70
+S1
+R270
+E1
+F13
+R90
+W1
+L90
+F82
+S2
+R90
+N2
+F80
+N1
+R90
+F26
+W4
+F23
+S5
+F72
+N5
+W2
+N5
+F47
+L270
+F18
+W1
+L90
+F91
+R90
+W5
+N2
+F60
+E5
+S3
+R90
+R180
+S3
+L90
+W4
+F80
+S2
+L90
+F31
+W1
+L90
+F45
+S3
+F64
+N1
+W4
+N2
+R180
+W3
+R90
+S1
+F7
+W1
+R180
+W1
+S4
+E4
+W4
+F87
+R90
+N5
+F65
+W4
+F15
+E5
+N5
+L90
+F36
+E1
+F70
+E1
+L180
+F42
+L90
+F9
+L90
+E4
+F2
+F35
+N4
+E4
+N2
+L90
+N4
+R90
+F100
+S3
+F13
+E1
+S4
+F66
+L90
+F75
+E5
+R180
+E2
+L90
+W4
+F89
+S5
+R90
+E4
+F5
+R270
+E3
+S5
+R90
+F65
+S5
+R180
+S3
+W4
+F68
+W1
+F62
+W1
+F99
+F96
+R90
+E1
+R90
+L180
+F96
+W4
+N2
+W2
+R90
+F32
+N1
+E1
+L90
+E1
+S3
+F6
+S4
+E1
+N2
+W5
+S5
+E4
+F3
+F58
+N1
+F96
+E5
+F31
+R90
+F74
+R180
+N2
+W1
+F65
+L90
+F45
+F73
+R90
+S1
+R270
+N5
+L90
+W1
+N3
+L90
+F36
+R90
+N4
+L180
+W4
+N5
+R180
+N1
+R90
+N1
+R270
+F80
+R90
+S5
+E3
+F49
+W5
+F27
+E1
+N2
+W4
+S3
+L90
+F100
+N1
+F28
+E2
+F88
+S1
+R90
+F14
+S4
+L180
+F93
+E1
+L90
+F64
+L90
+L90
+F84
+E4
+F69
+W1
+R90
+E5
+N2
+W1
+N3
+F10
+S2
+F64
+F10
+R180
+W3
+N1
+F82
+S2
+W1
+R90
+W1
+L90
+W2
+N3
+F95
+E4
+N3
+N2
+E3
+E1
+N1
+W5
+R90
+W3
+S3
+E3
+L90
+W3
+N5
+F61
+S3
+F50
+F71
+E1
+E1
+F63
+L90
+W5
+S1
+R90
+F29
+L90
+S1
+R90
+S1
+L90
+W4
+N4
+R270
+S1
+F87
+R90
+F82
+W1
+L90
+S4
+L90
+W2
+N4
+R90
+F61
+R90
+N5
+F38
+W3
+L90
+F56
+N1
+R90
+W1
+S2
+F97
+S5
+R90
+E1
+F97
+R270
+N2
+L180
+F14
+E1
+R90
+F70
+L180
+S4
+E3
+L90
+N3
+F87
+E3
+N3
+R90
+F59
+F18
+W5
+F71
+E3
+S5
+W3
+R180
+F71
+S3
+W5
+F47
+N5
+L90
+F79
+N3
+F40
+W5
+S2
+F59
+W3
+F2
+W1
+F63
+R90
+S4
+L90
+S1
+E5
+F33
+L180
+F83
+E5
+S3
+W1
+F66
+R270
+N4
+F29
+W5
+F83
+W1
+R90
+N5
+W3
+F58
+S4
+R90
+F36
+S1
+F67
+W1
+S4
+F56
+E2
+F88
+N4
+L90
+F71
+W3
+F88
+R180
+F50
+R90
+E4
+R90
+S1
+W2
+L90
+N1
+R90
+E5
+N1
+F39
+L180
+E5
+R90
+W4
+L180
+W3
+R90
+W3
+L90
+S5
+W5
+S2
+L90
+N3
+W5
+R180
+F74
+E1
+N1
+N4
+R90
+N2
+W4
+F34
+L90
+E4
+L90
+S4
+L90
+W3
+F83
+N1
+L90
+F58
+W5
+F38
+L180
+N1
+L90
+S4
+F45
+E4
+S3
+L90
+E1
+S2
+F30
+E2
+L90
+F84
+W4
+F53
+R90
+S5
+F31
+L180
+N1
+L90
+E2
+L180
+W3
+F98
+R270
+S2
+E5
+F84
+S4
+L90
+F26
+E2
+F66
+L270
+E1
+L180
+F11
+E5
+L90
+W1
+F11
+E3
+F70
+E5
+N3
+F78
+W5
+L270
+W5
+N1
+L90
+S4
+F8
+L90
+F95
+L90
+F99
+S3
+F61
+N2
+F84
+S5
+F3
+E5
+R90
+F33
+W5
+S1
+L90
+F52
+E4
+L90
+E1
+N1
+L270
+S3
+W4
+R180
+F89
+R270
+F10
+R90
+N1
+R180
+W5
+N2
+F78
+S5
+F25
+S4
+F25
+N3
+F29
+W1
+F24
+S4
+L90
+S5
+F17
+L90
+S4
+E2
+N3
+F38
+S4
+L90
+F1
+L90
+E2
+F92
+L90
+E4
+S1
+E1
+F28
+N1
+F46
+R90
+N1
+W4
+N3
+E3
+F90
+N5
+L180
+S1
+E3
+F96
+L90
+E1
+R90
+N5
+E1
+F74
+L180
+N2
+L90
+N1
+E4
+R90
+E1
+S2
+F91
+R90
+N5
+E4
+F9
+N2
+E1
+F13
+E2
+S4
+W1
+R90
+S1
+R90
+F18
+L90
+F12
+S2
+F65
+N2
+E3
+F65
+E1
+L180
+E2
+S4
+W4
+F89
+N4
+E5
+L180
+N4
+W1
+F22
+L90
+W2
+S2
+F6
+E4
+F74
+N2
+E2
+S1
+R90
+F87
+W5
+R90
+W4
+R90
+E4
+L90
+N3
+F68
+L180
+N1
+W1
+F81
+R90
+F95
+R90
+R180
+E5
+F51
+F23
+F74
+N3
+F16
+F64
+L90
+W4
+R90
+R90
+W4
+F97
+R180
+N3
+F79
+E3
+L90
+N2
+F14
+S4
+F41
+W2
+L90
+F54
+N3
+E4
+R90
+W2
+R90
+S4
+L90
+L90
+S4
+R90
+E4
+S3
+L90
+F5
+R90
+L90
+W2
+R90
+W3
+N4
+E5
+F14
+E5
+S4
+F96
+S4
+L90
+W2
+F96
+L90
+E3
+L90
+F4
+R90
+F61
+E2
+F56
+E1
+F100
+E5
+R90
+F54
+R90
+E4
+F2
+R180
+S3
+F8
+L90
+W1
+R90
+F19
+W5
+L90
+S1
+L90
+W5
+S2
+F30
+R90
+W5
+F31
+S5
+E1
+N2
+F100
+N1
+F3
+W3
+F46
+R90
+E2
+F62
+L180
+F90
+L180
+S3
+L90
+S5
+F40
+N4
+F95 \ No newline at end of file
diff --git a/2020/12/puzzle-1.c b/2020/12/puzzle-1.c
new file mode 100644
index 0000000..1a4c7ab
--- /dev/null
+++ b/2020/12/puzzle-1.c
@@ -0,0 +1,73 @@
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main(void)
+{
+ FILE *fp;
+ char cl[5];
+ int north, east;
+ int direction = 0;
+
+ if (!(fp = fopen("input", "r")))
+ err(EXIT_FAILURE, "fopen");
+
+ north = east = 0;
+ while (fgets(cl, sizeof(cl), fp) != NULL) {
+ switch (cl[0]) {
+ case 'N':
+ north += atoi(&cl[1]);
+ break;
+ case 'E':
+ east += atoi(&cl[1]);
+ break;
+ case 'S':
+ north -= atoi(&cl[1]);
+ break;
+ case 'W':
+ east -= atoi(&cl[1]);
+ break;
+ case 'L':
+ direction -= atoi(&cl[1]);
+ goto wrap;
+ case 'R':
+ direction += atoi(&cl[1]);
+
+wrap:
+ /* 0 <= direction < 360 */
+ if (direction >= 360)
+ direction -= 360;
+ else if (direction < 0)
+ direction += 360;
+ break;
+ case 'F':
+ switch (direction) {
+ case 0:
+ east += atoi(&cl[1]);
+ break;
+ case 90:
+ north -= atoi(&cl[1]);
+ break;
+ case 180:
+ east -= atoi(&cl[1]);
+ break;
+ case 270:
+ north += atoi(&cl[1]);
+ break;
+ }
+ break;
+ }
+ }
+ fclose(fp);
+
+ /* Absolute value */
+ if (north < 0)
+ north = -north;
+ if (east < 0)
+ east = -east;
+
+ printf("%d\n", north + east);
+ return EXIT_SUCCESS;
+}
diff --git a/2020/12/puzzle-2.c b/2020/12/puzzle-2.c
new file mode 100644
index 0000000..8ac34b4
--- /dev/null
+++ b/2020/12/puzzle-2.c
@@ -0,0 +1,79 @@
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct coords {
+ int north;
+ int east;
+};
+
+int
+main(void)
+{
+ FILE *fp;
+ char cl[6];
+ struct coords waypoint = {1, 10};
+ struct coords ship = {0, 0};
+
+ if (!(fp = fopen("input", "r")))
+ err(EXIT_FAILURE, "fopen");
+
+ while (fgets(cl, sizeof(cl), fp) != NULL) {
+ switch (cl[0]) {
+ case 'N':
+ waypoint.north += atoi(&cl[1]);
+ break;
+ case 'E':
+ waypoint.east += atoi(&cl[1]);
+ break;
+ case 'S':
+ waypoint.north -= atoi(&cl[1]);
+ break;
+ case 'W':
+ waypoint.east -= atoi(&cl[1]);
+ break;
+ case 'F':;
+ unsigned int const val = atoi(&cl[1]);
+ ship.north += val * waypoint.north;
+ ship.east += val * waypoint.east;
+ break;
+ /* Left and right */
+ default:;
+ int direction = atoi(&cl[1]);
+
+ /* (x, y) -> (-x, -y) */
+ if (direction == 180) {
+ waypoint.north = -waypoint.north;
+ waypoint.east = -waypoint.east;
+ break;
+ }
+
+ /* 0 <= direction < 360 */
+ if (cl[0] == 'R')
+ direction = -direction + 360;
+
+ int const temp = waypoint.east;
+ if (direction == 90) {
+ /* (x, y) -> (-y, x) */
+ waypoint.east = -waypoint.north;
+ waypoint.north = temp;
+ } else {
+ /* (x, y) -> (y, -x) */
+ waypoint.east = waypoint.north;
+ waypoint.north = -temp;
+ }
+ break;
+ }
+ }
+ fclose(fp);
+
+ /* Absolute value */
+ if (ship.north < 0)
+ ship.north = -ship.north;
+ if (ship.east < 0)
+ ship.east = -ship.east;
+
+ printf("%d\n", ship.north + ship.east);
+ return EXIT_SUCCESS;
+}