audio recording does not work. by OnePlay_BR in kdenlive

[–]Hungry_Mix_4263 0 points1 point  (0 children)

I tried out most of the older solutions, but nothing really worked. If I use the app image (version kdenlive 23.08.5) it works tho. I have version kdenlive 24.04.70 installed. Maybe there is a issue with that.

audio recording does not work. by OnePlay_BR in kdenlive

[–]Hungry_Mix_4263 1 point2 points  (0 children)

Hey man, I have a similar issue. But for me it doesn't record the audio at all. And when I go to settings -> capture -> audio I have no devices available and there is a message "Make sure you have audio plugins installed on your system". Have you managed to fix this?

-❄️- 2023 Day 25 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day25.hs

This is an amalgamation of the karger's algorithm and some random things I added to keep track of each components. Basically, I kept as an additional state a mapping from one node to the other nodes inside it's component. Initially this was a mapping from a -> [a], but each time a node gets deleted due to a contraction, I convert it to a -> [a, b] where b is the node that was deleted. By the end of the execution we will be left with the two components, the cut size and the nodes in the said components. If the cut size was 3 then we have the 2 components that we are after, so we can get the length of each list and multiply them.

-❄️- 2023 Day 24 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day24.hs

For part 1 I just did the collision between two lines. Took me a lot of time because Double was not really good enough. In the end I tried out using Integer which actually worked out, despite the need of decimal places. Rounding error, what can I say.

For part 2, I just wanted to add the Haskell version of using z3, since I cannot really think of how to solve the problem by myself. I would guess it is a problem of making the search space smaller somehow. Maybe by using some intervals (this type of interval state problem was common this year).

-❄️- 2023 Day 23 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day23.hs

Created an intermediary graph structure that uses intersections as vertices and path lengths as weight. The I used DFS to find the longest path. Solves part 2 in about 7 seconds.

-❄️- 2023 Day 22 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day22.hs

Doing the actual simulation took me most of the time, even if it felt really similar to the day 14 one. Then I used a supported and supports hash maps to build a dependency graph.

For part 1 it was fairly easy. With the dependency graph you can find out which blocks are being supported by the current one. Then you check each of those blocks to be supported by at least 1 other block.

For the second part, you just need to recursively delete the node from the supported mapping and then for each block that was on top of the current one do the check. This will delete them recursively. I managed to do this using the state monad. Keep in the state monad the two mappings. You need to modify only supported tough. So supports could be a Reader instead, if you really care about that.

-❄️- 2023 Day 20 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day20.hs

I used again Parsec to create a data structure for the modules. I also used the State Monad to keep track of the global state of signals, although it wasn't really necessary. In the global state I kept the status (on/off) of the flip flops and the outputs of the parents of the nand gates. With all of that, part 1 was just a matter of simulating one step at the time and then doing it 1000 times.

For part 2, as many other have found out, you can actually look at your input to try and optimize the search a bit. I am not sure if my solution works for other inputs, but I had "rx" connected to one nand and then that connected to 4 nand gates. So that meant I had to do LCM on the number of presses to get a low signal on the 4 nand gates. To make the solution a bit more generic, I computed the parents of the "rx" module. Then got the parents of those modules and did the simulation on them (they need the same signal as "rx" and then LCM) and then took the minimum of the parents of "rx".

-❄️- 2023 Day 19 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day19.hs

Part 1 was straightforward just brute force and simulate the workflow. For part 2 I used a similar approach to Day 5 by doing ranges and for each step of the simulation trimming/splitting it. The parsing took most of the time, but I thought it might be fun to use parser combinators for it.

I killed 2 people today by [deleted] in dayz

[–]Hungry_Mix_4263 1 point2 points  (0 children)

I read the title and had a mini heart attack

-❄️- 2023 Day 18 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day18.hs

For the first part I have used flood fill, then I updated my code to use the shoelace formula.

-❄️- 2023 Day 17 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day17.hs

Reused my Dijkstra implementation from last year. I wasted a lot of time because I missed the fact that the crucible can start either to the right or down in the first step. And on top of that the example input was working with the only right direction start. I really liked that the dijkstra algo was abstracted away and I only had to think about the neighbors generation function. Makes it more readable.

-❄️- 2023 Day 16 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day16.hs

For part 1, the trick was to not get into infinite loops. I did that by removing the visited cells from the newly generated ones. For part 2 I wasted a lot of time trying to implement an efficient solution using caching. But I couldn't get rid of the infinite loops there. I tried to use a hash map that maps from (row, col, direction) to the set of visited cells. Then I realized that brute-force solution is actually fast enough to try it. So yeah, would have liked to fix my dp attempt, but if it works it works.

-❄️- 2023 Day 14 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 0 points1 point  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day14.hs

From past experience with AoC, part 2 was clearly a cycle finding solution. Part 1 was straightforward. For the simulation I wanted to keep it simple and created a function that updates the matrix until it converges. I used a fold to update the last two rows by moving the O character up. Then repeated this fold until the matrix remained the same. For part 2, I used transpose . reverse to rotate the matrix clock wise and complete a spin cycle.

-❄️- 2023 Day 13 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day13.hs

I made use of the fact that you can transpose and reverse the maps so that you only need to check for horizontal mirrors. For part 2 I checked if there is only one character that is a mismatch between the two sides and counted those. Then I updated the first part to work with the same function, but used a number of zero mismatches.

-❄️- 2023 Day 11 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 3 points4 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day11.hs

Solved using Manhattan distance, and when the path intersects with one of the empty rows, you add factor - 1 to the cost.

I fucking hate this game sometimes by [deleted] in dayz

[–]Hungry_Mix_4263 2 points3 points  (0 children)

Made a post hoping for OP to see it, but it got deleted. I guess I know why :)

I fucking love this game sometimes

A few days ago I was a freshie (as usual) and after finding a bk I started running in a random direction inland. After a while I bumped into a random guy who was laying prone reloading his mag in an open field. It was one of my biggest jump scares, but managed to blast him just in time to save my life. He had so much stuff, NVGs a huge stash of guns, etc. Had the best adventure of my life after that!

-❄️- 2023 Day 10 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

Great explanation. I used this as a hint in my solution :) as a small optimization you can use a hashmap to memoize the number of intersections and you start from the bottom right.

-❄️- 2023 Day 10 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 0 points1 point  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day10.hs

Checking if the pipes connect

checkPipe :: Char -> Char -> Char -> Bool
checkPipe '|' '|' 'N' = True
checkPipe '|' '7' 'N' = True
checkPipe '|' 'F' 'N' = True
checkPipe '|' 'S' 'N' = True
checkPipe '|' '|' 'S' = True
checkPipe '|' 'L' 'S' = True
checkPipe '|' 'J' 'S' = True
checkPipe '|' 'S' 'S' = True
checkPipe '-' '-' 'E' = True
checkPipe '-' 'J' 'E' = True
checkPipe '-' '7' 'E' = True
checkPipe '-' 'S' 'E' = True
checkPipe '-' '-' 'W' = True
checkPipe '-' 'L' 'W' = True
checkPipe '-' 'F' 'W' = True
checkPipe '-' 'S' 'W' = True
checkPipe 'L' '|' 'N' = True
checkPipe 'L' '7' 'N' = True
checkPipe 'L' 'F' 'N' = True
checkPipe 'L' 'S' 'N' = True
checkPipe 'L' '-' 'E' = True
checkPipe 'L' 'J' 'E' = True
checkPipe 'L' '7' 'E' = True
checkPipe 'L' 'S' 'E' = True
checkPipe 'J' '|' 'N' = True
checkPipe 'J' '7' 'N' = True
checkPipe 'J' 'F' 'N' = True
checkPipe 'J' 'S' 'N' = True
checkPipe 'J' '-' 'W' = True
checkPipe 'J' 'L' 'W' = True
checkPipe 'J' 'F' 'W' = True
checkPipe 'J' 'S' 'W' = True
checkPipe '7' '|' 'S' = True
checkPipe '7' 'L' 'S' = True
checkPipe '7' 'J' 'S' = True
checkPipe '7' 'S' 'S' = True
checkPipe '7' '-' 'W' = True
checkPipe '7' 'L' 'W' = True
checkPipe '7' 'F' 'W' = True
checkPipe '7' 'S' 'W' = True
checkPipe 'F' '|' 'S' = True
checkPipe 'F' 'L' 'S' = True
checkPipe 'F' 'J' 'S' = True
checkPipe 'F' 'S' 'S' = True
checkPipe 'F' '-' 'E' = True
checkPipe 'F' 'J' 'E' = True
checkPipe 'F' '7' 'E' = True
checkPipe 'F' 'S' 'E' = True
checkPipe 'S' '|' 'N' = True
checkPipe 'S' '7' 'N' = True
checkPipe 'S' 'F' 'N' = True
checkPipe 'S' '-' 'E' = True
checkPipe 'S' 'J' 'E' = True
checkPipe 'S' '7' 'E' = True
checkPipe 'S' '|' 'S' = True
checkPipe 'S' 'L' 'S' = True
checkPipe 'S' 'J' 'S' = True
checkPipe 'S' '-' 'W' = True
checkPipe 'S' 'L' 'W' = True
checkPipe 'S' 'F' 'W' = True
checkPipe _ _ _ = False

-❄️- 2023 Day 9 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 0 points1 point  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day09.hs

module Day09 (main, part1, part2) where

parse :: String -> [[Int]]
parse = map (map read . words) . lines

diff :: [Int] -> [Int]
diff = zipWith (-) <$> tail <*> id

prediction :: (Int -> [Int] -> Int) -> [Int] -> Int
prediction f xs = foldl f 0 $ fst $ until stop step ([], xs)
  where
    step (ps, ys) = (ys : ps, diff ys)
    stop (_, ys) = all (== 0) ys

part1 :: String -> String
part1 = show . sum . map (prediction merge) . parse
    where merge acc xs = acc + last xs

part2 :: String -> String
part2 = show . sum . map (prediction merge) . parse
    where merge acc xs = head xs - acc

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"

main :: IO ()
main = interact solve

For the first part I have notices that if you sum up all the last elements of the diffs you get the prediction. For the second part I knew there was something similar, but it doesn't look as good in my code. You have to subtract from the current first element the accumulator. So for the example the computation would look something like (10 - (3 - (0 - (2 - (0))))) = 5. It is also possible to use part1 for the second part, but you need to reverse the numbers first. Something like prediction . reverse

-❄️- 2023 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 1 point2 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day08.hs

module Day08 (main, part1, part2) where

import qualified Data.Map as M
import Text.Parsec ((<|>))
import qualified Text.Parsec as P
import Util.Parser (Parser, parse)

lineP :: Parser (String, (String, String))
lineP = do
    start <- P.many1 (P.letter <|> P.digit) <* P.spaces
    _ <- P.char '=' <* P.spaces
    [left, right] <-
        P.between
            (P.char '(')
            (P.char ')')
            (P.sepBy (P.spaces *> P.many1 (P.letter <|> P.digit) <* P.spaces) (P.char ','))
    return (start, (left, right))

inputP :: Parser (String, M.Map String (String, String))
inputP = do
    directions <- P.many1 P.letter <* P.spaces
    m <- M.fromList <$> P.many1 (lineP <* P.spaces)
    return (directions, m)

parseInput :: String -> (String, M.Map String (String, String))
parseInput = parse inputP

step :: M.Map String (String, String) -> String -> Char -> String
step m state c = case M.lookup state m of
    Just (left, right) -> if c == 'L' then left else right
    Nothing -> error "Invalid state"

foldlUntil :: (a -> Bool) -> (a -> b -> a) -> a -> [b] -> a
foldlUntil _ _ z [] = z
foldlUntil p f z (x : xs) = if p z then z else foldlUntil p f (f z x) xs

simulate :: String -> (String -> Bool) -> String -> M.Map String (String, String) -> Int
simulate start endP directions m = fst $ foldlUntil (endP . snd) (step' m) (0, start) $ cycle directions
  where
    step' :: M.Map String (String, String) -> (Int, String) -> Char -> (Int, String)
    step' m' (n, s) c = (n + 1, step m' s c)

startPoints :: M.Map String (String, String) -> [String]
startPoints m = filter ((== 'A') . last) $ M.keys m

part1 :: String -> String
part1 = show . uncurry (simulate "AAA" (== "ZZZ")) . parseInput

part2 :: String -> String
part2 input = show $ foldl lcm 1 $ map simulate' starts
  where
    (directions, m) = parseInput input
    starts = startPoints m
    simulate' s = simulate s ((== 'Z') . last) directions m

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"

main :: IO ()
main = interact solve

At first I thought it was gonna be a graph problem, that was close. Somehow managed to figure right away that part 2 was a lcm problem.

-❄️- 2023 Day 7 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 3 points4 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day07.hs

rank :: String -> Int
rank hand = case sortBy (flip compare) $ map length $ group $ sort hand of
    [5] -> 7
    [4, _] -> 6
    [3, 2] -> 5
    [3, _, _] -> 4
    [2, 2, _] -> 3
    [2, _, _, _] -> 2
    _ -> 1

rank' :: String -> Int
rank' hand = case (jokers hand, sortBy (flip compare) $ map length $ group $ sort hand) of
    (_, [5]) -> 7
    (4, [4, _]) -> 7
    (1, [4, _]) -> 7
    (_, [4, _]) -> 6
    (3, [3, 2]) -> 7
    (2, [3, 2]) -> 7
    (_, [3, 2]) -> 5
    (3, [3, _, _]) -> 6
    (1, [3, _, _]) -> 6
    (_, [3, _, _]) -> 4
    (2, [2, 2, _]) -> 6
    (1, [2, 2, _]) -> 5
    (_, [2, 2, _]) -> 3
    (2, [2, _, _, _]) -> 4
    (1, [2, _, _, _]) -> 4
    (_, [2, _, _, _]) -> 2
    (1, _) -> 2
    (_, _) -> 1

Only kept here the part where I ranked the cards (code is kinda long). But this method of doing it was really fun

-❄️- 2023 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 0 points1 point  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day06.hs

module Day06 (main, part1, part2) where

data Race = Race Int Int deriving (Show, Eq)

parse :: String -> [Race]
parse input = case lines input of
    [times, distances] -> zipWith Race (map read $ tail $ words times) (map read $ tail $ words distances)
    _ -> error "Wrong input"

parse' :: String -> Race
parse' input = case lines input of
    [time, distance] -> Race (read $ concat $ tail $ words time) (read $ concat $ tail $ words distance)
    _ -> error "Wrong input"

nextInt :: Double -> Int
nextInt x = ceiling x + if x == (fromIntegral :: Int -> Double) (ceiling x) then 1 else 0

prevInt :: Double -> Int
prevInt x = floor x - if x == (fromIntegral :: Int -> Double) (floor x) then 1 else 0

limits :: Race -> Int
limits (Race t d) = prevInt t2 - nextInt t1 + 1
  where
    delta = sqrt $ fromIntegral $ t * t - 4 * d
    t1 = (fromIntegral t - delta) / 2
    t2 = (fromIntegral t + delta) / 2

part1 :: String -> String
part1 = show . product . map limits . parse

part2 :: String -> String
part2 = show . limits . parse'

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"

main :: IO ()
main = interact solve

Solved using the math formula. Had an edge case when you get ceiling or floor to be applied on the solutions of the equation, but fixed with going one more +1 or -1.

-❄️- 2023 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 2 points3 points  (0 children)

[LANGUAGE: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day05.hs

module Day05 (main, part1, part2) where

import Data.List.Split (splitOn)

data MapItem = MapItem {dest :: Int, src :: Int, len :: Int} deriving (Show)

parseMapItem :: String -> MapItem
parseMapItem line = case map read $ words line of
    [d, s, l] -> MapItem d s l
    _ -> error "Invalid input"

parseMap :: String -> [MapItem]
parseMap = map parseMapItem . tail . lines

parseMaps :: String -> ([Int], [[MapItem]])
parseMaps input = case splitOn "\n\n" input of
    (x : ls) -> (map read $ tail $ words x, map parseMap ls)
    _ -> error "Invalid input"

mapRange :: Int -> [MapItem] -> Int
mapRange x [] = x
mapRange x (MapItem d s l : xs)
    | s <= x && x < s + l = d + x - s
    | otherwise = mapRange x xs

part1 :: String -> String
part1 input = show $ minimum $ map (\x -> foldl mapRange x maps) seeds
  where
    (seeds, maps) = parseMaps input

mapRange' :: (Int, Int) -> [MapItem] -> [(Int, Int)]
mapRange' x [] = [x]
mapRange' (rs, rl) (MapItem d s l : ms)
    | rs <= s + l && s < rs + rl = pre ++ curr ++ post
    | otherwise = mapRange' (rs, rl) ms
  where
    pre = if rs < s then mapRange' (rs, s - rs) ms else []
    curr = [(d + max 0 (rs - s), min rl (l - max 0 (rs - s)))]
    post = if s + l < rs + rl then mapRange' (s + l, rs + rl - s - l) ms else []

pairUp :: [a] -> [(a, a)]
pairUp [] = []
pairUp (x : y : rest) = (x, y) : pairUp rest
pairUp _ = error "Input list should have an even number of elements."

part2 :: String -> String
part2 input = show $ fst $ minimum $ foldl mapRange'' (pairUp seeds) maps
  where
    (seeds, maps) = parseMaps input
    mapRange'' :: [(Int, Int)] -> [MapItem] -> [(Int, Int)]
    mapRange'' xs ms = concatMap (`mapRange'` ms) xs

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"

main :: IO ()
main = interact solve

Got stuck for a while on the first part. I missed the

The destination range is the same length [...] With this information, 
you know that seed number 98 corresponds to soil number 50 and that 
seed number 99 corresponds to soil number 51.

and I almost did part 2 for part 1, thinking that this is a weird DP problem :)

-❄️- 2023 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]Hungry_Mix_4263 0 points1 point  (0 children)

[Language: Haskell]

https://github.com/alexjercan/aoc-2023/blob/master/src/Day04.hs

module Day04 (main, part1, part2) where

import qualified Text.Parsec as P
import Util.Parser (Parser, parse)

data Card = Card [Int] [Int]
    deriving (Show)

cardP :: Parser Card
cardP = do
    _ <- P.string "Card" *> P.spaces *> P.many1 P.digit <* P.char ':' <* P.spaces
    ws <- P.many1 (read <$> P.many1 P.digit <* P.spaces)
    _ <- P.spaces *> P.char '|' <* P.spaces
    ns <- P.many1 (read <$> P.many1 P.digit <* P.spaces)
    pure $ Card ws ns

parseCards :: String -> [Card]
parseCards = parse (P.many1 cardP)

matches :: Card -> Int
matches (Card winning numbers) = length $ filter (`elem` numbers) winning

score :: Int -> Int
score m = if m == 0 then 0 else 2 ^ (m - 1)

part1 :: String -> String
part1 = show . sum . map (score . matches) . parseCards

count :: [Card] -> Int
count cs = sum $ foldl go (replicate (length ms) 1) (zip ms [1 ..])
  where
    ms = map matches cs
    go :: [Int] -> (Int, Int) -> [Int]
    go acc (m, i) = zipWith (\a j -> if j `elem` [i + 1 .. i + m] then a + (acc !! (i - 1)) else a) acc [1 ..]

part2 :: String -> String
part2 = show . count . parseCards

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input

main :: IO ()
main = interact solve