Skip to content

Instantly share code, notes, and snippets.

View danielronnkvist's full-sized avatar

Daniel Rönnkvist danielronnkvist

View GitHub Profile
@danielronnkvist
danielronnkvist / nine.rs
Created December 9, 2020 20:24
Advent of code day 9
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 October 18, 2017 08:31
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.
javascript:(function(){var a = document.querySelector('.js-paywall');a.parentElement.removeChild(a);document.querySelector('.article__body--mask').classList.remove('article__body--mask');})()
@danielronnkvist
danielronnkvist / sj.sh
Created June 2, 2017 07:43
SJ Mac changer
#!/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 August 16, 2017 07:02
Fetch the train position while travelling on a SJ train (and using their wifi) and store all data for further visualisations.
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 08:09
Simple map function for JS objects.
/*
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 January 4, 2016 15:04
Get a css 2D matrix for transformations.
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 December 22, 2015 15:34
Simple slideshow for animating with css.
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 November 19, 2015 14:34
My gulpscript for one page html
{
"directory": "build/vendor/"
}
@danielronnkvist
danielronnkvist / translate.coffee
Last active October 15, 2015 13:51
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'].
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 August 29, 2015 14:21
Sammanfattning för TND004

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.