From de5c4ac25c8a19338b00b33bcea96d198a715e34 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 22 Mar 2018 07:46:02 -0500 Subject: [PATCH] C: 2015 day 03 complete --- 2015/day02/day02.c | 2 -- 2015/day03/day03.c | 72 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/2015/day02/day02.c b/2015/day02/day02.c index a54b3f2..d4f990e 100644 --- a/2015/day02/day02.c +++ b/2015/day02/day02.c @@ -37,7 +37,6 @@ int getBoxArea(int l, int w, int h) { void part1() { printf("Part 1\n"); - char str[100]; int l, w, h, result = 0; while(scanf("%dx%dx%d", &l, &w, &h) > 0) { result += getBoxSidesArea(l, w, h) + getSmallestSideArea(l, w, h); @@ -47,7 +46,6 @@ void part1() { void part2() { printf("Part 2\n"); - char str[100]; int l, w, h, result = 0; while(scanf("%dx%dx%d", &l, &w, &h) > 0) { result += getSmallestPerimeter(l, w, h) + getBoxArea(l, w, h); diff --git a/2015/day03/day03.c b/2015/day03/day03.c index 758472b..6977f8a 100644 --- a/2015/day03/day03.c +++ b/2015/day03/day03.c @@ -2,6 +2,7 @@ void part1() { printf("Part 1\n"); + char ch = getchar(); int visits[1000][1000]; for(int i = 0; i < 1000; i++) { @@ -11,7 +12,6 @@ void part1() { } int x = 0, y = 0, minx = 0, maxx = 0, miny = 0, maxy = 0; int inc = 0; - printf("(%4d,%4d) (%4d:%4d,%4d:%4d)", x, y, minx, maxx, miny, maxy); while(ch) { visits[x+500][y+500]++; if(ch == '\n') { break; } @@ -56,6 +56,76 @@ void part1() { void part2() { printf("Part 2\n"); + char ch = getchar(); + int visits[1000][1000]; + for(int i = 0; i < 1000; i++) { + for(int j = 0; j < 1000; j++) { + visits[i][j] = 0; + } + } + int x1 = 0, y1 = 0, x2 = 0, y2 = 0, minx = 0, maxx = 0, miny = 0, maxy = 0; + int inc = 0; + int robo = -1; + int lastOutLngth = 0; + while(ch) { + robo++; + int usex = x1, usey = y1; + if(robo % 2 == 1) { + usex = x2; + usey = y2; + } + visits[usex+500][usey+500]++; + if(ch == '\n') { break; } + + switch(ch) { + case '>': + usex++; + break; + case 'v': + usey--; + break; + case '<': + usex--; + break; + case '^': + usey++; + break; + } + + if(usex > maxx) { + maxx = usex; + } + if(usex < minx) { + minx = usex; + } + if(usey > maxy) { + maxy = usey; + } + if(usey < miny) { + miny = usey; + } + // Get the next char + if(robo % 2 == 1) { + x2 = usex; + y2 = usey; + } else { + x1 = usex; + y1 = usey; + } + ch = getchar(); + } + + // Now find how many houses got presents + int result = 0; + for(int i = 0; i < 1000; i++) { + for(int j = 0; j < 1000; j++) { + if(visits[i][j] > 0) { + result++; + } + } + } + + printf("%d houses\n", result); } int main(int argc, char **argv) {