Skip to content

Instantly share code, notes, and snippets.

@someodd
someodd / CaddyShack.hs
Last active October 12, 2024 00:31
code golf gopher server
import Network;main=withSocketsDo$listenOn(PortNumber 70)>>=forever.fst.accept>=>\h->hGetLine h>>=readFile>>=hPutStr h>>hClose h
@someodd
someodd / giamp3.sh
Last active October 5, 2024 06:36
Download random audio samples from Internet Archive
#!/bin/bash
# PLEASE READ ME BEFORE RUNNING!
# ------------------------------
#
# giamp3 v1 (2024-10-04)
#
# Random 30 second samples from Internet Archive.
#
# Tested on Debian Unstable (Sid) as of the release date.
@someodd
someodd / GenericToPairs.hs
Created September 17, 2024 21:04
Haskell: data type with record fields to text pairs
import Data.Data (Data, gmapQ, toConstr, cast)
import Data.Maybe (catMaybes)
import Data.Data (constrFields)
import qualified Data.Text as Text
{- | Convert any generic data type with record syntax to a list
of key-value `Text` pairs.
Uses some Haskell magic.
@someodd
someodd / DoubleCola.hs
Created August 23, 2022 23:10
Codeforce Problem 821: Double Cola
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-
Double Cola
https://codeforces.com/problemset/problem/82/A
EFFICIENCY REQUIREMENTS
time limit per test: 1 second
memory limit per test: 256 megabytes
@someodd
someodd / TeslaInterview.hs
Created August 23, 2022 23:08
Tesla interview question
{-
My attempt at a Tesla interview question:
Minimum number of characters to delete from a string so that each character
appears unique number of times. Note: You can delete all occurances of
characters.
eg: "aaaabbbb" -> 1 "a" or 1"b" would make "a" and "b" appear unique number of
times.
-}
@someodd
someodd / AdventOfCodeTwentyDayOne.hs
Created August 23, 2022 23:07
Advent of Code 2020: Day 1
{-
Advent of Code: Day 1
Part 1 and 2
https://adventofcode.com/2020/day/1
-}
-- Converted using a simple find/replace vim command.
puzzleInput =
[ 1780
, 1693
@someodd
someodd / AdventOfCodeTwentyDayTwo.hs
Created August 23, 2022 23:06
Advent of Code 2020: Day 2
{-
Advent of Code: Day 2
Part 1 & 2
https://adventofcode.com/2020/day/2
-}
type PasswordData = ((Int, Int, Char), String)
@someodd
someodd / MathExpr.hs
Created August 23, 2022 23:04
Challenge in parsing math expressions from some kinda AST.
import Control.Monad.Reader
import Data.Map as Map
data Expr = Lit Int | Var String | Add Expr Expr | Let (String, Expr) Expr
eval :: Expr -> Reader (Map String Int) Int
eval (Lit i) = pure i
eval (Var s) = do
varMap <- ask
case Map.lookup s varMap of
@someodd
someodd / MostCandidatesFail.hs
Created August 23, 2022 23:01
Challenge I saw on HackerNews, "Most candidates cannot solve this interview problem."
{-
I saw this posted on HackerNews:
"Most candidates cannot solve this interview problem."
https://twitter.com/Al_Grigor/status/1357028887209902088
> Most candidates cannot solve this interview problem:
> Input: "aaaabbbcca"
> Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]
@someodd
someodd / BinaryGap.hs
Last active August 23, 2022 23:03
Longest binary gap coding puzzle
{-
# PROBLEM
A binary gap within a positive integer N is any maximal sequence of consecutive
zeros that is surrounded by ones at both ends in the binary representation of
N.
For example, number 9 has binary representation 1001 and contains a binary gap
of length 2. The number 529 has binary representation 1000010001 and contains
two binary gaps: one of length 4 and one of length 3. The number 20 has binary