Skip to content

Instantly share code, notes, and snippets.

@markroxor
Last active August 27, 2020 17:29
Show Gist options
  • Select an option

  • Save markroxor/aed9af671ea18364a9c31f24e1522c8c to your computer and use it in GitHub Desktop.

Select an option

Save markroxor/aed9af671ea18364a9c31f24e1522c8c to your computer and use it in GitHub Desktop.

Revisions

  1. markroxor revised this gist Aug 27, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion AOC.hs
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,9 @@ integrate l = [(n,p+1,(p+1)*d) | (n,p,d) <- l]
    sq l = [(n*n', p+p', d*d') | (n,p,d) <- l, (n',p',d') <- l ]

    -- evaluates the expr for a value
    solve_ l v = sum $ [( fromIntegral n)*(( fromIntegral v)^p) / (fromIntegral d) | (n, p, d) <- l]
    solve_ l v = sum ([( fromIntegral n)*(( fromIntegral v)^p) / (fromIntegral d) | (n, p, d) <- l, p >= 0]) + ( sum [( fromIntegral n)/(( fromIntegral v)^(-p) * (fromIntegral d)) | (n, p, d) <- l, p < 0])



    solve :: Int -> Int -> [Int] -> [Int] -> [Double]
    solve l r a b
  2. markroxor revised this gist Aug 27, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions AOC.hs
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,17 @@ import Text.Printf (printf)

    -- (n,p,d) represents coefficient, power, denominator of a algebric term.
    -- (4,2,3) == 4 * (x ^ 2) /3
    -- f(x) == n1 * (x ^ p1) / d1 + n2 * (x ^ p2) / d2 ....

    -- integrates f(x)
    integrate l = [(n,p+1,(p+1)*d) | (n,p,d) <- l]

    -- squares a function - f^2(x)
    -- f(x) = (x+1)
    -- f^2(x) = (x+1)^2
    sq l = [(n*n', p+p', d*d') | (n,p,d) <- l, (n',p',d') <- l ]

    -- evaluates the expr for a value
    solve_ l v = sum $ [( fromIntegral n)*(( fromIntegral v)^p) / (fromIntegral d) | (n, p, d) <- l]

    solve :: Int -> Int -> [Int] -> [Int] -> [Double]
    @@ -14,6 +21,7 @@ solve l r a b
    where
    c = [1,1 ..]
    f = zip3 a b c

    f_area = integrate f
    f_volume = integrate $ sq f

  3. markroxor revised this gist Aug 27, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions AOC.hs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    import Text.Printf (printf)

    -- (n,p,d) represents coefficient, power, denominator of a algebric term.
    -- (4,2,3) == 4 * (x ^ 2) /3

    integrate l = [(n,p+1,(p+1)*d) | (n,p,d) <- l]

    sq l = [(n*n', p+p', d*d') | (n,p,d) <- l, (n',p',d') <- l ]
  4. markroxor created this gist Aug 27, 2020.
    21 changes: 21 additions & 0 deletions AOC.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import Text.Printf (printf)

    integrate l = [(n,p+1,(p+1)*d) | (n,p,d) <- l]

    sq l = [(n*n', p+p', d*d') | (n,p,d) <- l, (n',p',d') <- l ]
    solve_ l v = sum $ [( fromIntegral n)*(( fromIntegral v)^p) / (fromIntegral d) | (n, p, d) <- l]

    solve :: Int -> Int -> [Int] -> [Int] -> [Double]
    solve l r a b
    | otherwise = [area, vol]
    where
    c = [1,1 ..]
    f = zip3 a b c
    f_area = integrate f
    f_volume = integrate $ sq f

    area = (solve_ f_area r) - (solve_ f_area l)
    vol = pi * ((solve_ f_volume r) - (solve_ f_volume l))

    main :: IO ()
    main = getContents >>= mapM_ (printf "%.1f\n"). (\[a, b, [l, r]] -> solve l r a b). map (map read. words). lines