advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit 4f5bb0681794bfa82e526a5e8c7837dd81da9023
parent c84db215e871fcc62e5b464ba493f6575df19d80
Author: bsandro <email@bsandro.tech>
Date:   Sat,  7 Dec 2024 23:56:46 +0200

Day 06 cleanup to prepare solving p2

Diffstat:
Mday01/main.cpp | 2+-
Mday02/main.cpp | 2+-
Mday03/main.cpp | 2+-
Mday04/main.cpp | 2+-
Mday05/main.cpp | 2+-
Mday06/main.cpp | 178+++++++++++++++++++------------------------------------------------------------
Mday07/main.cpp | 2+-
Mtpl/main.cpp | 2+-
8 files changed, 49 insertions(+), 143 deletions(-)

diff --git a/day01/main.cpp b/day01/main.cpp @@ -51,7 +51,7 @@ int part2(Data &input) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 01 " << fname << std::endl; + std::cout << "AoC 2024 day 01 " << fname << std::endl; Data input = read_file<Data>(fname); std::cout << "part1: " << part1(input) << std::endl; std::cout << "part2: " << part2(input) << std::endl; diff --git a/day02/main.cpp b/day02/main.cpp @@ -76,7 +76,7 @@ int part2(Data &input) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 02 " << fname << std::endl; + std::cout << "AoC 2024 day 02 " << fname << std::endl; Data input = read_file<Data>(fname); std::cout << "part1: " << part1(input) << std::endl; std::cout << "part2: " << part2(input) << std::endl; diff --git a/day03/main.cpp b/day03/main.cpp @@ -43,7 +43,7 @@ int part2(std::string input) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 03 " << fname << std::endl; + std::cout << "AoC 2024 day 03 " << fname << std::endl; std::string input = read_file(fname); std::cout << "part1: " << part1(input) << std::endl; std::cout << "part2: " << part2(input) << std::endl; diff --git a/day04/main.cpp b/day04/main.cpp @@ -113,7 +113,7 @@ int part2(Data &input) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 04 " << fname << std::endl; + std::cout << "AoC 2024 day 04 " << fname << std::endl; Data input = read_file<Data>(fname); std::cout << "part1: " << part1(input) << std::endl; std::cout << "part2: " << part2(input) << std::endl; diff --git a/day05/main.cpp b/day05/main.cpp @@ -108,7 +108,7 @@ int part2(Game &g) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 05 " << fname << std::endl; + std::cout << "AoC 2024 day 05 " << fname << std::endl; Game game = read_file(fname); std::cout << "part1: " << part1(game) << std::endl; std::cout << "part2: " << part2(game) << std::endl; diff --git a/day06/main.cpp b/day06/main.cpp @@ -10,18 +10,17 @@ #include <expected> #include <ranges> -typedef std::vector<std::string> Data; - -typedef std::pair<int,int> Vec2; // x, y - enum class Dir { up, down, left, - right, - out + right }; +typedef std::vector<std::string> Data; +typedef std::pair<int,int> Vec2; // x, y +typedef std::tuple<int,int,Dir> Vec3; + //@todo implement a generic map<width, height> class template<typename T> @@ -40,10 +39,10 @@ T read_file(const std::string &path) { return buf; } -std::expected<Vec2, std::string> find_guard(const Data &m) { +std::expected<Vec3, std::string> find_guard(const Data &m) { for (int y=0; y<(int)m.size(); ++y) { for (int x=0; x<(int)m[y].size(); ++x) { - if (m[y][x]=='^') return Vec2{x,y}; + if (m[y][x]=='^') return Vec3(x, y, Dir::up); } } return std::unexpected("no guard found"); @@ -56,137 +55,68 @@ static const char * str(Dir d) { case Dir::down: return "down"; case Dir::left: return "left"; case Dir::right: return "right"; - case Dir::out: return "out"; } return "NULL"; } -bool dirs_angled(Dir d1, Dir d2) { - switch (d1) { - case Dir::up: return d2==Dir::right; - case Dir::down: return d2==Dir::left; - case Dir::left: return d2==Dir::up; - case Dir::right: return d2==Dir::down; - case Dir::out: return false; - } - return false; -} - -std::pair<Vec2, Dir> get_next(const Data &m, const Vec2 &p, Dir d) { - Vec2 np = p; - Dir nd = d; +std::expected<Vec3, std::string> get_next(const Data &m, const Vec3 &p) { + Vec3 np = p; + auto [x,y,d] = np; switch (d) { case Dir::up: - if (np.second-1>=0) { - if (m[np.second-1][np.first]=='#') { - nd=Dir::right; + if (y-1>=0) { + if (m[y-1][x]=='#') { + d=Dir::right; } else { - np.second--; + y--; } } else { - return {np, Dir::out}; + return std::unexpected("out"); } break; case Dir::down: - if (np.second+1<(int)m.size()) { - if (m[np.second+1][np.first]=='#') { - nd=Dir::left; + if (y+1<(int)m.size()) { + if (m[y+1][x]=='#') { + d=Dir::left; } else { - np.second++; + y++; } } else { - return {np, Dir::out}; + return std::unexpected("out"); } break; case Dir::left: - if (np.first-1>=0) { - if (m[np.second][np.first-1]=='#') { - nd=Dir::up; + if (x-1>=0) { + if (m[y][x-1]=='#') { + d=Dir::up; } else { - np.first--; + x--; } } else { - return {np, Dir::out}; + return std::unexpected("out"); } break; case Dir::right: - if (np.first+1<(int)m.size()) { - if (m[np.second][np.first+1]=='#') { - nd=Dir::down; + if (x+1<(int)m.size()) { + if (m[y][x+1]=='#') { + d=Dir::down; } else { - np.first++; + x++; } } else { - return {np, Dir::out}; + return std::unexpected("out"); } break; - case Dir::out: - return {p, d}; } //printf("%d,%d(%s) -> %d,%d(%s)\n", p.first, p.second, str(d), np.first, np.second, str(nd)); - return {np, nd}; + return Vec3(x,y,d); } // works only on square input -void traverse(const Data &m, std::vector<std::tuple<int,int,Dir>> &visited, const Vec2 &p, Dir d) { - visited.push_back(std::make_tuple(p.first, p.second, d)); - auto [np, nd] = get_next(m, p, d); - if (nd==Dir::out) return; - traverse(m, visited, np, nd); -} - -//@todo make Data the proper class with methods -template <typename Iter> -bool find_line(const Data &m, const Iter &begin, const Iter &end, Vec2 p, Dir d) { - auto [x, y] = p; - //const auto [x1, y1, d1] = *begin; - //const auto [x2, y2, d2] = *end; - //std::printf("find_line(%d:%d:%s) %d:%d(%s) -> %d:%d(%s)\n\t",x,y,str(d),x1,y1,str(d1),x2,y2,str(d2)); - switch (d) { - case Dir::up: { // look for path to the right - for (++x;x<(int)m.size();++x) { - if (m[y][x]=='#') return false; - return std::count_if(begin, end, [&x,&y](const auto &v){ - auto [vx,vy,vd] = v; - return vx==x&&vy==y&&vd==Dir::right; - })>0; - } - } - break; - case Dir::down: { - for (--x;x>=0;--x) { - if (m[y][x]=='#') return false; - return std::count_if(begin, end, [&x,&y](const auto &v){ - auto [vx,vy,vd] = v; - return vx==x&&vy==y&&vd==Dir::left; - })>0; - } - } - break; - case Dir::left: { - for (--y;y>=0;--y) { - if (m[y][x]=='#') return false; - return std::count_if(begin, end, [&x,&y](const auto &v){ - auto [vx,vy,vd] = v; - return vx==x&&vy==y&&vd==Dir::up; - })>0; - } - } - break; - case Dir::right: { - for (++y;y<(int)m.size();++y) { - if (m[y][x]=='#') return false; - return std::count_if(begin, end, [&x,&y](const auto &v){ - auto [vx,vy,vd] = v; - return vx==x&&vy==y&&vd==Dir::right; - })>0; - } - } - break; - case Dir::out: - break; - } - return false; +void traverse(const Data &m, std::vector<Vec3> &visited, const Vec3 &p) { + visited.push_back(p); + auto np = get_next(m, p); + if (np.has_value()) traverse(m, visited, np.value()); } std::expected<Vec2, bool> find_wall(const Data &m, Vec2 p, Dir d) { @@ -218,33 +148,23 @@ std::expected<Vec2, bool> find_wall(const Data &m, Vec2 p, Dir d) { return std::unexpected(false); } -//@todo class method -Dir dir_shift(Dir d) { - switch (d) { - case Dir::up: return Dir::right; - case Dir::down: return Dir::left; - case Dir::left: return Dir::up; - case Dir::right: return Dir::down; - case Dir::out: return Dir::out; - } - return Dir::out; -} + Vec2 solve(const Data &input [[ maybe_unused ]]) { - std::vector<std::tuple<int, int, Dir>> visited; + std::vector<Vec3> visited; if (auto g=find_guard(input); g.has_value()) { - traverse(input, visited, g.value(), Dir::up); + traverse(input, visited, g.value()); } std::set<Vec2> p1; int p2 = 0; for (auto it=visited.begin(); it!=visited.end(); ++it) { auto [x, y, d] = *it; - std::printf("[%d,%d,%s]\n", x, y, str(d)); + //std::printf("[%d,%d,%s]\n", x, y, str(d)); Vec2 p(x, y); p1.insert(p); - if (std::count_if(visited.begin(), it, [&x,&y,&d](auto v) { + /*if (std::count_if(visited.begin(), it, [&x,&y,&d](auto v) { auto [vx, vy, vd] = v; return vx==x && vy==y && vd==dir_shift(d); })>0) { @@ -252,21 +172,7 @@ Vec2 solve(const Data &input [[ maybe_unused ]]) { p2++; continue; } - auto w = find_wall(input, p, dir_shift(d)); - if (w.has_value()) { - auto [wx, wy] = w.value(); - int valid = std::count_if(visited.begin(), it, [&](auto v) { - auto [x1, y1, d1] = v; - if (x1==wx&&y1==wy) { - std::printf("wall %d:%d(%s|%s) valid\n", wx, wy, str(d1), str(d)); - return true; - } else return false; - }); - if (valid>0) { - std::printf("wall found for %d:%d(%s) at %d:%d (valid=%d)\n", x, y, str(d), w.value().first, w.value().second,valid); - p2++; - } - } + }*/ } printf("\n"); @@ -275,7 +181,7 @@ Vec2 solve(const Data &input [[ maybe_unused ]]) { // 1103 too low int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 06 " << fname << std::endl; + std::cout << "AoC 2024 day 06 " << fname << std::endl; Data input = read_file<Data>(fname); auto [p1, p2] = solve(input); std::cout << "part1: " << p1 << std::endl; diff --git a/day07/main.cpp b/day07/main.cpp @@ -114,7 +114,7 @@ int64_t part2(Data &input [[ maybe_unused ]]) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day 07 " << fname << std::endl; + std::cout << "AoC 2024 day 07 " << fname << std::endl; Data input = read_file<Data>(fname); std::cout << "part1: " << part1(input) << std::endl; std::cout << "part2: " << part2(input) << std::endl; diff --git a/tpl/main.cpp b/tpl/main.cpp @@ -36,7 +36,7 @@ int part2(Data &input [[ maybe_unused ]]) { int main(int argc, char **argv) { const std::string fname = argc>1 ? argv[1] : "test1.txt"; - std::cout << "AoC 2015 day $DAY " << fname << std::endl; + std::cout << "AoC 2024 day $DAY " << fname << std::endl; Data input = read_file<Data>(fname); std::cout << "part1: " << part1(input) << std::endl; std::cout << "part2: " << part2(input) << std::endl;