-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8163ab6
commit 2313092
Showing
5 changed files
with
3,635 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.29) | ||
|
||
set(DAY 18) | ||
|
||
project("${YEAR} Day ${DAY}" LANGUAGES CXX) | ||
|
||
add_compile_definitions(DAY=day_${DAY}) | ||
add_compile_definitions(YEAR=year_${YEAR}) | ||
|
||
include_directories(..) | ||
include_directories(../..) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
add_executable(${YEAR}_day_${DAY} ../../main.cpp ../../aoc.cpp day_${DAY}.cpp input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
#include "day" | ||
|
||
//---------------------------------------------------------------------------// | ||
namespace aoc::YEAR::DAY { | ||
|
||
//---------------------------------------------------------------------------// | ||
// Test data | ||
std::string testinput ( | ||
R"(5,4 | ||
4,2 | ||
4,5 | ||
3,0 | ||
2,1 | ||
6,3 | ||
2,4 | ||
1,5 | ||
0,6 | ||
3,3 | ||
2,6 | ||
5,1 | ||
1,2 | ||
5,5 | ||
2,5 | ||
6,5 | ||
1,4 | ||
0,4 | ||
6,4 | ||
1,1 | ||
6,1 | ||
1,0 | ||
0,5 | ||
1,6 | ||
2,0 | ||
)"); | ||
|
||
//---------------------------------------------------------------------------// | ||
struct P{ | ||
int x,y; | ||
}; | ||
|
||
//---------------------------------------------------------------------------// | ||
inline ::std::istream& test_input() { | ||
static std::stringstream ss; | ||
return ss = std::stringstream ( testinput );; | ||
} | ||
|
||
::std::vector<::std::string> load ( ::std::istream& fs ) { | ||
::std::string line; | ||
::std::vector<::std::string> ret; | ||
while ( !fs.eof() ) { | ||
std::getline ( fs, line ); | ||
if( !line.empty() ) | ||
ret.push_back ( line ); | ||
} | ||
return ret; | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
void Task_1 ( ::std::istream& puzzle_input ) { | ||
auto ans = 0ull; | ||
std::vector<int> maze; | ||
|
||
//aoc::test_enable(); | ||
|
||
auto& file = aoc::is_test_enabled() ? test_input() : puzzle_input; | ||
auto data = load ( file ); | ||
constexpr int W=71; | ||
|
||
maze.resize ( W*W, INT32_MAX ); | ||
|
||
for ( int i=0; i<1024; i++ ) { | ||
std::istringstream is{data[i]}; | ||
int x,y; | ||
char c; | ||
is >> x >> c>> y; | ||
maze[x+y*W]=-1; | ||
} | ||
|
||
std::vector<P> l { P { 0, 0 } }; | ||
maze [ 0 ] = 0; | ||
while ( !l.empty() ) { | ||
auto p = l.back(); l.pop_back(); | ||
if ( p.x+1 < W && maze[p.x+p.y*W]+1 < maze[p.x+p.y*W+1] ) { | ||
maze[p.x+p.y*W+1] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x+1, p.y } ); | ||
} | ||
if ( p.x-1 >=0 && maze[p.x+p.y*W]+1 < maze[p.x+p.y*W-1] ) { | ||
maze[p.x+p.y*W-1] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x-1, p.y } ); | ||
} | ||
if ( p.y+1 < W && maze[p.x+p.y*W]+1 < maze[p.x+(p.y+1)*W] ) { | ||
maze[p.x+(p.y+1)*W] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x, p.y+1 } ); | ||
} | ||
if ( p.y-1 >=0 && maze[p.x+p.y*W]+1 < maze[p.x+(p.y-1)*W] ) { | ||
maze[p.x+(p.y-1)*W] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x, p.y-1 } ); | ||
} | ||
} | ||
ans = maze[W*W-1]; | ||
|
||
OUT ( ans ); | ||
} | ||
|
||
|
||
//---------------------------------------------------------------------------// | ||
void Task_2 ( ::std::istream& puzzle_input ) { | ||
::std::string ans; | ||
//aoc::test_enable(); | ||
|
||
auto& file = aoc::is_test_enabled() ? test_input() : puzzle_input; | ||
auto data = load ( file ); | ||
std::vector<int> maze; | ||
constexpr int W=71; | ||
maze.resize ( W*W, INT32_MAX ); | ||
|
||
int i = 0; | ||
int x = 0, y = 0; | ||
for ( ; i < 1024; i++ ) { | ||
std::istringstream is{data[i]}; | ||
char c; | ||
is >> x >> c>> y; | ||
maze [ x + y * W ] = -1; | ||
} | ||
|
||
int len = 0; | ||
while ( len != INT32_MAX && i < data.size() ) { | ||
std::vector<P> l { P { 0, 0 } }; | ||
maze [ 0 ] = 0; | ||
for ( int i = 1; i < W*W; i++ ) if ( maze[i]!=-1 ) maze[i] = INT32_MAX; | ||
while ( !l.empty() ) { | ||
auto p = l.back(); l.pop_back(); | ||
if ( p.x+1 < W && maze[p.x+p.y*W]+1 < maze[p.x+p.y*W+1] ) { | ||
maze[p.x+p.y*W+1] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x+1, p.y } ); | ||
} | ||
if ( p.x-1 >= 0 && maze[p.x+p.y*W]+1 < maze[p.x+p.y*W-1] ) { | ||
maze[p.x+p.y*W-1] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x-1, p.y } ); | ||
} | ||
if ( p.y+1 < W && maze[p.x+p.y*W]+1 < maze[p.x+(p.y+1)*W] ) { | ||
maze[p.x+(p.y+1)*W] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x, p.y+1 } ); | ||
} | ||
if ( p.y-1 >= 0 && maze[p.x+p.y*W]+1 < maze[p.x+(p.y-1)*W] ) { | ||
maze[p.x+(p.y-1)*W] = maze[p.x+p.y*W]+1; | ||
l.push_back ( P { p.x, p.y - 1 } ); | ||
} | ||
} | ||
len = maze [ W * W - 1 ]; | ||
if ( len == INT32_MAX ) | ||
ans = std::to_string ( x ) + ',' + std::to_string ( y ); | ||
|
||
std::istringstream is { data[i++] }; | ||
char c; | ||
is >> x >> c >> y; | ||
maze [ x + y * W ] = -1; | ||
} | ||
|
||
OUT ( ans ); | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
|
||
} | ||
//---------------------------------------------------------------------------// |
Oops, something went wrong.