aboutsummaryrefslogtreecommitdiff
path: root/day3
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-07 15:16:10 +0000
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-07 15:16:10 +0000
commit09cbfeeca74e6b7ca9a9b5d49646c538b8a3f4ba (patch)
treed91f437cc4b676155b7c110b8da8c90995523771 /day3
parent02d64d66b1e3c904b54c19ac2b5e9756d49959fb (diff)
Rearranged into folders, added makefile for convenience.
Diffstat (limited to 'day3')
l---------day3/Makefile1
-rw-r--r--day3/day3.c87
-rw-r--r--day3/day3.input1
-rw-r--r--day3/day3.readme27
4 files changed, 116 insertions, 0 deletions
diff --git a/day3/Makefile b/day3/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/day3/Makefile
@@ -0,0 +1 @@
+../Makefile \ No newline at end of file
diff --git a/day3/day3.c b/day3/day3.c
new file mode 100644
index 0000000..7179be6
--- /dev/null
+++ b/day3/day3.c
@@ -0,0 +1,87 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int** make2dWithOffset(int rows, int cols, int roff, int coff) {
+ int i;
+
+ int** arr = (int**) malloc(rows * sizeof(int*));
+ for(i = 0; i < rows ; i++)
+ arr[i] = (int*) malloc(cols * sizeof(int)) + coff;
+
+ return (arr + roff);
+}
+
+void free2dWithOffset(int** arr, int rows, int roff, int coff) {
+ int i;
+ for(i = -roff; i < rows - roff ; i++)
+ free(arr[i] - coff);
+ free(arr - roff);
+}
+
+int main() {
+ int** somap = make2dWithOffset(400, 400, 200, 200);
+ int** rsmap = make2dWithOffset(400, 400, 200, 200);
+ int ch, i, j, sosum = 0, rssum = 0, odd = 1;
+ int soeasting = 0, sonorthing = 0,
+ seasting = 0, snorthing = 0,
+ rseasting = 0, rsnorthing = 0;
+
+ for(i = -200; i < 200; i++)
+ for(j = -200; j < 200 ; j++) {
+ somap[i][j] = 0;
+ rsmap[i][j] = 0;
+ }
+
+ somap[0][0] = 1;
+ rsmap[0][0] = 2;
+
+ while((ch = getchar()) != EOF) {
+ switch(ch) {
+ case '<':
+ soeasting--;
+ if (odd)
+ seasting--;
+ else
+ rseasting--;
+ break;
+ case '>':
+ soeasting++;
+ if (odd)
+ seasting++;
+ else
+ rseasting++;
+ break;
+ case 'v':
+ sonorthing--;
+ if (odd)
+ snorthing--;
+ else
+ rsnorthing--;
+ break;
+ case '^':
+ sonorthing++;
+ if (odd)
+ snorthing++;
+ else
+ rsnorthing++;
+ }
+ odd = !odd;
+ somap[soeasting][sonorthing]++;
+ rsmap[seasting][snorthing]++;
+ rsmap[rseasting][rsnorthing]++;
+ }
+
+ for(i = -200; i < 200; i++)
+ for(j = -200; j < 200 ; j++) {
+ if(somap[i][j] > 0)
+ sosum++;
+ if(rsmap[i][j] > 0)
+ rssum++;
+ }
+
+ free2dWithOffset(somap, 400, 200, 200);
+ free2dWithOffset(rsmap, 400, 200, 200);
+
+ printf("Santa only: %d\nRobosanta: %d\n", sosum, rssum);
+ return 0;
+}
diff --git a/day3/day3.input b/day3/day3.input
new file mode 100644
index 0000000..f7791cc
--- /dev/null
+++ b/day3/day3.input
@@ -0,0 +1 @@
+v>v<vvv<<vv^v<v>vv>v<<<^^^^^<<^<vv>^>v^>^>^>^>^><vvvv<^>^<<^><<<^vvvv>^>^><^v^><^<>^^>^vvv^<vv>>^>^^<>><>^>vvv>>^vv>^<><>^<v^>^>^><vv^vv^>><<^><<v>><>^<^>>vvv>v>>>v<<^<><^<v<>v>^^v^^^<^v^^>>><^>^>v<>^<>>^>^^v^><v<v>>><>v<v^v>^v<>>^><v>^<>v^>^<>^v^^^v^^>>vv<<^^><^<vvv>^>^^<^>>^^^^^v^<v>vv<>>v^v<^v^^<><^<^vv^><>><><>v>vvv^vv^^<<><<vvv><<^v^><v<>vvv^<^>vvvv^>^>>^v^<v^vv<^^v<>v>vv^<>><v<<<^v^<<><v<^<^<><^^^>^>>v>^>v^<>v><^<^<v^>^^vv<^^<>v^v^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<^<<<<^^>v^v^^><<><^^^<v>v^^>>>vvv><>vv<>>^^v^v<<^>v^^v^>vv>^<<v<^<v^>^^<<v<^^>^v^^<^^v<<>>vv<^>>^><><>v>>v<>^<v^^><<>>>
diff --git a/day3/day3.readme b/day3/day3.readme
new file mode 100644
index 0000000..66077b3
--- /dev/null
+++ b/day3/day3.readme
@@ -0,0 +1,27 @@
+--- Day 3: Perfectly Spherical Houses in a Vacuum ---
+
+Santa is delivering presents to an infinite two-dimensional grid of houses.
+
+He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.
+
+However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?
+
+For example:
+
+ - > delivers presents to 2 houses: one at the starting location, and one to the east.
+ - ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
+ - ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.
+
+--- Part Two ---
+
+The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.
+
+Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.
+
+This year, how many houses receive at least one present?
+
+For example:
+
+ - ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
+ - ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
+ - ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.