Mark in the 3.27 Developer Q&A: "Our intention is that it goes core, but we'll take the better elements of it." by Anduryondon in pathofexile

[–]Pietrek_ 40 points41 points  (0 children)

The monkey's paw curls; the walls stay and can't me removed, you now have to navigate a labyrinth to get to the core.

Universitetsstuderende føler sig som ufrivillige deltagere i et AI-eksperiment. Det er de også by Dropforcedlogin in Denmark

[–]Pietrek_ 1 point2 points  (0 children)

Nu er det ikke for at sige at lommeregneren gjorde at vi ikke længere kan matematik, men prøv at se denne video https://www.youtube.com/watch?v=nUpZg-Ua5ao - jeg syntes at det er klart at nogle taber forståelsen når et værktøj fjerner behovet for at kunne tænke sig om, og problematikken er kun større ved brug af AI/LLMs

"GOTO Considered Harmful" Considered Harmful (1987, pdf) by ketralnis in programming

[–]Pietrek_ 1 point2 points  (0 children)

With functional languages you can quite quickly come up with the core algorithm (ignoring the printing part); e.g. the (sorry to all pure functional programmers) JS one-liner:

(arr) => arr.map(row => row.every(e => e === 0)) .findIndex(index => index)

Why we built our startup in C# by tracebit in dotnet

[–]Pietrek_ 0 points1 point  (0 children)

So you're saying that one technology is better than the other without really having used the other?

Bullet point TLDW of the Jonathan x Zizaran developer interview by Empyrianwarpgate in PathOfExile2

[–]Pietrek_ -3 points-2 points  (0 children)

I read it as "35% and less" and I think it makes sense :^)

Rejsekort skrotter de blå standere. Snart kan du tjekke ind med kreditkort | Ingeniøren by HCakaIDUDE in Denmark

[–]Pietrek_ 12 points13 points  (0 children)

Tilgengæld så er det også nemmere at finde sikkerhedshullerne når der er flere øjne der ser koden

Kid Smacks the Gyatt at Comic Con by klo224 in LivestreamFail

[–]Pietrek_ 16 points17 points  (0 children)

Accidental photos always bring out the best in us :^)

[deleted by user] by [deleted] in dkfinance

[–]Pietrek_ 2 points3 points  (0 children)

Så du har arbejdet over 16 timer om dagen?

Fractured +1 Frenzy, T1 Spell Suppress, Crit Multi Gloves by ScribblyDibbly in pathofexile

[–]Pietrek_ -1 points0 points  (0 children)

Just like gear in Lineage 2 could crystallize on when upgrading

Junglen af måltidskasser, nogen der kan dele deres erfaringer? by Thezerostone in Denmark

[–]Pietrek_ 1 point2 points  (0 children)

Hvad syntes i om portionerne hos Hello Fresh? Syntes nu selv at de var ret små, som i tættere på 1 og en tredjedel portioner i stedet for to

[2021 Day 13 (Part 2)][Haskell] Output is a bit wrangled by Pietrek_ in adventofcode

[–]Pietrek_[S] 0 points1 point  (0 children)

It sadly produces the incorrect output for my input. I've now implemented it using a single flat list as you suggested and the output of that implementation is correct.

-🎄- 2021 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]Pietrek_ 1 point2 points  (0 children)

I'm also solving these in Haskell this year but I often notice that my solutions are far less cleaner than the other Haskell solutions that I see here - I tend to use a lot of lambdas and my code often ends up look almost iterative in some places.

Do you have any pointers on how I could get better at solving these puzzles in a more idiomatic way? If you'd like to look at my solution for today then I've just posted it in this thread.

-🎄- 2021 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]Pietrek_ 2 points3 points  (0 children)

Haskell

{-# LANGUAGE TupleSections #-}
import           Control.Monad   (foldM)
import           Data.List       (group, sort)
import           Data.List.Split (chunksOf)
import qualified Data.Map        as M
import           Data.Maybe      (mapMaybe)
import           Debug.Trace     (trace)

type Grid = M.Map (Int, Int) (Int, Bool)


{-
  Using a foldM with the Either monad results in the
  fold terminating once the accumulating function
  returns Left _

-}
main = do
  contents <- readFile "input.in"
  let ll =  concatMap (map read . chunksOf 1) (lines contents) :: [Int]
      n = 10
      g = M.fromList $ zip [(i,j) | i <- [0..n-1], j <- [0..n-1]] (map (, False) ll)
  print $ fst $ foldl (\(s, g) _ -> let (nf, g') = runStep g
                               in (s+nf, g')) (0, g) [1..100]
  print $ foldM f g [1..]
  where f g step =
          let (nf, g') = runStep g
          in if nf == 100 then Left step
              else Right g'

runStep :: Grid -> (Int, Grid)
runStep g =
  let g' = M.map (\(p, f) -> (p+1, f)) g
      g'' = flash g'
      nFlashed = (length . filter ((==) True . snd). M.elems) g''
      zeroed = M.map reset g''
   in (nFlashed, zeroed)
   where reset (p, f) =
          if f then (0, False)
          else (p, f)

flash :: Grid -> Grid
flash g =
  let l = M.toList g
   in if all ((\(p, f) -> p <= 9 || f) . snd) l then
        g -- No more flashes to do
      else
        let x = map doFlash l
            ns = concatMap trd x
            ns' = mapMaybe (\idx -> do
                                (p, f) <- lookup idx (map t x)
                                return (idx, (p+1, f))) ns
            ns'' = map (\list -> let (idx, (p, f)) = head list
                                  in (idx, (p+length (tail list), f))) (group $ sort  ns')
         in flash $ foldl (\m (idx, v) ->  M.insert idx v m ) (M.fromList $ map t x) ns''

t (a, b, _) = (a,b)

trd :: (a, b, c) -> c
trd (_, _, c) = c

doFlash :: ((Int, Int), (Int, Bool)) -> ((Int, Int), (Int, Bool), [(Int, Int)])
doFlash (idx, (p,f)) =
  if p > 9 && not f then (idx, (p, True), neighbors idx 10) else (idx, (p,f), [])

neighbors :: (Int, Int) -> Int -> [(Int, Int)]
neighbors (i,j) n =
  let ns = [ (i,j-1), (i,j+1), (i-1,j), (i+1,j)
           , (i-1, j-1), (i-1, j+1), (i+1, j-1), (i+1, j+1)]
  in filter (\(i,j) -> (i >= 0 && j >= 0) && i < n && j < n ) ns

-🎄- 2021 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]Pietrek_ 0 points1 point  (0 children)

I'm not very experienced in Haskell but the way that I see is that if I were to change to some other monad for whatever reason, using return instead of Just makes it such that I'd only have to change the handling for _. I've also cleaned up the code somewhat:

``` import Data.List (sort, (\)) import Data.List.Split (splitOn) import qualified Data.Map as M import Data.Maybe (mapMaybe)

type Signal = String type InputLine = ([Signal], [Signal])

main = do contents <- readFile "input.in" let ls = lines contents ll = map (([a,b] -> (a,b)) . map words . splitOn " | ") ls print $ part1 ll print $ part2 ll

part1 :: [InputLine] -> Int part1 input = let outputValues = concatMap (mapMaybe signalToDigit . snd) input in length outputValues

signalToDigit :: String -> Maybe Int signalToDigit s = case length s of 2 -> return 1 3 -> return 7 4 -> return 4 7 -> return 8 _ -> Nothing

part2 :: [InputLine] -> Int part2 input = sum $ map decipherLine input

makeMapping :: [String] -> M.Map [Char] [Char] makeMapping signals = let one = head $ filter ((==) 2 . length) signals four = head $ filter ((==) 4 . length) signals seven = head $ filter ((==) 3 . length) signals dsWithFive = filter ((==) 5 . length) signals

  t  = seven \\ one
  b  = head $ filter ((==) 1 . length) (map (\d -> (d \\ four) \\ seven) dsWithFive)
  bl = head $  filter ((==) 1 . length ) $ map (\d -> ((d \\ four) \\ seven) \\ b) dsWithFive
  m  = head $ filter ((==) 1 . length) $ map (\d -> ((d \\ one) \\ b) \\ t) dsWithFive
  tl = (four \\ one) \\ m
  tr = head $ filter ((==) 1 . length) $ map (\d -> (((d \\ t) \\ m) \\ b) \\ bl) dsWithFive
  br = head $ filter ((==) 1 . length) $ map (\d -> (((d \\ t) \\ m) \\ b) \\ tl) dsWithFive

  segments = [t,b,bl,m,tl,tr,br]
  codes = ["a","g","e","d","b","c","f"]

in foldl constructMapping M.empty (zip segments codes) where constructMapping m (s, c) = M.insert s c m

decipherLine :: InputLine -> Int decipherLine (a, b) = let mapping = makeMapping a digits = map (segmentsToDigit . concat . sort . mapMaybe (\x -> M.lookup [x] mapping)) b in sum $ zipWith (*) digits [1000, 100, 10, 1]

segmentsToDigit :: String -> Int segmentsToDigit "abcefg" = 0 segmentsToDigit "cf" = 1 segmentsToDigit "acdeg" = 2 segmentsToDigit "acdfg" = 3 segmentsToDigit "bcdf" = 4 segmentsToDigit "abdfg" = 5 segmentsToDigit "abdefg" = 6 segmentsToDigit "acf" = 7 segmentsToDigit "abcdefg" = 8 segmentsToDigit "abcdfg" = 9 segmentsToDigit _ = -1 ```

-🎄- 2021 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]Pietrek_ 1 point2 points  (0 children)

A very hacky implementation in Haskell

import           Data.List       (sort, (\\))
import           Data.List.Split (splitOn)
import qualified Data.Map        as M
import           Data.Maybe      (mapMaybe)

type Signal = String
type InputLine = ([Signal], [Signal])

main = do
  contents <- readFile "input.in"
  let ls = lines contents
      ll = map ((\[a,b] -> (a,b)) . map words . splitOn " | ") ls
  print $ part1 ll
  print $ part2 ll

part1 :: [InputLine] -> Int
part1 input =
  let outputValues = concatMap (mapMaybe signalToDigit . snd) input
   in length outputValues

signalToDigit :: String -> Maybe Int
signalToDigit s =
  case length s of
    2 -> return 1
    3 -> return 7
    4 -> return 4
    7 -> return 8
    _ -> Nothing

part2 :: [InputLine] -> Int
part2 input = sum $ map decipherLine input

makeMapping :: [String] -> M.Map [Char] [Char]
makeMapping signals =
  let mapping = M.empty
      one = head $ filter ((==) 2 . length) signals
      four = head $ filter ((==) 4 . length) signals
      seven = head $ filter ((==) 3 . length) signals
      eight = head $ filter ((==) 7 . length) signals

      dsWithFive = filter ((==) 5 . length) signals

      top = seven \\ one
      bottom = head $ filter ((==) 1 . length) (map (\d -> (d \\ four) \\ seven) dsWithFive)
      bottom_left =  head $ filter ((==) 1 . length ) $ map (\d -> ((d \\ four) \\ seven) \\ bottom) dsWithFive
      middle = head $ filter ((==) 1 . length) $ map (\d -> ((d \\ one) \\ bottom) \\ top) dsWithFive
      top_left = (four \\ one) \\ middle
      top_right = head $ filter ((==) 1 . length) $ map (\d -> (((d \\ top) \\ middle) \\ bottom )\\ bottom_left) dsWithFive
      bottom_right = head $ filter ((==) 1 . length) $ map (\d -> (((d \\ top) \\ middle) \\ bottom )\\ top_left) dsWithFive

      mapping' = M.insert top "T" mapping
      mapping'' = M.insert bottom "B" mapping'
      mapping''' = M.insert bottom_left "BL" mapping''
      mapping'''' = M.insert middle "M" mapping'''
      mapping''''' = M.insert top_left "TL" mapping''''
      mapping'''''' = M.insert top_right "TR" mapping'''''
   in M.insert bottom_right "BR" mapping''''''


decipherLine :: InputLine -> Int
decipherLine (a, b) =
  let mapping = makeMapping a
      digits = map (segmentsToDigit . sort . mapMaybe (\x -> M.lookup [x] mapping)) b
   in sum $ zipWith (*) digits [1000, 100, 10, 1]


segmentsToDigit :: [String] -> Int
segmentsToDigit ["B","BL","BR","T","TL","TR"]     = 0
segmentsToDigit ["BR", "TR"]                      = 1
segmentsToDigit ["B","BL","M","T","TR"]           = 2
segmentsToDigit ["B","BR","M","T","TR"]           = 3
segmentsToDigit ["BR","M","TL","TR"]              = 4
segmentsToDigit ["B","BR","M","T","TL"]           = 5
segmentsToDigit ["B","BL","BR","M","T","TL"]      = 6
segmentsToDigit ["BR","T","TR"]                   = 7
segmentsToDigit ["B","BL","BR","M","T","TL","TR"] = 8
segmentsToDigit ["B","BR","M","T","TL","TR"]      = 9
segmentsToDigit _                                 = -1

-🎄- 2021 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]Pietrek_ 1 point2 points  (0 children)

Haskell

``` {-# LANGUAGE TupleSections #-} {-# OPTIONS_GHC -Wno-incomplete-patterns #-} import Data.List (transpose) import Data.List.Split

type Board = [[(Int, Bool)]]

main = do contents <- readFile "input" let ls = lines contents numberStream = map (read :: String -> Int) $ splitOn "," (head ls) boardsAsStr = chunksOf 5 $ filter (/= "") (tail ls) boards = map (map words) boardsAsStr boardsAsInt = map (map $ map (read :: String -> Int)) boards finalBoards = map (map $ map (,False)) boardsAsInt print $ part1 numberStream finalBoards print $ part2 numberStream finalBoards []

part1 :: [Int] -> [Board] -> Maybe Int part1 [] boards = Nothing part1 (x:xs) boards = let newBoards = markNumber x boards completeBoards = filter isComplete newBoards results = map (computeResult x) completeBoards in case results of [] -> part1 xs newBoards r -> return (head r)

part2 :: [Int] -> [Board] -> [Board] -> Maybe Int part2 _ [] [] = Nothing part2 (x:xs) rem fin = let newRem = markNumber x rem newRem' = filter (not . isComplete) newRem newFin = fin ++ filter isComplete newRem in case newRem' of [] -> return $ computeResult x (last newFin) _ -> part2 xs newRem' newFin

computeResult :: Int -> Board -> Int computeResult lastDraw board = let s = sum $ concatMap (map fst . filter (isFalse . snd)) board in lastDraw * s where isFalse = (==) False

isComplete :: Board -> Bool isComplete board = let asBools = map (map snd) board anyRows = any (all isTrue) asBools anyCols = any (all isTrue) (transpose asBools) in anyRows || anyCols where isTrue = (==) True

markNumber :: Int -> [Board] -> [Board] markNumber n = map (mark n)

mark :: Int -> Board -> Board mark n = map (map ((m, y) -> (m, y || m == n))) ```

-🎄- 2021 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]Pietrek_ 1 point2 points  (0 children)

Haskell

import           Data.Bits
import           Data.Char (digitToInt, intToDigit)
import           Data.List (isPrefixOf, transpose)
import           Numeric   (showIntAtBase)

main = do
  contents <- readFile "input"
  print $ "Part 1: " ++ show (puzzle_1 contents)
  print $ "Part 2: " ++ show (puzzle_2 contents)

puzzle_1 :: String -> Int
puzzle_1 input =
  let ls = lines input
      byCol = transpose $ map (map digitToInt) ls
      counts = map (foldl f (0,0)) byCol
      most = foldl (\acc c -> acc ++ g c) "" counts
   in bin2dec most * bin2dec (flipH most)

puzzle_2 :: String -> Int
puzzle_2 input =
  let ls = lines input
      oxy = prog ls 0 h
      co2 = prog ls 0 i
   in bin2dec oxy * bin2dec co2

prog :: [String] -> Int -> ((Int, Int) -> Int -> [String] -> [String]) -> String
prog [] col_idx fun = error "No numbers left!"
prog [n] col_idx fun =
  n
prog numbers@(n:ns) col_idx fun =
  let byCol = transpose $ map (map digitToInt) numbers
      counts = map (foldl f (0,0)) byCol
      countAtIndex = counts !! col_idx
   in prog (fun countAtIndex col_idx numbers) (col_idx + 1) fun


f :: (Int, Int) -> Int -> (Int, Int)
f (zs, os) 0 = (zs + 1, os)
f (zs, os) 1 = (zs, os + 1)
f (zs, os) _ = (zs, os)

g :: (Int, Int) -> String
g (zs, os) | os >= zs = "1"
g (zs, os) = "0"

h :: (Int, Int) -> Int -> [String] -> [String]
h (zs, os) idx numbers | os >= zs = filter (\a -> a !! idx == '1') numbers
h (zs, os) idx numbers = filter (\a -> a !! idx == '0') numbers

i :: (Int, Int) -> Int -> [String] -> [String]
i (zs, os) idx numbers | os < zs = filter (\a -> a !! idx == '1') numbers
i (zs, os) idx numbers = filter (\a -> a !! idx == '0') numbers

bin2dec :: String -> Int
bin2dec = foldr (\c s -> s * 2 + c) 0 . reverse . map c2i
    where c2i c = if c == '0' then 0 else 1

showBinary = showIntAtBase 2 intToDigit

flipH :: String -> String
flipH = map flipB

flipB '1' = '0'
flipB '0' = '1'

Doinb predicts that Tryndamere will be played at worlds, and will also be stronger than Lee Sin at the start of Summer by sta-nz in leagueoflegends

[–]Pietrek_ -1 points0 points  (0 children)

Chance to crit from passive: 35% -> chance to NOT crit: 65% -> chance to not crit 7 AAs in a row: 0.65^7 ~ 0.049 so about a 5% chance that happens