Skip to content

Instantly share code, notes, and snippets.

@freelon
freelon / dice_roll_comparison.clj
Created August 2, 2022 18:37
Comparison of roll probabilities for different dice sets
(ns foo.dice-roll-comparison)
(def roll-1d20 (for [i (range 1 21)] i))
(def roll-median-3d20 (for [a (range 1 21) b (range 1 21) c (range 1 21)] (second (sort (list a b c)))))
(def roll-sum-3d6 (for [a (range 1 7) b (range 1 7) c (range 1 7)] (+ a b c)))
(def roll-sum-2d10 (for [a (range 1 11) b (range 1 11)] (+ a b)))
(defn probabilities [values] (into (sorted-map) (update-vals (frequencies values) (fn [i] (/ (* i 100.0) (count values))))))
(doseq [[x y] (seq (probabilities roll1d20))] (println x y))
@freelon
freelon / day2.rs
Created December 2, 2018 13:30
Advent of Code 2018 Day 2 Part 2 Linear time solution
pub fn task2_linear() {
let input = utils::read_file("input/day02.txt");
// first order of business: create a tree where each input line is sorted
// into every nodes same_prefix, if the path leading from the root to that
// has that prefix.
let mut root = Node::default();
for id in input.lines() {
add_id_to_tree(&mut root, &id);