diff options
-rw-r--r-- | src/2016/day1/aoc.cpp | 9 | ||||
-rw-r--r-- | src/2016/day1/aoc.h | 59 | ||||
-rw-r--r-- | src/2019/day1/aoc.h | 2 | ||||
-rw-r--r-- | test/test_2015.cpp | 46 | ||||
-rw-r--r-- | test/test_2016.cpp | 8 | ||||
-rw-r--r-- | test/test_2017.cpp | 2 | ||||
-rw-r--r-- | test/test_2018.cpp | 7 | ||||
-rw-r--r-- | test/test_2019.cpp | 2 | ||||
-rw-r--r-- | test/test_2020.cpp | 2 | ||||
-rw-r--r-- | test/test_2021.cpp | 2 |
10 files changed, 101 insertions, 38 deletions
diff --git a/src/2016/day1/aoc.cpp b/src/2016/day1/aoc.cpp index 2711d8f..8f79ece 100644 --- a/src/2016/day1/aoc.cpp +++ b/src/2016/day1/aoc.cpp @@ -17,10 +17,13 @@ instruction parse_day1(const char** pp) { return i; } -int day1(line_view file) { +std::pair<int, int> day1(line_view file) { position x{position::north, 0, 0}; std::vector<instruction> is; + bool found = false; + position first; + const char* p1 = file.line; const char* p2 = file.line + file.length; while (p1 < p2) { @@ -30,8 +33,8 @@ int day1(line_view file) { p1++; } } - position n = x.move(is); - return x.blocks(n); + position n = x.move(is, &first, &found); + return {x.blocks(n), x.blocks(first)}; } } // namespace aoc2016 diff --git a/src/2016/day1/aoc.h b/src/2016/day1/aoc.h index abcafe0..b6e556e 100644 --- a/src/2016/day1/aoc.h +++ b/src/2016/day1/aoc.h @@ -2,6 +2,7 @@ #include "common.h" #include <algorithm> #include <math.h> +#include <set> #include <vector> namespace aoc2016 { @@ -25,6 +26,7 @@ struct position { int x; int y; + bool operator<(const position& p) const noexcept { return x < p.x ? true : (x > p.x ? false : y < p.y); } int blocks(position p) const noexcept { return abs(p.x - x) + abs(p.y - y); } position move(instruction i) const noexcept { @@ -52,13 +54,64 @@ struct position { return next; } - position move(const std::vector<instruction>& is) const noexcept { + std::vector<position> line(position p1, position p2) const noexcept { + std::vector<position> ps; + auto copyx = [&p1, &p2, &ps](int x) { + if (x > 0) { + for (int i = p1.x; i < p2.x; i++) { + ps.push_back({p2.b, i, p1.y}); + } + } + if (x < 0) { + for (int i = p1.x; i > p2.x; i--) { + ps.push_back({p2.b, i, p1.y}); + } + } + }; + auto copyy = [&p1, &p2, &ps](int y) { + if (y > 0) { + for (int i = p1.y; i < p2.y; i++) { + ps.push_back({p2.b, p1.x, i}); + } + } + if (y < 0) { + for (int i = p1.y; i > p2.y; i--) { + ps.push_back({p2.b, p1.x, i}); + } + } + }; + copyx(p2.x - p1.x); + copyy(p2.y - p1.y); + return ps; + } + + void cross(position p1, position p2, position* first, bool* found) const noexcept { + static std::set<position> ps = {}; + if (!(*found)) { + auto v = line(p1, p2); + for (size_t i = 0; i < v.size(); i++) { + // printf("%d %d %d\n", int(v[i].b), v[i].x, v[i].y); + auto p = ps.insert(v[i]); + if (!p.second) { + *found = true; + *first = *p.first; + return; + } + } + } + } + + position move(const std::vector<instruction>& is, position* first, bool* found) const noexcept { position next = *this; - std::for_each(is.begin(), is.end(), [&next](const instruction& i) { next = next.move(i); }); + std::for_each(is.begin(), is.end(), [&next, first, found, this](const instruction& i) { + position prev = next; + next = next.move(i); + cross(prev, next, first, found); + }); return next; } }; -int day1(line_view); +std::pair<int, int> day1(line_view); } // namespace aoc2016 diff --git a/src/2019/day1/aoc.h b/src/2019/day1/aoc.h index 95823fe..959d6d8 100644 --- a/src/2019/day1/aoc.h +++ b/src/2019/day1/aoc.h @@ -1,6 +1,6 @@ #pragma once #include "common.h" -namespace aoc2016 { +namespace aoc2019 { } diff --git a/test/test_2015.cpp b/test/test_2015.cpp index 431b434..8b6c73a 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -23,13 +23,13 @@ #include "catch.hpp" #include <stdio.h> -TEST_CASE("Not Quite Lisp", "[day1]") { +TEST_CASE("Not Quite Lisp", "[2015]") { line_view lv = load_file("../src/2015/day1/input"); REQUIRE(aoc2015::day1(lv) == 74); REQUIRE(aoc2015::day1(lv, -1) == 1795); } -TEST_CASE("parse box", "[day2]") { +TEST_CASE("parse box", "[2015]") { line_view lv1{"27x2x5\n", 7}; auto b1 = aoc2015::parse_day2(lv1); auto a1 = b1.l == 27 && b1.w == 2 && b1.h == 5; @@ -40,21 +40,21 @@ TEST_CASE("parse box", "[day2]") { REQUIRE(a2); } -TEST_CASE("I Was Told There Would Be No Math", "[day2]") { +TEST_CASE("I Was Told There Would Be No Math", "[2015]") { line_view lv = load_file("../src/2015/day2/input"); auto p = aoc2015::day2(lv); REQUIRE(p.first == 1586300); REQUIRE(p.second == 3737498); } -TEST_CASE("Perfectly Spherical Houses in a Vacuum", "[day3]") { +TEST_CASE("Perfectly Spherical Houses in a Vacuum", "[2015]") { line_view lv = load_file("../src/2015/day3/input"); auto p = aoc2015::day3(lv); REQUIRE(p.first == 2565); REQUIRE(p.second == 2639); } -TEST_CASE("The Ideal Stocking Stuffer", "[day4]") { +TEST_CASE("The Ideal Stocking Stuffer", "[2015]") { char s1[] = "abcdef609043"; char s2[] = "pqrstuv1048970"; char s3[] = "bgvyzdsv254575"; @@ -66,7 +66,7 @@ TEST_CASE("The Ideal Stocking Stuffer", "[day4]") { REQUIRE(aoc2015::lead_zeros(aoc2015::md5sum(s4)) >= 6); } -TEST_CASE("Doesn't He Have Intern-Elves For This?", "[day5]") { +TEST_CASE("Doesn't He Have Intern-Elves For This?", "[2015]") { line_view lv{"adcccfadd", 9}; REQUIRE(aoc2015::count_vowels(lv, "aeiou") == 2); REQUIRE(aoc2015::count_vowels(lv, "eiou") == 0); @@ -89,7 +89,7 @@ TEST_CASE("Doesn't He Have Intern-Elves For This?", "[day5]") { REQUIRE(p.second == 55); } -TEST_CASE("Probably a Fire Hazard", "[day6]") { +TEST_CASE("Probably a Fire Hazard", "[2015]") { /* aoc2015::grid<aoc2015::Bit, 1000> grid; grid.turn_on({0, 0}, {0, 999}); @@ -106,7 +106,7 @@ TEST_CASE("Probably a Fire Hazard", "[day6]") { REQUIRE(14687245 == p.second); } -TEST_CASE("Some Assembly Required", "[day7]") { +TEST_CASE("Some Assembly Required", "[2015]") { aoc2015::cals cals; cals.parse("1 -> ab"); cals.parse("8 RSHIFT ab -> x"); @@ -127,38 +127,38 @@ TEST_CASE("Some Assembly Required", "[day7]") { REQUIRE(14710 == r2.value); } -TEST_CASE("Matchsticks", "[day8]") { +TEST_CASE("Matchsticks", "[2015]") { line_view lv = load_file("../src/2015/day8/input"); auto p = aoc2015::day8(lv); REQUIRE(1371 == p.first); REQUIRE(2117 == p.second); } -TEST_CASE("All in a Single Night", "[day9]") { +TEST_CASE("All in a Single Night", "[2015]") { line_view lv = load_file("../src/2015/day9/input"); auto p = aoc2015::day9(lv); REQUIRE(117 == p.first); REQUIRE(909 == p.second); } -TEST_CASE("Elves Look, Elves Say", "[day10]") { +TEST_CASE("Elves Look, Elves Say", "[2015]") { REQUIRE(329356 == aoc2015::day10("3113322113", 40)); REQUIRE(4666278 == aoc2015::day10("3113322113", 50)); } -TEST_CASE("Corporate Policy", "[day11]") { +TEST_CASE("Corporate Policy", "[2015]") { REQUIRE(strcmp(aoc2015::day11("hxbxwxba"), "hxbxxyzz") == 0); REQUIRE(strcmp(aoc2015::day11("hxbxxyzz"), "hxcaabcc") == 0); } -TEST_CASE("JSAbacusFramework.io", "[day12]") { +TEST_CASE("JSAbacusFramework.io", "[2015]") { // printf("%d\n", aoc2015::day12_part2("[{ 5 {red1} {3{4 {10}}3}} 10 red 10]")); line_view lv = load_file("../src/2015/day12/input"); REQUIRE(156366 == aoc2015::day12(lv)); REQUIRE(96852 == aoc2015::day12_part2(lv)); } -TEST_CASE("Knights of the Dinner Table", "[day13]") { +TEST_CASE("Knights of the Dinner Table", "[2015]") { line_view lv = load_file("../src/2015/day13/input"); auto p1 = aoc2015::day13(lv); REQUIRE(-638 == p1.first); @@ -169,41 +169,41 @@ TEST_CASE("Knights of the Dinner Table", "[day13]") { REQUIRE(668 == p2.second); } -TEST_CASE("Reindeer Olympics", "[day14]") { +TEST_CASE("Reindeer Olympics", "[2015]") { line_view lv = load_file("../src/2015/day14/input"); auto p = aoc2015::day14(lv, 2503); REQUIRE(2640 == p.first); REQUIRE(1102 == p.second); } -TEST_CASE("Science for Hungry People", "[day15]") { +TEST_CASE("Science for Hungry People", "[2015]") { line_view lv = load_file("../src/2015/day15/input"); REQUIRE(21367368 == aoc2015::day15(lv)); int cals = 500; REQUIRE(1766400 == aoc2015::day15(lv, &cals)); } -TEST_CASE("Aunt Sue", "[day16]") { +TEST_CASE("Aunt Sue", "[2015]") { line_view lv = load_file("../src/2015/day16/input"); auto p = aoc2015::day16(lv); REQUIRE(103 == p.first); REQUIRE(405 == p.second); } -TEST_CASE("No Such Thing as Too Much", "[day17]") { +TEST_CASE("No Such Thing as Too Much", "[2015]") { line_view lv = load_file("../src/2015/day17/input"); auto p = aoc2015::day17(lv, 150); REQUIRE(654 == p.first); REQUIRE(57 == p.second); } -TEST_CASE("Like a GIF For Your Yard", "[day18]") { +TEST_CASE("Like a GIF For Your Yard", "[2015]") { line_view lv = load_file("../src/2015/day18/input"); // REQUIRE(821 == aoc2015::day18(lv, 100)); REQUIRE(886 == aoc2015::day18(lv, 100)); } -TEST_CASE("Medicine for Rudolph", "[day19]") { +TEST_CASE("Medicine for Rudolph", "[2015]") { line_view lv = load_file("../src/2015/day19/input"); // line_view lv = line_view{"e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOHOHO\n"}; auto p = aoc2015::day19(lv); @@ -211,17 +211,17 @@ TEST_CASE("Medicine for Rudolph", "[day19]") { REQUIRE(195 == p.second); } -TEST_CASE("Infinite Elves and Infinite Houses", "[day20]") { +TEST_CASE("Infinite Elves and Infinite Houses", "[2015]") { auto p = aoc2015::day20(36000000, 10); REQUIRE(831600 == p.first); REQUIRE(884520 == p.second); } -TEST_CASE("RPG Simulator 20XX", "[day21]") { +TEST_CASE("RPG Simulator 20XX", "[2015]") { auto p = aoc2015::day21(); REQUIRE(91 == p.first); REQUIRE(158 == p.second); } -TEST_CASE("Wizard Simulator 20XX", "[day22]") { +TEST_CASE("Wizard Simulator 20XX", "[2015]") { } diff --git a/test/test_2016.cpp b/test/test_2016.cpp index f0f454f..75ed006 100644 --- a/test/test_2016.cpp +++ b/test/test_2016.cpp @@ -2,9 +2,9 @@ #include "catch.hpp" #include <stdio.h> -TEST_CASE("No Time for a Taxicab", "[day1]") { +TEST_CASE("No Time for a Taxicab", "[2016]") { line_view lv = load_file("../src/2016/day1/input"); - REQUIRE(298 == aoc2016::day1(lv)); - // printf("%d\n", aoc2016::day1("R2, R2, R2")); - // printf("%d\n", aoc2016::day1("R5, L5, R5, R3")); + auto p = aoc2016::day1(lv); + REQUIRE(298 == p.first); + REQUIRE(158 == p.second); } diff --git a/test/test_2017.cpp b/test/test_2017.cpp index d062697..be7c947 100644 --- a/test/test_2017.cpp +++ b/test/test_2017.cpp @@ -2,6 +2,6 @@ #include "catch.hpp" #include <stdio.h> -TEST_CASE("", "[day1]") { +TEST_CASE("", "[2017]") { // line_view lv = load_file("../src/2017/day1/input"); } diff --git a/test/test_2018.cpp b/test/test_2018.cpp index e69de29..ac15881 100644 --- a/test/test_2018.cpp +++ b/test/test_2018.cpp @@ -0,0 +1,7 @@ +#include "2018/day1/aoc.h" +#include "catch.hpp" +#include <stdio.h> + +TEST_CASE("", "[2018]") { + // line_view lv = load_file("../src/2018/day1/input"); +} diff --git a/test/test_2019.cpp b/test/test_2019.cpp index 428c067..287f50e 100644 --- a/test/test_2019.cpp +++ b/test/test_2019.cpp @@ -2,6 +2,6 @@ #include "catch.hpp" #include <stdio.h> -TEST_CASE("", "[day1]") { +TEST_CASE("", "[2019]") { // line_view lv = load_file("../src/2019/day1/input"); } diff --git a/test/test_2020.cpp b/test/test_2020.cpp index e1bb716..fd4039f 100644 --- a/test/test_2020.cpp +++ b/test/test_2020.cpp @@ -2,6 +2,6 @@ #include "catch.hpp" #include <stdio.h> -TEST_CASE("", "[day1]") { +TEST_CASE("", "[2020]") { // line_view lv = load_file("../src/2020/day1/input"); } diff --git a/test/test_2021.cpp b/test/test_2021.cpp index 4709dac..dfc312d 100644 --- a/test/test_2021.cpp +++ b/test/test_2021.cpp @@ -2,6 +2,6 @@ #include "catch.hpp" #include <stdio.h> -TEST_CASE("", "[day1]") { +TEST_CASE("", "[2021]") { // line_view lv = load_file("../src/2021/day1/input"); } |