Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Rönnkvist danielronnkvist

View GitHub Profile
@danielronnkvist
danielronnkvist / nine.rs
Created Dec 9, 2020
Advent of code day 9
View nine.rs
use std::collections::VecDeque;
use std::fs::File;
use std::io::{self, BufRead};
const PREAMBLE_LENGTH: usize = 25;
fn has_sum_makers(sum: u64, preamble: Vec<u64>) -> bool {
assert_eq!(preamble.len(), PREAMBLE_LENGTH);
for a in &preamble {
for b in &preamble {
@danielronnkvist
danielronnkvist / Remove DN premium
Created Oct 18, 2017
Removes premium content block on DN articles. Add it to your bookmarks and whenever on a DN article click on the bookmark and it will remove the premium blocker.
View Remove DN premium
javascript:(function(){var a = document.querySelector('.js-paywall');a.parentElement.removeChild(a);document.querySelector('.article__body--mask').classList.remove('article__body--mask');})()
View sj.sh
#!/bin/sh
# Script for quickly swicthing MAC-adress on SJ trains!
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z
sudo ifconfig en0 ether $(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')
networksetup -detectnewhardware
networksetup -setairportnetwork en0 "SJ"
@danielronnkvist
danielronnkvist / SJ-position.js
Last active Aug 16, 2017
Fetch the train position while travelling on a SJ train (and using their wifi) and store all data for further visualisations.
View SJ-position.js
var http = require('http');
var fs = require('fs');
var dataFile = './data.json';
var dataURL = 'http://www.ombord.info/api/jsonp/position/';
var errorHandling = (e) => {
console.log(`Got error: ${e.message}`);
fetchData();
}
@danielronnkvist
danielronnkvist / object-map.js
Created May 30, 2016
Simple map function for JS objects.
View object-map.js
/*
Use like
{key: "value"}.map((object, key) => { console.log(key) })
*/
if (typeof Object.prototype.map != 'function') {
Object.prototype.map = function(callback) {
'use strict';
if (this == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
@danielronnkvist
danielronnkvist / matrixTransform.js
Created Jan 4, 2016
Get a css 2D matrix for transformations.
View matrixTransform.js
function getTransformationMatrix(rotation, scale, translateX, translateY){
var matrix = [scale * Math.cos(rotation), scale * Math.sin(rotation),
-scale * Math.sin(rotation), scale * Math.cos(rotation),
translateX, translateY]
return matrix.join(", ")
}
@danielronnkvist
danielronnkvist / Slider.coffee
Created Dec 22, 2015
Simple slideshow for animating with css.
View Slider.coffee
class window.Slider
constructor: (selector, @options)->
@index = 0
@elements = document.querySelectorAll selector
@prevIndex = @elements.length - 1
@setupOptions()
@changeActive(@index)
@startInterval()
@danielronnkvist
danielronnkvist / .bowerrc
Last active Nov 19, 2015
My gulpscript for one page html
View .bowerrc
{
"directory": "build/vendor/"
}
@danielronnkvist
danielronnkvist / translate.coffee
Last active Oct 15, 2015
Puts text from config file into html. Just add an translate attribute followed by the keys separated by dashes,for example: <h1 translate-header-title> will get config['header']['title'].
View translate.coffee
Translation = (config)->
if typeof config != "object"
throw 'Error, config is not an object'
paths = []
iterate = (obj, parent, paths)->
for property of obj
if obj.hasOwnProperty(property)
if parent != ''
new_parent = parent + '-' + property
@danielronnkvist
danielronnkvist / TND004.md
Last active Aug 29, 2015
Sammanfattning för TND004
View TND004.md

TND004 sammanfattning

Hashtabell

Hash function

Ett hashtabell har en hash function som mappar ett element till ett index där det bör sparas i tabellen. En bra hash function distibuerar elementen jämnt över tabellen för att inte orsaka kollisioner, den ska även vara enkel att beräkna. Så att medelkostnaden blir O(1).

String

Detta exempel lämpar sig för tabeller som inte är allt för stora.