Skip to content

Instantly share code, notes, and snippets.

View colinwd's full-sized avatar

Colin Davis colinwd

  • Portland, OR
View GitHub Profile
@colinwd
colinwd / main.rs
Created December 16, 2020 19:45
dice roller
use rand::{thread_rng, Rng};
use regex::Regex;
fn main() {
let dice = std::env::args().nth(1).expect("No dice pattern given");
let regex = Regex::new("(\\d+)?d(\\d+)([\\+\\-]*)(\\d*)?").unwrap();
let captures = regex.captures(&dice).unwrap();
let count: &i32 = &captures[1].parse().unwrap();
// class version 52.0 (52)
// access flags 0x21
public class Filter {
// compiled from: Filter.java
// access flags 0x19
public final static INNERCLASS java/lang/invoke/MethodHandles$Lookup java/lang/invoke/MethodHandles Lookup
// access flags 0x1
public <init>()V
@colinwd
colinwd / Result.java
Created August 17, 2018 21:42
An implementation of the Either monad for Java
import java.util.Optional;
import java.util.function.Function;
public abstract class Result<T, E> {
private T ok;
private E error;
public Optional<T> ok() {
return Optional.ofNullable(this.ok);
fn main() {
let input = 277678;
println!("Next Square: {}", get_next_square(input));
println!("Side Length: {}", get_side_length(input));
println!("Layer Number: {}", get_layer_number(input));
println!("Side: {}", get_side(input));
println!("Distance to center of side: {}", get_distance_to_center_of_side(input));
println!("Manhattan Distance: {}", get_layer_number(input) + get_distance_to_center_of_side(input))
}
pub fn solve_two() -> String {
let f = BufReader::new(File::open("resources/day_two.txt").unwrap());
let checksum :u32 = f.lines()
.map(|line| line.unwrap())
.map(|line| line_checksum_two(line))
.sum();
return checksum.to_string()
}
pub fn solve_two() -> u32 {
let input :Vec<u32> = input().chars().map(|c| c.to_digit(10).unwrap()).collect();
let half = input.len() / 2;
let first_half = input.get(0..half).unwrap();
let second_half = input.get(half..input.len()).unwrap();
let mut sum = 0;
for (i, &digit) in first_half.iter().enumerate() {
if digit == second_half[i] {
sum = sum + (digit * 2)
use std::io::{BufReader, BufRead};
use std::fs::File;
pub fn solve() -> String {
let f = BufReader::new(File::open("resources/day_two.txt").unwrap());
let checksum :u32 = f.lines()
.map(|line| line.unwrap())
.map(|line| line_checksum(line))
.sum();
@colinwd
colinwd / keybase.md
Last active November 23, 2016 19:41

Keybase proof

I hereby claim:

  • I am colinwd on github.
  • I am colinwd (https://keybase.io/colinwd) on keybase.
  • I have a public key ASBrJv1E1JtCupXHcfEZUnYCxYzRwI-gLzdDyJlw5deopQo

To claim this, I am signing this object:

@colinwd
colinwd / gist:5750344
Created June 10, 2013 16:50
Backup all DBs in sql server
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
-- specify database backup directory
SET @path = 'C:\tmp\sqlExpress_backups\'
@colinwd
colinwd / gist:2837721
Created May 30, 2012 17:15
Google Maps JS
function buttonHandler(layername, type) {
var layer = layers[layername];
var kml = loadKML(layer);
if ( document.getElementById(layername).checked === true ) {
//show loading spinner
$('#loadingGif').removeClass('invisible');
//load KML and draw to map
kml.setMap(map);
redrawRoutes(kml);