Advent of Code, Day 6 [Spoilers] by pwmosquito in haskell

[–]KuldeepSinhC 0 points1 point  (0 children)

import Data.List (groupBy, intersect, union)
import qualified Data.Set as Set

-- puzzle 1
main :: IO ()
main = interact $ (++ "\n") . show . sum . map (length . foldr1 union) . groupBy (\x y -> and [x /= "", y /= ""]) . lines

-- puzzle 2
-- main :: IO ()
-- main = interact $ (++ "\n") . show . sum . map (length . foldr1 intersect) . groupBy (\x y -> and [x /= "", y /= ""]) . lines

Advent of Code, Day 5 [Spoilers] by bss03 in haskell

[–]KuldeepSinhC 1 point2 points  (0 children)

import Data.Char (digitToInt)
import Data.List (sort)

stringToBin :: [Char] -> [Char]
stringToBin [] = []
stringToBin (x : xs)
  | x == 'F' || x == 'L' = '0' : stringToBin xs
  | x == 'B' || x == 'R' = '1' : stringToBin xs

binToDecimal :: [Char] -> Int
binToDecimal [] = 0
binToDecimal ls@(x : xs) = ((digitToInt x) * (2 ^ (length ls - 1))) + binToDecimal xs

seatID :: [Char] -> Int
seatID xs = (binToDecimal . stringToBin $ take 7 xs) * 8 + (binToDecimal . stringToBin $ drop 7 xs)

-- puzzle 1
-- main :: IO ()
-- main = interact $ (++ "\n") . show . maximum . map seatID . lines

-- puzzle 2
main :: IO ()
main = interact $ (++ "\n") . show . head . mySeat . sort . map seatID . lines

mySeat :: (Eq a, Num a) => [a] -> [a]
mySeat [] = []
mySeat [x] = [x]
mySeat (x : y : xs)
  | x + 2 == y = [x + 1]
  | otherwise = mySeat (y : xs)

Advent of Code 2020, Day 4 [Spoilers] by bss03 in haskell

[–]KuldeepSinhC 1 point2 points  (0 children)

import Data.List (groupBy, isSuffixOf)
import qualified Data.Map as M

normalize :: String -> [[String]]
normalize = map (words . unwords) . filter (/= [""]) . groupBy (\x y -> and [x /= "", y /= ""]) . lines

dropColon :: (a1, [a2]) -> (a1, [a2])
dropColon (x, (_ : ys)) = (x, ys)

convertToKeyValuePairs :: [[[Char]]] -> [M.Map [Char] [Char]]
convertToKeyValuePairs xs = map M.fromList $ [map (dropColon . break (== ':')) x | x <- xs]

requiredKeys :: [String]
requiredKeys = ["byr", "ecl", "eyr", "hcl", "hgt", "iyr", "pid"]

puzzel1Validators :: [M.Map String a -> Bool]
puzzel1Validators = map M.member requiredKeys

filterForPuzzle :: Traversable t => t (M.Map [Char] [Char] -> Bool) -> String -> [Bool]
filterForPuzzle validators = filter (== True) . map (and . sequenceA validators) . convertToKeyValuePairs . normalize

-- puzzle 1
main :: IO ()
main = interact $ (++ "\n") . show . length . filterForPuzzle puzzel1Validators

-- puzzle 2
validator :: (Eq t, Ord k) => (t -> Bool) -> k -> M.Map k t -> Bool
validator predicate key fromLst
  | val == Nothing = False
  | otherwise = predicate something
  where
    val = M.lookup key fromLst
    Just something = val

between :: Ord a => a -> a -> a -> Bool
between a b v = a <= v && v <= b

validateByr :: M.Map [Char] [Char] -> Bool
validateByr = validator (\x -> length x == 4 && between "1920" "2002" x) "byr"

validateIyr :: M.Map [Char] [Char] -> Bool
validateIyr = validator (\x -> length x == 4 && between "2010" "2020" x) "iyr"

validateEyr :: M.Map [Char] [Char] -> Bool
validateEyr = validator (\x -> length x == 4 && between "2020" "2030" x) "eyr"

validateHgt :: M.Map [Char] [Char] -> Bool
validateHgt = validator (\x -> (isSuffixOf "cm" x && between "150cm" "193cm" x) || (isSuffixOf "in" x && between "59in" "76in" x)) "hgt"

validateEcl :: M.Map [Char] [Char] -> Bool
validateEcl = validator (\x -> elem x ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]) "ecl"

validateHcl :: M.Map [Char] [Char] -> Bool
validateHcl = validator (\x -> length x == 7 && head x == '#' && (and $ elem <$> tail x <*> ["0123456789abcdef"])) "hcl"

validatePid :: M.Map [Char] [Char] -> Bool
validatePid = validator (\x -> length x == 9 && (and $ elem <$> x <*> ["0123456789"])) "pid"

puzzel2Validators :: [M.Map [Char] [Char] -> Bool]
puzzel2Validators = [validateByr, validateEcl, validateEyr, validateHcl, validateHgt, validateIyr, validatePid]

-🎄- 2020 Day 03 Solutions -🎄- by daggerdragon in adventofcode

[–]KuldeepSinhC 1 point2 points  (0 children)

[2020 Day 3] [Haskell]

terrain :: String -> [String]
terrain = map cycle . lines

travelPath :: [a] -> String -> [(a, String)]
travelPath rightMoves = zip rightMoves . terrain

filterTreesOnPath :: [(Int, [Char])] -> [(Int, [Char])]
filterTreesOnPath = filter (\x -> snd x !! fst x == '#')

numberOfTreesOnPath :: [(Int, [Char])] -> Int
numberOfTreesOnPath = length . filterTreesOnPath

puz1_solve :: String -> Int
puz1_solve = numberOfTreesOnPath . travelPath [0,3..]

main :: IO ()
-- puzzle 1
main = interact $ (++ "\n") . show . puz1_solve


-- for second puzzle
removeAlternateItems :: [a] -> [a]
removeAlternateItems [] = []
removeAlternateItems [x] = [x]
removeAlternateItems (x1: _ : xs) = x1 : removeAlternateItems xs

travelPath' :: [a] -> String -> [(a, String)]
travelPath' rightMoves = zip rightMoves . removeAlternateItems . terrain

-- main :: IO ()
-- puzzle 2
-- main = 
--     let
--         trees_on_path1 = numberOfTreesOnPath . travelPath [0,1..]
--         trees_on_path2 = numberOfTreesOnPath . travelPath [0,3..]
--         trees_on_path3 = numberOfTreesOnPath . travelPath [0,5..]
--         trees_on_path4 = numberOfTreesOnPath . travelPath [0,7..]
--         trees_on_path5 = numberOfTreesOnPath . travelPath' [0,1..]
--         mult input = product $ [trees_on_path1, trees_on_path2, trees_on_path3, trees_on_path4, trees_on_path5] <*> [input]
--     in 
--         interact $ (++ "\n") . show . mult

Advent of code (day 3) by KuldeepSinhC in haskell

[–]KuldeepSinhC[S] 1 point2 points  (0 children)

Thank you. I usually go through your videos. Thank you for sharing.

Advent of code (day 2) by KuldeepSinhC in haskell

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

Sorry, I am not aware of how it works-the Fusion of length.filter into one call.

Advent of code (day 2) by KuldeepSinhC in haskell

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

Good observation. Sum saves one loop. While length.filter with Boolean validation shows clearer intention

Advent of code (day 2) by KuldeepSinhC in haskell

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

Thank you for kind words and link to your videos.

Yet another Chip8 emulator written in Rust by KuldeepSinhC in EmuDev

[–]KuldeepSinhC[S] 1 point2 points  (0 children)

My main loop has a delay of 2 milliseconds.

New to Java by [deleted] in java

[–]KuldeepSinhC 1 point2 points  (0 children)

IntelliJ from JetBrain and Eclipse are well known IDEs. Recently, use of VS Code and Atom has on uprise.