Micah Halter 2 месяцев назад
Родитель
Сommit
d5d823fedf
4 измененных файлов: 17 добавлений и 19 удалений
  1. +2
    -3
      01-the-tyranny-of-the-rocket-equation/answer.hs
  2. +2
    -3
      03-crossed-wires/answer.hs
  3. +6
    -4
      06-universal-orbit-map/answer.hs
  4. +7
    -9
      07-amplification-circuit/answer.hs

+ 2
- 3
01-the-tyranny-of-the-rocket-equation/answer.hs Просмотреть файл

@@ -4,9 +4,8 @@ mass1 :: Int -> Int
mass1 x = (x `div` 3) - 2

mass2 :: Int -> Int
mass2 x = do
let m = mass1 x
if m > 0 then m + mass2 m else 0
mass2 x = if m > 0 then m + mass2 m else 0
where m = mass1 x

main = do
inputs <- getArgs


+ 2
- 3
03-crossed-wires/answer.hs Просмотреть файл

@@ -13,10 +13,9 @@ getDir dir = if dir == 'L' || dir == 'D' then -1 else 1

-- Calculate an array of points in a single step of the wire
getPoints :: Char -> Int -> [[Int]] -> [[Int]]
getPoints dir len points = do
let [x,y] = last points
points ++ [if dir == 'L' || dir == 'R' then [x + point, y] else [x, y + point]
getPoints dir len points = points ++ [if dir == 'L' || dir == 'R' then [x + point, y] else [x, y + point]
| point <- Prelude.map ((getDir dir)*) [1..len]]
where [x,y] = last points

-- Calculate all the points for all the steps
getAllPoints :: [String] -> [[Int]] -> [[Int]]


+ 6
- 4
06-universal-orbit-map/answer.hs Просмотреть файл

@@ -14,9 +14,10 @@ orbits :: [(String, String)] -> Map String [String]
orbits input = fromListWith (++) $ second (:[]) <$> input

totalOrbits :: Map String [String] -> Int
totalOrbits orb = sum checksums where
checksums = checksum <$> orb
checksum = sum . Prelude.map (maybe 1 (+ 1) . flip Map.lookup checksums)
totalOrbits orb = sum checksums
where
checksums = checksum <$> orb
checksum = sum . Prelude.map (maybe 1 (+ 1) . flip Map.lookup checksums)

solveA :: [String] -> Int
solveA input = totalOrbits $ orbits $ Prelude.map parse input
@@ -32,7 +33,8 @@ path :: [(String, String)] -> String -> [String]
path orbits = (reverse .) . unfoldr $ fmap (join (,)) . flip Map.lookup (rorbits orbits)

solveB :: [String] -> Int
solveB input = uncurry ((+) `on` length) $ dropCommonPrefix (path parsed "SAN") (path parsed "YOU") where parsed = Prelude.map parse input
solveB input = uncurry ((+) `on` length) $ dropCommonPrefix (path parsed "SAN") (path parsed "YOU")
where parsed = Prelude.map parse input

main = do
input <- getArgs


+ 7
- 9
07-amplification-circuit/answer.hs Просмотреть файл

@@ -14,15 +14,13 @@ startingStates prog comps = update 0 (state, (input ++ [0], output)) out
(state, (input, output)) = out `index` 0

runSeries :: Seq ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> [Int] -> Int -> Int
runSeries states comps idx = do
let (state, (newInput, newOutput:outputs)) = uncurry compute $ index states idx
let newStates = update idx (state, (newInput, newOutput:outputs)) states
let newIdx = if idx+1 == Data.List.length comps then 0 else idx+1
let ((nextProg, nextY, nextZ), (nextInput, nextOutput)) = index newStates newIdx
if nextY == -1
then newOutput
else
runSeries (update newIdx ((nextProg, nextY, nextZ), (nextInput ++ [newOutput], nextOutput)) newStates) comps newIdx
runSeries states comps idx = if nextY == -1 then newOutput
else runSeries (update newIdx ((nextProg, nextY, nextZ), (nextInput ++ [newOutput], nextOutput)) newStates) comps newIdx
where
(state, (newInput, newOutput:outputs)) = uncurry compute $ index states idx
newStates = update idx (state, (newInput, newOutput:outputs)) states
newIdx = if idx+1 == Data.List.length comps then 0 else idx+1
((nextProg, nextY, nextZ), (nextInput, nextOutput)) = index newStates newIdx

findMax :: Map.Map Int Int -> [[Int]] -> Int
findMax prog comps = snd $ maximumBy (comparing snd) $ map (\i -> (i, runSeries (startingStates prog i) i 0)) comps


Загрузка…
Отмена
Сохранить