Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
commander-trashdin / word_count.cpp
Created September 15, 2020 23:04
Interview question.
#include <vector>
#include <string>
#include <unordered_map>
#include <fstream>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string_view>
@commander-trashdin
commander-trashdin / .lisp
Created September 11, 2020 17:22
consecutive sum
(defun consecsum (array)
(loop :with res := (make-array 1 :adjustable t :fill-pointer 0)
:with acc := (aref array 0)
:for i :from 1 :below (length array)
:for j := (- i 1)
:if (= (aref array i) (aref array j))
:do (incf acc (aref array i))
:else
:do (vector-push-extend acc res)
(setf acc (aref array i))
(defun diophantine-equation (a b)
(declare (fixnum a) (fixnum b)
(optimize (speed 3)))
(let ((left-side-a 1) (left-side-b 0)
(right-side-a 0) (right-side-b 1))
(declare (fixnum left-side-a) (fixnum left-side-b)
(fixnum right-side-a) (fixnum right-side-b))
(loop :until (or (= 0 a) (= 0 b))
:if (> a b)
(defun tug-of-war (people)
(declare (optimize (speed 3))
((simple-array fixnum (*)) people))
(setf people (sort people #'>))
(let ((sum (reduce #'+ people)))
(declare (fixnum sum))
(if (evenp sum)
(let ((sum (floor sum 2)))
(labels ((recursive-walk (upto currentsum)
(declare (fixnum currentsum))
30
14
21 41 85 91 15 13 77 26 90 44 99 23 77 82
11
7 69 22 96 26 15 18 53 87 90 89
39
60 31 12 77 47 48 73 21 55 64 20 18 96 55 99 28 21 28 90 27 15 96 43 21 29 41 56 27 41 79 34 59 34 19 54 85 28 36 53
14
81 46 13 67 48 86 98 51 25 89 19 58 26 5
13
@commander-trashdin
commander-trashdin / mul_1.asm
Created June 18, 2020 14:59
Should be a multiplication for natural bignums
dnl AMD64 mpn_mul_1.
dnl Copyright 2003-2005, 2007, 2008, 2012 Free Software Foundation, Inc.
dnl This file is part of the GNU MP Library.
dnl
dnl The GNU MP Library is free software; you can redistribute it and/or modify
dnl it under the terms of either:
dnl
dnl * the GNU Lesser General Public License as published by the Free
;;;; cl-overload.lisp
(in-package #:cl-overload)
(defgeneric generic-find (item sequence &key test)
(:generic-function-class fast-generic-functions:fast-generic-function))
(defmethod generic-find (item (list list) &key (test #'eql))
(and (member item list :test test)
@commander-trashdin
commander-trashdin / optimization.lisp
Created June 8, 2020 12:22
Constantfolding go brrrrr
(defpackage #:optimizations
(:use #:cl)
(:shadow #:+))
(in-package #:optimizations)
(declaim (inline quaternion))
(defstruct quaternion
@commander-trashdin
commander-trashdin / optimization.lisp
Created June 8, 2020 12:22
Constantfolding go brrrrr
(defpackage #:optimizations
(:use #:cl)
(:shadow #:+))
(in-package #:optimizations)
(declaim (inline quaternion))
(defstruct quaternion
@commander-trashdin
commander-trashdin / dis.repl
Created June 8, 2020 07:19
Disassembly for different types with +.
CL-USER> (disassemble (lambda (a b) (declare (optimize (speed 3) (safety 0) (compilation-speed 0)))
(declare (long-float a) (long-float b))
(+ a b)))
; disassembly for (LAMBDA (A B))
; Size: 65 bytes. Origin: #x52C50EE0 ; (LAMBDA (A B))
; EE0: F20F58D1 ADDSD XMM2, XMM1
; EE4: 49896D28 MOV [R13+40], RBP ; thread.pseudo-atomic-bits
; EE8: 498B5568 MOV RDX, [R13+104] ; thread.alloc-region
; EEC: 4C8D5A10 LEA R11, [RDX+16]
; EF0: 4D3B5D70 CMP R11, [R13+112]