Skip to content

Commit

Permalink
2024 day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
SewerynKaminski committed Dec 10, 2024
1 parent 3501ad4 commit de4ba0c
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ addday(06)
addday(07)
addday(08)
addday(09)
addday(10)

#===============================================================================
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
</details>

<details><summary><a href="year2024">2024</a>
:star: :star: :star: :star: :star: :star: :star: :star: :star:
:star: :star: :star: :star: :star: :star: :star: :star: :star: :star:
✩ ✩ ✩ ✩ ✩ ✩ ✩ ✩ ✩ ✩
✩ ✩ ✩ ✩ ✩
</summary>
Expand All @@ -360,6 +360,8 @@
* [Day 8](year2024/day08) - Resonant Collinearity :star:

* [Day 9](year2024/day09) - Disk Fragmenter :star:

* [Day 10](year2024/day10) - Hoof It :star:
</details>

Legend
Expand Down
17 changes: 17 additions & 0 deletions year2024/day10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.29)

set(YEAR 2024)
set(DAY 10)

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)
113 changes: 113 additions & 0 deletions year2024/day10/day_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "day"

//---------------------------------------------------------------------------//
struct P {
int x,y;
size_t hash() const { return ((size_t)y<<32)|(size_t)x; }
bool operator==(const P& o ) const { return y==o.y && x==o.x; }
};

//---------------------------------------------------------------------------//

template<>
struct std::hash<P> {
size_t operator()( const P& p) const { return p.hash();}
};

//---------------------------------------------------------------------------//
namespace aoc::YEAR::DAY {

//---------------------------------------------------------------------------//
// Test data
std::string testinput (
R"(89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
)");

//---------------------------------------------------------------------------//
inline std::istream& test_input() {
static std::stringstream ss;
return ss = std::stringstream ( testinput );;
}

//---------------------------------------------------------------------------//
auto load( std::istream& file ) {
::std::vector<::std::string> ret;
::std::string line;
while(::std::getline( file, line ))
ret.push_back ( line );

return ret;
}

//---------------------------------------------------------------------------//
size_t go ( std::vector<std::string>& t, int x, int y, std::unordered_set<P>& p ) {
size_t ret = 0;
int h = t.size(), w = t[0].size();
auto c = t[y][x];
if ( c=='9' ) {
p.insert ( { x, y } );
return 1;
}

if ( x +1 < w && c + 1 == t[y][x+1] ) ret += go ( t, x+1, y , p );
if ( x -1 >= 0 && c + 1 == t[y][x-1] ) ret += go ( t, x-1, y , p );
if ( y +1 < h && c + 1 == t[y+1][x] ) ret += go ( t, x , y+1, p );
if ( y -1 >= 0 && c + 1 == t[y-1][x] ) ret += go ( t, x , y-1, p );

return ret;
}

//---------------------------------------------------------------------------//
void Task_1 ( ::std::istream& puzzle_input ) {
auto ans=0ull;

//aoc::test_enable();

auto& file = aoc::is_test_enabled() ? test_input() : puzzle_input;
auto data = load( file );

for ( uint y = 0; y < data.size(); y++ ) {
for ( uint x = 0; x < data[0].size(); x++ ) {
if ( data[y][x] == '0' ) {
std::unordered_set<P> p;
go ( data, x, y, p );
ans += p.size();
}
}
}

OUT ( ans );
}

//---------------------------------------------------------------------------//
void Task_2 ( ::std::istream& puzzle_input ) {
auto ans = 0ull;

//aoc::test_enable();

auto& file = aoc::is_test_enabled() ? test_input() : puzzle_input;
auto data = load( file );

for ( uint y = 0; y < data.size(); y++ ) {
for ( uint x = 0; x < data[0].size(); x++ ) {
if ( data[y][x] == '0' ) {
std::unordered_set<P> p;
ans += go ( data, x, y, p );
}
}
}

OUT ( ans );
}

//---------------------------------------------------------------------------//

}
//---------------------------------------------------------------------------//
52 changes: 52 additions & 0 deletions year2024/day10/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
5654348767654328943210568976534369910789674323105678
8701239458901217658121477687623478823658789410234089
9687012344567806789054389598510562734569876501870123
7896543569478910690343201423421051650121965432965434
3234512478321001541252102310436710543230892396566985
0105601309010192432567010121045827652340181087654876
3238711210520983401478121012310938981653276321049965
4589100345601276512389652983429845670787345438038734
7671018986798345601456743876536762103894421589125621
8562327019887016512567895805445653214545430677894100
9443456523456325434898656914560344367656528912763210
2356932108701236721019847823271255698767817603454323
1047823019854349832478739854180766789678906541065410
2154014323769858210567026765099850176543215432878929
3763005458958765523450110623458901289012103898945678
7892176767567894654543210210567110378650112367630567
4567989853018723743210321671003025496743203456721656
3298789914329610894987434582312136789867654109876545
0107678801458543043406535495443249834578943218103430
1014565432367232112312345456954956725669867823212321
0123054545850187601432196307867875218754756984654321
1232123456987696566545087212987890109843405498789210
8741876567896501487656996501256765210762112301230101
9650985401223432398747887450345034369854013010149877
8967812370314671054321076365432123456343401323456778
7874901987404589969965431256501432369265692454987569
2965543256563217878872120387124501078104783467873456
1234630159870101767013098591037612156943476550012347
0323701068965432656523107652348765747872105621891298
3410892454321140545432112543659877831030323436760101
6598543265210061236789093456701436921121410345603456
5677610178901176678768186569892345430236587107812367
0189825679543285789451076654385454321947496236901098
1670134389056994874342345789276542100858905145610329
2765245212167823965218763210167033103767814098783410
3874396103456710454109454109098124912980123045696598
4983087023469832310087345678567865843832652136787867
5672171110578761101296218778410976756741743421691958
9876780987645670814385609569328985401650898330540349
6565491234532189985674212432310670332798567891231210
5430303456541023476543203421211591243897678780010676
6321212187650116567800134560101487654016549654321985
7890341098565207656912129870122371210323434545401014
6088780879876348745923038565430110326546783256910123
2109691965665489632874567678321065487235490127823434
1234532234786543211065212589832966599104381030198569
0398940123894678707845103492123877678567210943217678
1787654014783289698934598783034568943498219854006010
5671013005654194567827647695693210012382108760125421
0542562196678003430918034506784345894563456978934438
1233478787549112321209123215410256723874567834983549
2012989101238103012213230123322105012965450125676678

0 comments on commit de4ba0c

Please sign in to comment.