This commit is contained in:
Ivan Malison 2021-08-01 15:25:10 -06:00
parent 44d1254c06
commit 75093e0979
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8
3 changed files with 0 additions and 174 deletions

View File

@ -1,77 +0,0 @@
#!/usr/bin/env run_haskell_stack.sh
{-# LANGUAGE OverloadedStrings, AllowAmbiguousTypes #-}
import Data.Stack
import Data.Maybe
import Debug.Trace
data ProblemState = ProblemState
{ maxValue :: Int
, maxHeight :: Int
, maxStartIndex :: Int
, maxEndIndex :: Int
, heightStartStack :: Stack (Int, Int)
} deriving Show
stackPeekDef theStack = fromMaybe (0, 0) $ stackPeek theStack
processItem index problemState =
let (newStack, (itemIndex, itemHeight)) =
fromMaybe (stackNew, (0, 0)) $ stackPop $ heightStartStack problemState
rectSize = (index - itemIndex) * itemHeight
in if rectSize > (maxValue problemState)
then problemState
{ maxValue = rectSize
, maxHeight = itemHeight
, maxStartIndex = itemIndex
, maxEndIndex = index
, heightStartStack = newStack
}
else problemState { heightStartStack = newStack }
processFenceHeight _ [] problemState = problemState
processFenceHeight index allHeights@(height:heights) problemState =
let hss = heightStartStack problemState
(lastIndex, lastHeight) = stackPeekDef hss
handleTaller = processFenceHeight (index + 1) heights $
problemState
{ heightStartStack = stackPush hss (index, height) }
handleShorter = let withNewMax = processItem index problemState
newProblemState =
withNewMax
{ heightStartStack =
addIfLarger (heightStartStack withNewMax)
(height, lastIndex)
}
-- No increment because we may need to pop more off of the stack
in processFenceHeight index allHeights newProblemState
addIfLarger theStack (index, height) =
let (thisIndex, thisHeight) = stackPeekDef theStack
in if thisHeight < height
then stackPush theStack (index, height)
else theStack
in case lastHeight of
_ | lastHeight < height -> handleTaller
_ | lastHeight > height -> handleShorter
_ -> processFenceHeight (index + 1) heights problemState
processFences fences =
let lastState = processFenceHeight 0 fences
ProblemState { maxValue = -1
, maxHeight = -1
, maxStartIndex = -1
, maxEndIndex = -1
, heightStartStack = stackNew
}
fenceCount = length fences
processItems state
| stackIsEmpty (heightStartStack state) = state
| otherwise = processItems $ processItem fenceCount state
in processItems lastState
main = do
let finalState = processFences [2, 4, 6, 6, 6, 7, 6, 2, 2, 2, 2, 2, 2, 2]
print $ maxValue finalState
print $ maxHeight finalState
print $ maxStartIndex finalState
print $ maxEndIndex finalState

View File

@ -1,22 +0,0 @@
#!/usr/bin/env run_haskell_stack.sh
import Data.Bifunctor
import Data.Tuple
knightDeltasSimple =
applyAll [swap] (1, 2)
>>= applyAll [first negate, second negate, bimapBoth negate]
where applyAll fns v = map ($ v) $ id:fns
bimapBoth fn = bimap fn fn
knightDeltas = applyInStages [(1, 2)]
[ [swap]
, [bimapBoth (* 2)]
, [first negate, second negate, bimapBoth negate]
]
where applyAll fns v = map ($ v) $ id:fns
applyAllToValues values fns = values >>= applyAll fns
applyInStages = foldl applyAllToValues
bimapBoth fn = bimap fn fn
main = print knightDeltas

View File

@ -1,75 +0,0 @@
#!/usr/bin/env python
import argparse
import os
import sys
class BrightnessManager(object):
@classmethod
def from_path(cls, path):
return cls(
set_brightness_filepath=os.path.join(path, "brightness"),
actual_brightness_filepath=os.path.join(path, "actual_brightness"),
max_brightness_filepath=os.path.join(path, "max_brightness"),
)
def __init__(
self, set_brightness_filepath, max_brightness_filepath, actual_brightness_filepath
):
self.set_brightness_filepath = set_brightness_filepath
self.max_brightness_filepath = max_brightness_filepath
self.actual_brightness_filepath = actual_brightness_filepath
@property
def current_brightness(self):
with open(self.actual_brightness_filepath) as fd:
return int(fd.read())
@property
def max_brightness(self):
with open(self.max_brightness_filepath) as fd:
return int(fd.read())
@current_brightness.setter
def current_brightness(self, brightness):
with open(self.set_brightness_filepath, 'w') as fd:
fd.write(str(brightness))
def increment_by_proportion(self, proportion):
new_brightness = self.current_brightness + int(self.max_brightness * proportion)
new_brightness = min(new_brightness, self.max_brightness)
self.current_brightness = new_brightness
@property
def current_proportion(self):
return float(self.current_brightness) / self.max_brightness
IntelBrightnessManager = BrightnessManager.from_path(
"/sys/class/backlight/intel_backlight",
)
def build_parser():
parser = argparse.ArgumentParser(
description='Interact with macbook brightness',
)
parser.add_argument(
"--change", "-c",
help="Change volume by the given percentage",
default=0
)
parser.add_argument(
"--print", "-p", dest="do_print",
action='store_true',
default=False
)
return parser
if __name__ == '__main__':
args = build_parser().parse_args()
IntelBrightnessManager.increment_by_proportion(float(args.change) / 100)
if args.do_print:
print(int(IntelBrightnessManager.current_proportion * 100))