From 09cbfeeca74e6b7ca9a9b5d49646c538b8a3f4ba Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Mon, 7 Dec 2015 15:16:10 +0000 Subject: Rearranged into folders, added makefile for convenience. --- day3/Makefile | 1 + day3/day3.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day3/day3.input | 1 + day3/day3.readme | 27 ++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 120000 day3/Makefile create mode 100644 day3/day3.c create mode 100644 day3/day3.input create mode 100644 day3/day3.readme (limited to 'day3') 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 +#include + +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>vvv>v<<<^^^^^<<^^>v^>^>^>^>^>^<<^><<<^vvvv>^>^><^v^><^<>^^>^vvv^>^>^^<>><>^>vvv>>^vv>^<><>^^>^>><<^><><>^<^>>vvv>v>>>v<<^<><^v>^^v^^^<^v^^>>><^>^>v<>^<>>^>^^v^>>><>v^v<>>^>^<>v^>^<>^v^^^v^^>>vv<<^^><^^>^^<^>>^^^^^v^vv<>>v^v<^v^^<><^<^vv^><>><><>v>vvv^vv^^<<><<<^v^>vvv^<^>vvvv^>^>>^v^v>vv^<>><^^^>^>>v>^>v^<>v><^<^^^vv<^^<>v^v^vv<>>>>v^v<>><^^v>vv^^>v^v>v>v>>vv>^^>^v><<^<vv^^^v>v^^^>><^^>v>^^v>>v^^^<^^v>^v>><^<^<>>v<<^^vv>^^^v<^<^^vv^>>v^>><<<>^vv^<^<>v^^<<^><>>^^^<^vv<^^^>><^^v>^^v^^^^<^v<^<<<<^v^<^^<>^^>^><<>>^v><>><^<^^^>>vv>^>^^^^^v^vvv><><^<^>v>v^v^>^><><^<^><>v<><>^v^^v>^<<<>^v^>^<v^<>>^vv>v>>>^<^>>>>>v>>^v>v><>>vvv<^^><<^>^>v<^vvvv<^^^v^^^>v^v<>v<^^v>>><>v>v>>^^<^^v><<<<<^vv<^<>^>>>^v>^v>vv>^v<>v>v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>^vv<<>>>>>vv<><>^<^v>vv^<>><<>^<>><^>>>><<>^^>><<<^^^^^v>>^<<>>vvvv<^v^vvv<<<^><>>>>vv^<^v>v<^<>^v>>^<^^v^>>><>^^<^v>>v<<>vvvv>^><>v^<>^<<^vv<^>>^v^>^^<<<^>>^^>^<^^<^<<v^^v^^<^v<^>>><<>vv<<^><^>vv<^>>^vv>>>^>>><^<<<>>^<<>><^<<^^^>>v^^>v<<<>v>v>v^<>>>^vvv><<^^<<><^v>>>>vv^^v^v<>v>v<<<<><<>vv<><^^^<>>v>>>>^^<><^<^v^>>^^v>^<v>^^>^v^<>>v^^<^v^^<<>^^>v^^>><<<<^<^^v>^^v>v<^>v^<>vv>>^^v>v^^>vvvvv<<>vv>vvvvvv>>v>>^^^vv^^><>v^^^^v>vv>v<^v>>>>^>^>^v>^>>><<>>^vv>>>><><<^<^><^vv^v>>>>>v>^>^^v^>v<^v^<^<v<>>^vv<<>^v^v>><><<>>v^^<<>^^<>v<<^^<^^>^^>^<^><>>v<>>^^<^>><<>>^v^>v>v<<^^<<^>v>v^^v^^<<>^v>v>v<^^>^v<><^<<v<^<<<<^^>v^v^^><<><^^^v^^>>>vvv><>vv<>>^^v^v<<^>v^^v^>vv>^<^^<^v^^<^^v<<>>vv<^>>^><><>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. -- cgit v1.2.1