-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish 2023 with days 21 and 24. Add benchmark of map, unordered_map, and a custom BST "splay tree" in day 14. Summary of commits while squashing: Day 21: * Add alternative for part two that turned out to be slower. I am guessing the unordered_set gets very large. * Improve bookeeping in searches. Update print making nice visualization. Visualization using escape code. Refactor. For part two, printing number garden plots at each step. Checking the sequence and difference sequence in Python shows kind of exponential and a periodic-like function with an exponential trend, respectively. * Python analysis WIP. * Part two answer using the Lagrange polynomial using pybind11. * Coming back to day 24 with basic Python visualization and warning fix. * Update nob.yml * Update nob.yml * Add benchmark of a problem with 3rd party. Refactor. * Add minimal splay BST (self-balancing) and parameterize benchmark. * Update build files. * 🙈 Then: * Updates to nob.yml * Day 24 part two using sympy. Last star! 🌟 * CI fixes. Remove building of step counter (day 21) since with pybind I set up CMake. The build with CMake manually, without nob, using tmate, didn't work due to some errors that seem to do with either the version of pybind or the compiler / compilation flags. * Revert python version bump to fix nb CI * Update github-actions-duplicate-code-check.yml
- Loading branch information
Showing
12 changed files
with
510 additions
and
126 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 |
---|---|---|
|
@@ -7,3 +7,6 @@ gcm.cache/ | |
__pycache__/ | ||
nobuild/ | ||
venv/ | ||
build/ | ||
logs/ | ||
*.png |
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,135 @@ | ||
#include <memory> | ||
#include <functional> | ||
#include <utility> | ||
|
||
namespace dts { | ||
|
||
//FIXME design | ||
namespace splay_tree { | ||
|
||
using std::pair; | ||
|
||
// Initial version result of fixing memory and refactoring wikipedia's splay tree ¯\_(ツ)_/¯ | ||
template<typename K, typename V> class map { | ||
|
||
//FIXME generic | ||
std::less<K> comp; | ||
|
||
struct node { | ||
node *left, *right; | ||
node *parent; | ||
pair<K, V> key_value; | ||
K key; | ||
node(const pair<K, V>& init = pair<K, V>()) : | ||
left(nullptr), right(nullptr), parent(nullptr), key_value(init) { | ||
key = key_value.first; | ||
} | ||
~node() { } | ||
} *root; | ||
|
||
void left_rotate(node *x) { | ||
node *y = x->right; | ||
if (y) { | ||
x->right = y->left; | ||
if (y->left) y->left->parent = x; | ||
y->parent = x->parent; | ||
} | ||
|
||
if (!x->parent) root = y; | ||
else if (x == x->parent->left) x->parent->left = y; | ||
else x->parent->right = y; | ||
if (y) y->left = x; | ||
x->parent = y; | ||
} | ||
|
||
void right_rotate(node *x) { | ||
node *y = x->left; | ||
if (y) { | ||
x->left = y->right; | ||
if (y->right) y->right->parent = x; | ||
y->parent = x->parent; | ||
} | ||
if (!x->parent) root = y; | ||
else if (x == x->parent->left) x->parent->left = y; | ||
else x->parent->right = y; | ||
if (y) y->right = x; | ||
x->parent = y; | ||
} | ||
|
||
void splay(node *x) { | ||
if (!x->parent) return; | ||
if (!x->parent->parent) { | ||
if (x->parent->left == x) right_rotate(x->parent); | ||
else left_rotate(x->parent); | ||
} else if (x->parent->left == x && x->parent->parent->left == x->parent) { | ||
right_rotate(x->parent->parent); | ||
right_rotate(x->parent); | ||
} else if (x->parent->right == x && x->parent->parent->right == x->parent) { | ||
left_rotate(x->parent->parent); | ||
left_rotate(x->parent); | ||
} else if (x->parent->left == x && x->parent->parent->right == x->parent) { | ||
right_rotate(x->parent); | ||
left_rotate(x->parent); | ||
} else { | ||
left_rotate(x->parent); | ||
right_rotate(x->parent); | ||
} | ||
} | ||
|
||
void free(node **z) { | ||
if (!(*z)) return; | ||
if ((*z)->left or (*z)->right) { | ||
free(&((*z)->left)); | ||
free(&((*z)->right)); | ||
} | ||
delete *z; | ||
*z = nullptr; | ||
} | ||
|
||
public: | ||
map() : root(nullptr) { } | ||
~map() { | ||
free(&root); | ||
} | ||
|
||
void emplace(const K& key, const V& value) { | ||
node *z = root; | ||
node *p = nullptr; | ||
|
||
while (z) { | ||
p = z; | ||
if (comp(z->key, key)) z = z->right; | ||
else z = z->left; | ||
} | ||
|
||
// TODO fix leak | ||
z = new node(std::make_pair(key, value)); | ||
z->parent = p; | ||
|
||
if (!p) root = z; | ||
else if (comp(p->key, z->key)) p->right = z; | ||
else p->left = z; | ||
|
||
splay(z); | ||
} | ||
|
||
bool contains(const K& key) { | ||
node *z = root; | ||
while (z) { | ||
if (comp(z->key, key)) z = z->right; | ||
else if (comp(key, z->key)) z = z->left; | ||
else { splay(z); return true; } | ||
} | ||
return false; | ||
} | ||
|
||
//FIXME precondition: contains(key) (or insert(key)) was called last, | ||
// with no ohter insertion or lookup after it | ||
V at([[maybe_unused]] const K& key) { | ||
return root->key_value.second; | ||
} | ||
}; | ||
|
||
} // namespace splay_tree | ||
|
||
} // namespace dts |
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,8 @@ | ||
cmake_minimum_required(VERSION 3.15..3.27) | ||
project(aoc-2023-21 LANGUAGES CXX) | ||
set(CMAKE_CXX_STANDARD 23) | ||
#set(CMAKE_CXX_FLAGS "-Wall -Wconversion -Wextra -pedantic -fsanitize=address,pointer-overflow,signed-integer-overflow,undefined") | ||
find_package(pybind11 REQUIRED) | ||
add_executable(step_counter step_counter.cpp) | ||
target_link_libraries(step_counter PRIVATE pybind11::embed) | ||
set(CMAKE_BUILD_TYPE Release) |
Oops, something went wrong.