Semigroup has been added to Prelude, so implementations of Monoid were
broken. For day 20, use of the AC-Vector library has been replaced with various implementations of typeclasses necessary to make everything work. Extra packages used have also been added to the README.
This commit is contained in:
parent
26da4fd154
commit
a0df5a759c
3
11.hs
3
11.hs
|
@ -2,9 +2,10 @@ import Data.List.Split (splitOn)
|
|||
import Data.Foldable (fold)
|
||||
|
||||
data Coordinates = Coordinates Int Int Int deriving Show
|
||||
instance Semigroup Coordinates where
|
||||
Coordinates x y z <> Coordinates x' y' z' = Coordinates (x + x') (y + y') (z + z')
|
||||
instance Monoid Coordinates where
|
||||
mempty = Coordinates 0 0 0
|
||||
Coordinates x y z `mappend` Coordinates x' y' z' = Coordinates (x + x') (y + y') (z + z')
|
||||
|
||||
getCoordinates :: String -> Coordinates
|
||||
getCoordinates direction = case direction of
|
||||
|
|
33
20.hs
33
20.hs
|
@ -2,30 +2,41 @@ import Data.List.Split (splitOn)
|
|||
import Data.List (elemIndex, sortOn, groupBy)
|
||||
import Data.Monoid ((<>))
|
||||
import Data.Function (on)
|
||||
import Data.Vector.Class
|
||||
import Data.Vector.V3
|
||||
|
||||
data Particle = Particle {
|
||||
position :: Vector3,
|
||||
velocity :: Vector3,
|
||||
acceleration :: Vector3
|
||||
}
|
||||
data Vector3 s = Vector3 s s s deriving Eq
|
||||
|
||||
instance Ord Vector3 where
|
||||
instance Functor Vector3 where
|
||||
fmap f (Vector3 x y z) = Vector3 (f x) (f y) (f z)
|
||||
|
||||
instance Num s => Semigroup (Vector3 s) where
|
||||
Vector3 x1 y1 z1 <> Vector3 x2 y2 z2 = Vector3 (x1 + x2) (y1 + y2) (z1 + z2)
|
||||
|
||||
instance Ord s => Ord (Vector3 s) where
|
||||
Vector3 x1 y1 z1 `compare` Vector3 x2 y2 z2 = compare x1 x2 <> compare y1 y2 <> compare z1 z2
|
||||
|
||||
norm :: Vector3 -> Double
|
||||
type Property = Vector3 Double
|
||||
|
||||
data Particle = Particle {
|
||||
position :: Property,
|
||||
velocity :: Property,
|
||||
acceleration :: Property
|
||||
}
|
||||
|
||||
(*|) :: Double -> Property -> Property
|
||||
(*|) s = fmap (s *)
|
||||
|
||||
norm :: Property -> Double
|
||||
norm (Vector3 x y z) = abs x + abs y + abs z
|
||||
|
||||
updateParticle :: Double -> Particle -> Particle
|
||||
updateParticle t (Particle p v a) =
|
||||
Particle (p + t *| v + (t * (t + 1) / 2) *| a) (v + t *| a) a
|
||||
Particle (p <> t *| v <> (t * (t + 1) / 2) *| a) (v <> t *| a) a
|
||||
|
||||
stepParticles :: [Particle] -> [Particle]
|
||||
stepParticles particles =
|
||||
concat . filter ((== 1) . length) . groupBy ((==) `on` position) . sortOn position . map (updateParticle 1) $ particles
|
||||
|
||||
parseProperty :: String -> Vector3
|
||||
parseProperty :: String -> Property
|
||||
parseProperty str =
|
||||
let x : y : z : [] = map read . splitOn "," . drop 3 . init $ str
|
||||
in Vector3 x y z
|
||||
|
|
11
README.md
11
README.md
|
@ -40,3 +40,14 @@ These are the runtimes of only one trial but the variances are fairly small and
|
|||
Problems that could use some work: 05, 22, 24, 25, 14, 15, 21, 17
|
||||
|
||||
Problems that are good enough with optimizations: 13
|
||||
|
||||
## Dependencies
|
||||
|
||||
I haven't gotten Stack set up with this project (I'll do it next year!), so here's a list of the extra dependencies needed by various files.
|
||||
|
||||
* hashmap
|
||||
* matrix
|
||||
* split
|
||||
* tuple
|
||||
* unordered-containers
|
||||
* vector
|
||||
|
|
Loading…
Reference in New Issue