Skip to content

Instantly share code, notes, and snippets.

;; compiled from Rust function:
;; fn defrag_disk(sectors: &mut [Option<u32>]) -> Option<()> {
;; let mut sectors = sectors;
;; loop {
;; // Find the first Empty sector
;; let left_idx = sectors.iter().position(|s| s.is_none())?;
;; let left_bound = left_idx + 1;
;; // Find the last Full sector after the left_idx
;; let right_idx = sectors[left_bound..].iter().rposition(|s| s.is_some())? + left_bound;
@jhusain
jhusain / advent-of-code-9.rs
Last active January 6, 2025 08:34
advent-of-code-9.rs
fn defrag_disk(sectors: &mut [Option<u32>]) -> Option<()> {
let mut sectors = sectors;
loop {
// Find the first Empty sector
let left_idx = sectors.iter().position(|s| s.is_none())?;
// Find the last Full sector after the left_idx
let right_idx = sectors[left_idx + 1..].iter().rposition(|s| s.is_some())? + left_idx + 1;
// Swap the Empty and Full sectors
@jhusain
jhusain / Advent of code 2024 9.roc
Last active January 7, 2025 11:43
advent of code 2024 problem 7.roc
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br" }
import pf.Stdout
import pf.Stderr
appendN = \list, a, num ->
if num == 0 then
list
else
appendN (List.append list a) a (num - 1)
@jhusain
jhusain / gist:c1495ff445ffcf68012617585a4456e6
Created December 16, 2024 18:09
eval for mini language.roc
Expr : [
Func Str Expr,
Call Expr Expr,
Symbol Str,
Add Expr Expr,
Const I32,
]
eval: Expr, Dict Str Expr -> Result Expr [ExpectedFunction, SymbolNotFound Str, ExpectedI32]
eval = \ast, scope ->
@jhusain
jhusain / gist:88b4fb2d20d417103f67d00ea2186e39
Created December 10, 2024 20:45
advent-of-code-2024-9.roc
digitToInt =
\char -> Num.intCast (char - '0')
appendNTimes =
\list, v, n ->
if n == 0 then
list
else
appendNTimes (List.append list v) v (n - 1)
module Advent2024.Problem7
let file = """
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
@jhusain
jhusain / gist:26382a1b2267691284fbe0bcdc44e519
Created August 13, 2018 04:40
kth largest card (heap remove broken)
/*
Bar Problem
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
You want to know the count all possible numbers that the game administrator can shout.
@jhusain
jhusain / falcor stuff
Created April 18, 2018 21:46
falcor stuff
https://github.com/Netflix/falcor-path-utils/blob/master/lib/toTree.js
[
["list",{from:0,to:9],["name","rating"]],
["list", "length"]
]
->
{
@jhusain
jhusain / MyFree.purs
Created January 17, 2018 17:57
MyFree implementation
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Data.Foldable (fold)
import TryPureScript (DOM, h1, h2, p, text, list, indent, link, render, code)
type Video = { name:: String }
import cyclops.control.Unrestricted;
import io.reactivex.Observable;
public class ObservableInterpreter {
public static <R> Observable<R> interpret(Unrestricted<R> program){
//walk the Free data structure and handle each command,