Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
commander-trashdin / day5.lisp
Last active December 5, 2021 15:08
AoC day 5, stupid solution
(defun read-coords (path)
(with-open-file (stream path :direction :input :if-does-not-exist :error)
(let ((res (make-array 0 :adjustable t :fill-pointer 0)))
(loop :for (x1 y1 _ x2 y2)
:= (uiop:split-string (read-line stream nil nil) :separator ", ")
:while x1
:do (vector-push-extend (list (parse-integer x1)
(parse-integer y1)
(parse-integer x2)
(parse-integer y2)) res))
@commander-trashdin
commander-trashdin / day4.lisp
Created December 4, 2021 21:57
AoC 2021, day 4, not too bad
(defmacro do-table (indexes &body body)
(if (null indexes)
`(progn ,@body)
`(loop :for ,(caar indexes) :from 0 :below ,(cadar indexes)
:do (do-table ,(cdr indexes) ,@body))))
(defun read-nums (stream)
(map 'vector #'parse-integer
@commander-trashdin
commander-trashdin / day3.lisp
Created December 3, 2021 20:46
AoC, day 3, parts 1&2, Common Lisp, really sloppy work on my part, so please ignore this.
(defun update-maxs (line maxs)
(loop :for ch :across line
:for i :from 0
:when (char= ch #\1)
:do (incf (aref maxs i))))
(defun day3.1 (path)
(with-open-file (stream path :direction :input
:if-does-not-exist :error)
(let ((firstline (read-line stream nil nil)))
@commander-trashdin
commander-trashdin / ht.cpp
Last active September 7, 2021 18:18
concurrent hash map
#include <unordered_map>
#include <mutex>
#include <functional>
#include <forward_list>
#include <vector>
#include <utility>
#include <algorithm>
#include <memory>
template <class K, class V, class Hash = std::hash<K>>
@commander-trashdin
commander-trashdin / reduce.cpp
Created August 26, 2021 12:19
Parallel "reduce"
template <class RandomAccessIterator, class T, class Func>
T Reduce(RandomAccessIterator first, RandomAccessIterator last, const T& initial_value, Func func) {
std::vector<std::thread> workers;
std::atomic<T> cur_value(initial_value);
auto dist = std::distance(first, last);
auto seglen = dist / 7;
auto rem = dist % 7;
if (seglen != 0) {
for (size_t i = 0; i < 7; i++) {
@commander-trashdin
commander-trashdin / rawlist.cpp
Last active August 7, 2021 18:45
An attempt to make list with raw pointers
#include <iostream>
#include <memory>
template <class T>
class List {
struct Node {
public:
Node(T data, Node* next) : data_(data), next_(next) {
}
@commander-trashdin
commander-trashdin / sll.cpp
Last active August 4, 2021 13:44
Single linked list
#include <iostream>
#include <memory>
template <class T>
class SLList {
public:
SLList() : size_(0), first_(nullptr) {
}
SLList(std::initializer_list<T> list) : size_(list.size()) {
/// A munger which XORs a key with some data
use std::borrow::Borrow;
#[derive(Clone)]
pub struct Xorcism<'a> {
key: &'a [u8],
position: usize,
}
pub trait Captures<'a> {}
@commander-trashdin
commander-trashdin / dl.lisp
Created April 26, 2021 10:09
dl-list-template 2: electric boogaloo
(ql:quickload 'adhoc-polymorphic-functions)
(use-package :adhoc-polymorphic-functions)
(deftype ind () `(integer 0 #.array-dimension-limit))
(defparameter *default-impl* (make-hash-table))
@commander-trashdin
commander-trashdin / struct.lisp
Last active April 8, 2021 22:26
more adpf structures
(defmacro define-struct (name inheritance &body slots)
(let ((trueslots (loop :for (name . rest) :in slots
:collect (ecase (length rest)
(1 `(,name . ,rest))
(2 (let ((type (cadr (member :t rest))))
`(,name ,(default type) :type ,type)))
(3 (let ((type (cadr (member :t rest))))
`(,name ,(third rest) :type ,type)))))))
`(progn