Skip to content

Commit 34e8b01

Browse files
committedDec 13, 2024
Day 13 bit trouble with the overflows, also updated some utils
1 parent 3646604 commit 34e8b01

File tree

36 files changed

+482
-1586
lines changed

36 files changed

+482
-1586
lines changed
 

‎Advent_2022/2

-16.5 KB
Binary file not shown.

‎Advent_2022/3

-16.6 KB
Binary file not shown.

‎Advent_2022/4

-16.5 KB
Binary file not shown.

‎Advent_2022/5

-70.2 KB
Binary file not shown.

‎Advent_2022/6

-18 KB
Binary file not shown.

‎Advent_2022/7

-66.6 KB
Binary file not shown.

‎Advent_2022/7p

-21.6 KB
Binary file not shown.

‎Advent_2022/8

-16.7 KB
Binary file not shown.

‎Advent_2022/8.c

-72
This file was deleted.

‎Advent_2022/8.txt

-99
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎Advent_2022/input.txt

-5
This file was deleted.

‎Advent_2022/input2.txt

-1,063
This file was deleted.

‎Advent_2022/main

-34.9 KB
Binary file not shown.

‎Advent_2022/test.py

Whitespace-only changes.

‎Advent_2022/test.txt

-300
This file was deleted.

‎Advent_2022/trial.cpp

-14
This file was deleted.

‎Advent_2024/Day_10/main.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <set>
2+
3+
#include "../../Utils/utils.cpp"
4+
5+
std::pair<int, int> get_direction_from_enum(Direction dir) {
6+
if (dir == UP) {
7+
return {-1, 0};
8+
} else if (dir == DOWN) {
9+
return {1, 0};
10+
} else if (dir == LEFT) {
11+
return {0, -1};
12+
} else if (dir == RIGHT) {
13+
return {0, 1};
14+
}
15+
std::cout << "Invalid direction" << std::endl;
16+
return {0, 0};
17+
}
18+
19+
int travel_one_trail(Grid<int>& grid, std::pair<int, int> act_pos, Direction dir, int prev_value, std::set<std::pair<std::pair<int, int>, Direction>> visited, std::set<std::pair<int, int>>& visited_nines) {
20+
if (visited.find({act_pos, dir}) != visited.end()) {
21+
return 0;
22+
}
23+
std::pair<int, int> direction = get_direction_from_enum(dir);
24+
visited.insert({act_pos, dir});
25+
act_pos.first += direction.first;
26+
act_pos.second += direction.second;
27+
if (act_pos.first < 0 || act_pos.first > grid.get_row_size() - 1 || act_pos.second < 0 || act_pos.second > grid.get_col_size() - 1) {
28+
return 0;
29+
}
30+
int act_value = grid.get(act_pos.first, act_pos.second);
31+
if (act_value - prev_value != 1) {
32+
return 0;
33+
}
34+
if (grid.get(act_pos.first, act_pos.second) == 9) {
35+
visited_nines.insert(act_pos);
36+
visited.insert({act_pos, dir});
37+
return 1;
38+
}
39+
40+
return travel_one_trail(grid, act_pos, UP, act_value, visited, visited_nines) + travel_one_trail(grid, act_pos, DOWN, act_value, visited, visited_nines) + travel_one_trail(grid, act_pos, LEFT, act_value, visited, visited_nines) + travel_one_trail(grid, act_pos, RIGHT, act_value, visited, visited_nines);
41+
}
42+
43+
std::pair<int, int> get_trail_sizes(Grid<int>& grid, std::pair<int, int> trail_head) {
44+
std::set<std::pair<std::pair<int, int>, Direction>> visited = {};
45+
std::set<std::pair<int, int>> visited_nines = {};
46+
int visited_nines_score = 0;
47+
int total_score = travel_one_trail(grid, trail_head, UP, 0, visited, visited_nines);
48+
total_score += travel_one_trail(grid, trail_head, DOWN, 0, visited, visited_nines);
49+
total_score += travel_one_trail(grid, trail_head, LEFT, 0, visited, visited_nines);
50+
total_score += travel_one_trail(grid, trail_head, RIGHT, 0, visited, visited_nines);
51+
visited_nines_score += visited_nines.size();
52+
return {visited_nines_score, total_score};
53+
}
54+
55+
int main() {
56+
std::vector<std::string> lines = read_lines();
57+
std::vector<std::pair<int, int>> trail_heads = {};
58+
Grid grid = Grid(lines.size(), lines[0].size(), 0);
59+
for (int i = 0; i < lines.size(); i++) {
60+
for (int j = 0; j < lines[i].size(); j++) {
61+
grid.set(i, j, lines[i][j] - '0');
62+
if (lines[i][j] == '0') {
63+
trail_heads.push_back({i, j});
64+
}
65+
}
66+
}
67+
int part_1_score = 0;
68+
int part_2_score = 0;
69+
for (auto trail_head : trail_heads) {
70+
auto scores = get_trail_sizes(grid, trail_head);
71+
part_1_score += scores.first;
72+
part_2_score += scores.second;
73+
}
74+
std::cout << "Part 1: " << part_1_score << '\n';
75+
std::cout << "Part 2: " << part_2_score << '\n';
76+
77+
return 0;
78+
}

‎Advent_2024/Day_11/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ int main() {
4747
print_vector(stones, ' ');
4848
int num_blinks = 25;
4949
std::vector<std::string> new_stones;
50+
std::unordered_map<unsigned long long, unsigned long long> stone_to_stone_count;
5051

5152
std::vector<unsigned long long> stones_long;
5253
for (auto stone : stones) {
5354
stones_long.push_back(std::stoull(stone));
55+
stone_to_stone_count[std::stoull(stone)] = 1;
5456
}
5557
std::vector<unsigned long long> new_stones_long;
5658
std::unordered_map<std::pair<unsigned long long, int>, unsigned long long, hash_pair> stone_to_num_stones;
59+
5760
new_stones.reserve(stones.size());
5861
// Part 1
5962
for (int i = 0; i < num_blinks; i++) {

‎Advent_2024/Day_13/main.cpp

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include <climits>
2+
#include <cmath>
3+
#include <numeric>
4+
5+
#include "../../Utils/utils.cpp"
6+
7+
struct Button {
8+
int x;
9+
int y;
10+
int current_x;
11+
int current_y;
12+
};
13+
14+
struct Prize {
15+
long long X;
16+
long long Y;
17+
};
18+
19+
Button get_button(std::vector<std::string> line) {
20+
Button b = {};
21+
b.current_x = 0;
22+
b.current_y = 0;
23+
std::vector<std::string> x_y = split(',', line[1]);
24+
std::string x = x_y[0];
25+
std::string y = x_y[1];
26+
b.x = std::stoi(split('+', x)[1]);
27+
b.y = std::stoi(split('+', y)[1]);
28+
return b;
29+
}
30+
31+
Prize get_prize(std::vector<std::string> line) {
32+
Prize p = {};
33+
p.X = 0;
34+
p.Y = 0;
35+
std::vector<std::string> x_y = split(',', line[1]);
36+
std::string x = x_y[0];
37+
std::string y = x_y[1];
38+
p.X = std::stoi(split('=', x)[1]) + 10000000000000;
39+
p.Y = std::stoi(split('=', y)[1]) + 10000000000000;
40+
return p;
41+
}
42+
43+
std::ostream& operator<<(std::ostream& os, Button b) {
44+
os << b.x << " " << b.y << " Curr value " << b.current_x << "," << b.current_y;
45+
return os;
46+
}
47+
48+
std::ostream& operator<<(std::ostream& os, Prize p) {
49+
os << p.X << " " << p.Y;
50+
return os;
51+
}
52+
53+
int find_lowest_token_win(Button A, Button B, Prize P) {
54+
int lowest_token_count = INT_MAX;
55+
for (int i = 0; i <= 100; i++) {
56+
if (A.current_x > P.X || A.current_y > P.Y) {
57+
break;
58+
}
59+
B.current_x = 0;
60+
B.current_y = 0;
61+
for (int j = 0; j <= 100; j++) {
62+
if (A.current_x + B.current_x == P.X && A.current_y + B.current_y == P.Y) {
63+
int token_count = i * 3 + j * 1;
64+
lowest_token_count = std::min(token_count, lowest_token_count);
65+
}
66+
if (A.current_x + B.current_x > P.X || A.current_y + B.current_y > P.Y) {
67+
break;
68+
}
69+
B.current_x += B.x;
70+
B.current_y += B.y;
71+
}
72+
A.current_x += A.x;
73+
A.current_y += A.y;
74+
}
75+
return lowest_token_count;
76+
}
77+
78+
long long find_lowest_cramer(Button A, Button B, Prize P) {
79+
long long lowest_token_count = LONG_LONG_MAX;
80+
long long determinant = (long long)A.x * (long long)B.y - (long long)A.y * (long long)B.x;
81+
82+
if (determinant != 0) {
83+
long long delta1 = (long long)P.X * B.y - (long long)B.x * P.Y;
84+
long long delta2 = (long long)P.Y * A.x - (long long)P.X * A.y;
85+
86+
if (delta1 % determinant == 0 && delta2 % determinant == 0) {
87+
long long x = delta1 / determinant;
88+
long long y = delta2 / determinant;
89+
long long token_count = x * 3 + y * 1;
90+
lowest_token_count = std::min(token_count, lowest_token_count);
91+
}
92+
}
93+
94+
return lowest_token_count;
95+
}
96+
97+
std::pair<double, double> get_button_press_count(Button A, Button B, Prize P) {
98+
double ca = (P.X * B.y - P.Y * B.x) / (A.x * B.y - A.y * B.x);
99+
double cb = (P.X - A.x * ca) / B.x;
100+
double ipart;
101+
double ipart_1;
102+
if (std::modf(ca, &ipart) == 0 && std::modf(cb, &ipart_1) == 0) {
103+
if (std::fmod(ca, 1.0) == 0 && std::fmod(cb, 1.0) == 0) {
104+
if (trunc(ca) == ca && trunc(cb) == cb) {
105+
return {ca, cb};
106+
}
107+
}
108+
}
109+
110+
return {0, 0};
111+
}
112+
113+
int main() {
114+
std::vector<std::string> lines = read_lines();
115+
long long total = 0;
116+
long long cramer_total = 0;
117+
for (int i = 0; i < lines.size(); i += 4) {
118+
std::vector<std::string> line = split(':', lines[i]);
119+
Button A = get_button(line);
120+
std::vector<std::string> line_1 = split(':', lines[i + 1]);
121+
Button B = get_button(line_1);
122+
std::vector<std::string> line_2 = split(':', lines[i + 2]);
123+
Prize P = get_prize(line_2);
124+
125+
// int token_count = find_lowest_token_win(A, B, P);
126+
auto c = get_button_press_count(A, B, P);
127+
auto cramer = find_lowest_cramer(A, B, P);
128+
auto lin = c.first * 3 + c.second;
129+
total += lin;
130+
if (cramer != LONG_LONG_MAX) {
131+
cramer_total += cramer;
132+
}
133+
134+
if (lin != cramer && lin != 0) {
135+
std::cout << A << " " << B << " " << P << std::endl;
136+
std::cout << lin << " " << cramer << std::endl;
137+
}
138+
// Part 1
139+
// if (token_count != INT_MAX) {
140+
// total += token_count;
141+
// }
142+
}
143+
std::cout << "CRAMER TOTAL = " << cramer_total << "\n";
144+
std::cout << "Total = " << total << std::endl;
145+
return 0;
146+
}

‎Advent_2024/Day_6/day_6.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import numpy as np
2+
3+
4+
class GameField:
5+
def __init__(self, file):
6+
with open(file, "r") as f:
7+
x = f.read()
8+
listed = []
9+
for line in x.rsplit("\n"):
10+
listed.append([char for char in line])
11+
listed = np.array(listed)
12+
self.game_field = listed
13+
self.game_field_0 = self.game_field.copy()
14+
self.loc_0 = np.where(self.game_field == "^")
15+
self.loc_0 = [self.loc_0[0][0], self.loc_0[1][0]]
16+
self.loc = self.loc_0.copy()
17+
self.in_field = True
18+
self.locations = []
19+
self.options = 0
20+
21+
def turn(self):
22+
if self.game_field[*self.loc] == "^":
23+
self.game_field[*self.loc] = ">"
24+
elif self.game_field[*self.loc] == ">":
25+
self.game_field[*self.loc] = "v"
26+
elif self.game_field[*self.loc] == "v":
27+
self.game_field[*self.loc] = "<"
28+
elif self.game_field[*self.loc] == "<":
29+
self.game_field[*self.loc] = "^"
30+
31+
def move_ahead(self):
32+
if self.game_field[*self.loc] == "^":
33+
if self.loc[0] != 0:
34+
if self.game_field[self.loc[0] - 1, self.loc[1]] == "#":
35+
self.turn()
36+
else:
37+
self.game_field[*self.loc] = "X"
38+
self.loc[0] -= 1
39+
self.game_field[*self.loc] = "^"
40+
elif self.game_field[*self.loc] == ">":
41+
if self.loc[1] != self.game_field.shape[1] - 1:
42+
if self.game_field[self.loc[0], self.loc[1] + 1] == "#":
43+
self.turn()
44+
else:
45+
self.game_field[*self.loc] = "X"
46+
self.loc[1] += 1
47+
self.game_field[*self.loc] = ">"
48+
elif self.game_field[*self.loc] == "v":
49+
if self.loc[1] != self.game_field.shape[1] - 1:
50+
if self.game_field[self.loc[0] + 1, self.loc[1]] == "#":
51+
self.turn()
52+
else:
53+
self.game_field[*self.loc] = "X"
54+
self.loc[0] += 1
55+
self.game_field[*self.loc] = "v"
56+
elif self.game_field[*self.loc] == "<":
57+
if self.loc[1] != 0:
58+
if self.game_field[self.loc[0], self.loc[1] - 1] == "#":
59+
self.turn()
60+
else:
61+
self.game_field[*self.loc] = "X"
62+
self.loc[1] -= 1
63+
self.game_field[*self.loc] = "<"
64+
65+
def in_game_field(self):
66+
return (
67+
0 < self.loc[0] < self.game_field.shape[0] - 1
68+
and 0 < self.loc[1] < self.game_field.shape[1] - 1
69+
)
70+
71+
def play_game(self):
72+
while self.in_game_field():
73+
self.move_ahead()
74+
if [self.loc[0], self.loc[1]] not in self.locations:
75+
self.locations.append([self.loc[0], self.loc[1]])
76+
77+
def sol_2(self):
78+
for i, loc in enumerate(self.locations):
79+
if loc == self.loc_0:
80+
continue
81+
self.ocurance = np.zeros(shape=self.game_field.shape)
82+
self.ocurance.fill(0)
83+
self.game_field = self.game_field_0.copy()
84+
self.game_field[*loc] = "#"
85+
self.loc = self.loc_0.copy()
86+
while self.in_game_field():
87+
self.ocurance[*self.loc] += 1
88+
if self.ocurance[*self.loc] > 4:
89+
self.options += 1
90+
# print(self.game_field)
91+
break
92+
self.move_ahead()
93+
94+
95+
if __name__ == "__main__":
96+
file = "test.txt"
97+
x = GameField(file)
98+
x.play_game()
99+
x.sol_2()
100+
print(x.options)

‎Utils/utils.cpp

+155-33
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,179 @@
44
#include <vector>
55

66
void ltrim(std::string &input) {
7-
input = input.substr(input.find_first_not_of(" "));
7+
input = input.substr(input.find_first_not_of(" "));
88
}
99

1010
void rtrim(std::string &input) {
11-
input = input.substr(0, input.find_last_not_of(" ") + 1);
11+
input = input.substr(0, input.find_last_not_of(" ") + 1);
1212
}
1313

1414
void trim(std::string &input) {
15-
ltrim(input);
16-
rtrim(input);
15+
ltrim(input);
16+
rtrim(input);
1717
}
1818

1919
std::vector<std::string> read_lines() {
20-
std::vector<std::string> lines;
21-
std::string line;
22-
while (getline(std::cin, line)) {
23-
lines.push_back(line);
24-
}
20+
std::vector<std::string> lines;
21+
std::string line;
22+
while (getline(std::cin, line)) {
23+
lines.push_back(line);
24+
}
2525

26-
return lines;
26+
return lines;
2727
}
2828

2929
std::vector<std::string> split(char delim, std::string input) {
30-
std::vector<std::string> tokens;
31-
std::string token;
32-
std::istringstream iss(input);
33-
while (std::getline(iss, token, delim)) {
34-
if (token.length() != 0) {
35-
tokens.push_back(token);
30+
std::vector<std::string> tokens;
31+
std::string token;
32+
std::istringstream iss(input);
33+
while (std::getline(iss, token, delim)) {
34+
if (token.length() != 0) {
35+
tokens.push_back(token);
36+
}
3637
}
37-
}
38-
return tokens;
38+
return tokens;
3939
}
4040

41-
template <typename T> void print_vector(std::vector<T> inp, char delim) {
42-
for (auto a : inp) {
43-
std::cout << a << delim;
44-
// if (newline) {
45-
// std::cout << '\n';
46-
//} else {
47-
// std::cout << ' ';
48-
//}
49-
}
50-
std::cout << '\n';
41+
template <typename T>
42+
void print_vector(std::vector<T> inp, char delim) {
43+
for (auto a : inp) {
44+
std::cout << a << delim;
45+
}
46+
std::cout << '\n';
5147
}
5248

5349
template <typename T>
5450
void print_grid(std::vector<std::vector<T>> grid, char delim) {
55-
for (auto row : grid) {
56-
print_vector(row, delim);
57-
// std::cout << '\n';
58-
}
59-
std::cout << '\n';
51+
for (auto row : grid) {
52+
print_vector(row, delim);
53+
}
54+
std::cout << '\n';
6055
}
56+
57+
struct Point {
58+
int x;
59+
int y;
60+
Point(int x, int y) : x(x), y(y) {}
61+
62+
Point operator+(const Point &other) const {
63+
return Point(this->x + other.x, this->y + other.y);
64+
}
65+
66+
Point operator-(const Point &other) const {
67+
return Point(this->x - other.x, this->y - other.y);
68+
}
69+
70+
Point &operator+=(const Point &other) {
71+
*this = *this + other;
72+
return *this;
73+
}
74+
75+
Point &operator-=(const Point &other) {
76+
*this = *this - other;
77+
return *this;
78+
}
79+
80+
bool operator==(const Point &other) const {
81+
return this->x == other.x && this->y == other.y;
82+
}
83+
bool operator!=(const Point &other) const { return !(*this == other); }
84+
85+
bool operator<(const Point &other) const {
86+
return this->x < other.x || (this->x == other.x && this->y < other.y);
87+
}
88+
89+
bool operator>(const Point &other) const {
90+
return this->x > other.x || (this->x == other.x && this->y > other.y);
91+
}
92+
};
93+
94+
template <>
95+
struct std::hash<Point> {
96+
size_t operator()(const Point &p) const {
97+
size_t hash1 = std::hash<int>{}(p.x);
98+
// Hash the second element
99+
size_t hash2 = std::hash<int>{}(p.y);
100+
return hash1 ^ (hash2 + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2));
101+
}
102+
};
103+
104+
struct hash_pair {
105+
template <class T1, class T2>
106+
size_t operator()(const std::pair<T1, T2> &p) const {
107+
// Hash the first element
108+
size_t hash1 = std::hash<T1>{}(p.first);
109+
// Hash the second element
110+
size_t hash2 = std::hash<T2>{}(p.second);
111+
// Combine the two hash values
112+
return hash1 ^ (hash2 + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2));
113+
}
114+
};
115+
116+
std::ostream &operator<<(std::ostream &os, const Point &point) {
117+
os << "(" << point.x << ", " << point.y << ")";
118+
return os;
119+
}
120+
121+
enum Direction { UP,
122+
DOWN,
123+
LEFT,
124+
RIGHT };
125+
126+
Point get_direction_from_enum(Direction dir) {
127+
if (dir == UP) {
128+
return {-1, 0};
129+
} else if (dir == DOWN) {
130+
return {1, 0};
131+
} else if (dir == LEFT) {
132+
return {0, -1};
133+
} else if (dir == RIGHT) {
134+
return {0, 1};
135+
}
136+
std::cout << "Invalid direction" << std::endl;
137+
return {0, 0};
138+
}
139+
140+
template <typename T>
141+
class Grid {
142+
public:
143+
std::vector<std::vector<T>> grid;
144+
Grid(int rows, int cols, T fill_value) {
145+
grid = std::vector<std::vector<T>>(rows, std::vector<T>(cols, fill_value));
146+
}
147+
148+
void print(char delim) {
149+
for (auto row : grid) {
150+
print_vector(row, delim);
151+
}
152+
}
153+
154+
void print() {
155+
for (auto row : grid) {
156+
print_vector(row, ' ');
157+
}
158+
}
159+
160+
void add_row(std::vector<T> row) { grid.push_back(row); }
161+
162+
void set_row(int row, std::vector<T> values) { grid[row] = values; }
163+
164+
T get(int row, int col) { return grid[row][col]; }
165+
166+
void set(int row, int col, T value) { grid[row][col] = value; }
167+
168+
void set_col(int col, std::vector<T> values) {
169+
for (int i = 0; i < grid.size(); i++) {
170+
grid[i][col] = values[i];
171+
}
172+
}
173+
174+
int get_row_size() { return this->grid.size(); }
175+
176+
int get_col_size() {
177+
if (grid.size() > 0) {
178+
return this->grid[0].size();
179+
}
180+
return 0;
181+
}
182+
};

0 commit comments

Comments
 (0)
Please sign in to comment.