Skip to content

Commit

Permalink
2024 day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
SewerynKaminski committed Dec 18, 2024
1 parent 65704d6 commit 310385e
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@
* [Day 14](year2024/day14) - Restroom Redoubt :star:

* [Day 15](year2024/day15) - Warehouse Woes :star:

* [Day 16](year2024/day16) - Reindeer Maze

* [Day 17](year2024/day17) - Chronospatial Computer :star:
</details>

Legend
Expand Down
2 changes: 2 additions & 0 deletions year2024/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ add_subdirectory(day12)
add_subdirectory(day13)
add_subdirectory(day14)
add_subdirectory(day15)
add_subdirectory(day16)
add_subdirectory(day17)
40 changes: 35 additions & 5 deletions year2024/day14/day_14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ struct P {
x*=o;
y*=o;
}
P operator *( const int64_t o ) {
return {x*o, y*o};
}
P operator *( const int64_t o ) { return { x * o, y * o }; }
P operator /(const int64_t o ) { return { x / o, y / o }; }
P operator -( const P& o ) const { return { x - o.x, y - o.y }; }
int64_t len_squared() const { return x * x + y * y; }
};

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -111,6 +112,29 @@ void Task_1 ( ::std::istream& puzzle_input ) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}*/

struct PD{
double x,y;
};

P sr ( const ::std::vector<Guard>& guard ) {
P p{0,0};
for ( const auto& g: guard ) {
p.x += g.p.x;
p.y += g.p.y;
}
return { p.x / (int64_t)guard.size(), p.y / (int64_t)guard.size() };
}

int64_t var ( const ::std::vector<Guard>& guard ) {
auto s = sr ( guard );
int64_t sum = 0;
for ( const auto& g : guard ) {
auto d = s-g.p;
sum += d.len_squared();
}
return sum / guard.size();
}

//---------------------------------------------------------------------------//
void Task_2 ( ::std::istream& puzzle_input ) {
int64_t ans = 0;
Expand All @@ -126,6 +150,7 @@ void Task_2 ( ::std::istream& puzzle_input ) {

//enableRawMode();

auto v = var(data)/2;
bool search=true;
while ( search ) {
ans++;
Expand All @@ -141,13 +166,18 @@ void Task_2 ( ::std::istream& puzzle_input ) {
tab[g.p.y][g.p.x] = '*';
}

for ( const auto& l : tab ) {
if ( var( data ) < v ) {
for ( const auto& l : tab ) std::cout << l <<'\n';
search = false;
}

/*for ( const auto& l : tab ) {
if ( l.find ( std::string{"*******************************"},0)!=std::string::npos ) {
search = false;
for ( const auto& l : tab ) std::cout << l <<'\n';
break;
}
}
}*/

/*char c;
std::cout << ans <<'\n';
Expand Down
16 changes: 16 additions & 0 deletions year2024/day16/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.29)

set(DAY 16)

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

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

//---------------------------------------------------------------------------//
// Test data
std::string testinput (
R"(#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################
)"
);

//---------------------------------------------------------------------------//
struct P {
int x = 0, y = 0;
void operator +=( const P& o ) { x += o.x; y += o.y; }
void operator +=( const int o ) { x += o; y += o; }
void operator *=( const int o ) { x *= o; y *= o; }
P operator +( const P& o ) const { return { x + o.x, y + o.y }; }
P operator -( const P& o ) const { return { x + o.x, y + o.y }; }
P operator *( const int o ) { return { x * o, y * o }; }
P operator /(const int o ) { return { x / o, y / o }; }
bool operator ==(const P& o ) const { return x==o.x && y==o.y; }
bool operator !=(const P& o ) const { return x!=o.x || y!=o.y; }
int len_squared() const { return x * x + y * y; }
};

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

//---------------------------------------------------------------------------//
struct Data{
::std::vector<std::string> map;
::std::string movements;
};

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

return map;
}

//---------------------------------------------------------------------------//
static P UP{0,-1};
static P DOWN{0,1};
static P LEFT{-1,0};
static P RIGHT{1,0};
static P ZERO{0,0};

P end;
int min=INT_MAX;
//---------------------------------------------------------------------------//
int go ( std::vector<std::string>& maze, P p ) {
if ( p == end ) {
std::cout<< std::format ( "End\n" );
return 0;
}



return 0;
}

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

//aoc::test_enable();

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

P p;
int y=0,x=0;
// find 'S'
for( auto& l: maze ) {
x = l.find('S');
if ( x>0 ) {
p={x,y};
l[x]='.';
}
x = l.find('E');
if ( x>0 ) {
end={x,y};
l[x]='.';
}
y++;
}

std::cout << p.x << ' ' << p.y << '\n';
//ans = go ( maze, p, p, 0 );

OUT ( ans );
}

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

//aoc::test_enable();

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


OUT ( ans );
}

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

}
//---------------------------------------------------------------------------//
Loading

0 comments on commit 310385e

Please sign in to comment.