Skip to content

Instantly share code, notes, and snippets.

View codesections's full-sized avatar

Daniel Sockwell codesections

View GitHub Profile
@codesections
codesections / robot_paths.rs
Last active January 10, 2019 16:07
A rust implementation of a simple exercise
fn main() {
println!("{:?}", robot_paths(6));
}
#[derive(Clone)]
struct Board {
squares: Vec<Vec<bool>>,
}
impl Board {
@codesections
codesections / factorial.lisp
Created February 23, 2019 16:43
Lisp example
(defun factorial (n)
(if (zerop n) 1 (* n (factorial (1- n)))))
@codesections
codesections / linked_list.rs
Created January 6, 2019 23:02
A simple linked list implemented in Rust
#[derive(Debug)]
pub struct LinkedList {
head: Option<Box<Node>>,
tail: Option<*mut Node>,
}
#[derive(Debug)]
struct Node {
value: i32,
next: Option<Box<Node>>,
}
@codesections
codesections / xmodmap.sh
Created August 21, 2019 22:26
Output of `xmodmap -pke`
keycode 8 =
keycode 9 = Escape NoSymbol Escape
keycode 10 = 1 exclam 1 exclam
keycode 11 = 2 at 2 at
keycode 12 = 3 numbersign 3 numbersign
keycode 13 = 4 dollar 4 dollar
keycode 14 = 5 percent 5 percent
keycode 15 = 6 asciicircum 6 asciicircum
keycode 16 = 7 ampersand 7 ampersand
keycode 17 = 8 asterisk 8 asterisk
@codesections
codesections / 02.dyalog
Last active December 3, 2019 02:30
Advent of Code day 2
⎕IO←0
find←{
⍺←0
in←(⊃⍺⌷,(⍳100)∘.,⍳100) set ⊃0⌷⍵
(⊃1⌷⍵)=0⌷intcode in:in[1 2]
(⍺+1)∇in (⊃1⌷⍵)
}
set←{s←⍵ ⋄ s[1 2]←⍺[0 1] ⋄ s}
intcode←{
⍺←0 ⋄ s←state←⍵
@codesections
codesections / 05.dyalog
Last active December 6, 2019 04:13
Advent of Code day 5
⎕IO←0
intcode←{
⍺←0 ⋄ i←⍺ ⋄ s←⍵ ⋄ instr←'0'(,⍣{4<≢⍺})⍕⍺⌷⍵ ⋄ opcode←⍎¯2↑instr ⋄ p_modes←⍎¨⌽3⍴instr
p←{mode←p_modes[⍵-1]
mode=0:s[i+⍵]
mode=1:i+⍵
'ERR: Unknown parameter mode',mode}
i≥≢s:'ERR: no HALT opcode'
@codesections
codesections / 09.dyalog
Created December 10, 2019 00:36
Advent of Code day 9
intcode←{
⍺←0 0 ⋄ (i rb)←⍺ ⋄ val←⊃⍵[0] ⋄ addr←⊃⍵[1]
get←{ ⍵<0: ⎕←'ERR: invalid negative index'
(addr⍳⍵)<(≢addr):(addr⍳⍵)⌷val
addr,←⍵ ⋄ val,←0 ⋄ ∇ ⍵ }
set←{ (addr⍳⍺)<(≢addr):((addr⍳⍺)⌷val)←⍵
addr,←⍺ ⋄ val,←⍵ }
instr←(10 10 10 10 10)⊤(get i) ⋄ opcode←(10 10)⊥¯2↑instr ⋄ p_modes←⌽3↑instr
p←{mode←p_modes[⍵-1]
@codesections
codesections / 10.dyalog
Created December 11, 2019 03:23
Advent of Code day 10
in←↑⊃⎕nget '10.input' 1 ⋄ p←'#'=in
can_see←{⍺≡⍵:0
0=+/¯1↓{p[⍵[0];⍵[1]]}¨⍺ els_btwn ⍵ }
els_btwn←{ ⍺≡⍵:⍬
n←⍺+(⍺ slope_btwn ⍵)
(⊂n),(n ∇ ⍵) }
slope_btwn←{{⍵÷(∨/⍵)}-⍺-⍵}
sub postfix:<‽>($a) { $ = $a};
class A { method n { Nil };
method fine { self }
method non-nil { 42}
};
say A.new.fine‽
.non-nil;
say A.new.n‽
.non-nil;
@codesections
codesections / try-catch.raku
Created March 8, 2021 21:13
Comparing `try` to a CATCH block in Raku
my $result = do try {
# some code that may throw X::This
# some code that may throw X::NotSpecified (default)
# some code that may throw X::Something
# some code that may throw X::This or X::That