Added Haskell Implementations and changed directory structure
This commit is contained in:
parent
9f40c27eb9
commit
cb9dee889b
14 changed files with 392 additions and 0 deletions
8
Haskell/2020/01/day1.hs
Normal file
8
Haskell/2020/01/day1.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
nums = [1655,1384,1752,1919,1972,1766,1852,1835,1408,1721,1879,1846,1394,1577,1588,1097,1748,1585,765,1375,1806,1785,1824,1847,1037,1531,1989,1570,1625,1600,1832,1927,1691,1593,1936,1812,570,1391,1883,1592,1875,1185,1903,855,1331,1742,1884,1356,1944,1994,1556,1271,1572,1661,1914,1905,1581,1634,1252,1657,989,1907,1998,1040,1833,1612,1725,1680,1869,1900,1550,1768,1727,1930,1810,1841,734,1779,1774,1825,1446,1259,1552,1310,1885,1689,1929,1959,787,1642,1890,1164,1986,1796,1465,1217,1741,1480,1683,1808,1058,1970,1361,2003,1898,1668,1754,1773,1235,1158,1975,1479,1995,1648,1023,883,1553,1658,1794,1747,1978,1268,1966,1192,1886,1471,1548,1819,1551,1958,1732,1676,1745,1501,1858,1652,1596,473,1314,1814,1409,1877,1344,1735,1635,1273,871,1643,1599,1565,1695,1803,1764,1778,1823,1831,1701,282,1089,1623,1639,1568,1469,1674,1818,1992,1597,1711,1359,1851,1496,1630,1755,1529,1881,1718,1916,1325,1578,1441,1722,1545,1472,1783,1497,1791,1183,1926,1937,1255,1095,1451,1395,1665,1432,1693,1821,1938,1941,2002]
|
||||||
|
|
||||||
|
|
||||||
|
productOf2020Sum :: (Num a, Eq a) => [a] -> [a]
|
||||||
|
productOf2020Sum x = [d | a <- x, b <- x, let d = a*b, a+b == 2020]
|
||||||
|
|
||||||
|
productOf2020Sum' :: (Num a, Eq a) => [a] -> [a]
|
||||||
|
productOf2020Sum' x = [d | a <- x, b <- x, c <- x, let d = a*b*c, a+b+c == 2020]
|
||||||
19
Haskell/2020/02/day2.hs
Normal file
19
Haskell/2020/02/day2.hs
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
-- parseInput :: String -> (Int, Int, Char)
|
||||||
|
-- parseInput st = (ord (head st), read (tail (take 3 st)), tail (take 5 st))
|
||||||
|
|
||||||
|
parseMinimum :: String -> String -> Int
|
||||||
|
parseMinimum st out
|
||||||
|
| h == '-' = read (reverse out)
|
||||||
|
| h `elem` ['0'..'9'] = parseMinimum (tail st) (h:out)
|
||||||
|
| otherwise = error "Unexpected input"
|
||||||
|
where h = head st
|
||||||
|
|
||||||
|
parseMax :: String -> String -> Int
|
||||||
|
parseMax st out
|
||||||
|
| h == ' ' = read (reverse out)
|
||||||
|
| h == '-' = parseMax (tail st) ""
|
||||||
|
| out == "!" = parseMax (tail st) "!"
|
||||||
|
| h `elem` ['0'..'9'] = parseMax (tail st) (h:out)
|
||||||
|
| otherwise = error "Unexpected input"
|
||||||
|
where h = head st
|
||||||
18
Haskell/2021/01/day1.hs
Normal file
18
Haskell/2021/01/day1.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
countIncreases :: [Integer] -> Integer
|
||||||
|
countIncreases [_] = 0
|
||||||
|
countIncreases x
|
||||||
|
| previous < current = 1 + countIncreases (tail x)
|
||||||
|
| otherwise = countIncreases (tail x)
|
||||||
|
where previous = head x
|
||||||
|
current = head (tail x)
|
||||||
|
|
||||||
|
--data1 = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]
|
||||||
|
|
||||||
|
window :: [Integer] -> [Integer]
|
||||||
|
window [] = []
|
||||||
|
window [_] = []
|
||||||
|
window [_,_] = []
|
||||||
|
window st = (a+b+c):window (tail st)
|
||||||
|
where a = head st
|
||||||
|
b = head (tail st)
|
||||||
|
c = head (tail (tail st))
|
||||||
24
Haskell/2021/02/day2.hs
Normal file
24
Haskell/2021/02/day2.hs
Normal file
File diff suppressed because one or more lines are too long
57
Haskell/2021/03/day3.hs
Normal file
57
Haskell/2021/03/day3.hs
Normal file
File diff suppressed because one or more lines are too long
99
Haskell/2021/04/day4.hs
Normal file
99
Haskell/2021/04/day4.hs
Normal file
File diff suppressed because one or more lines are too long
62
Haskell/2021/05/day5.hs
Normal file
62
Haskell/2021/05/day5.hs
Normal file
File diff suppressed because one or more lines are too long
38
Haskell/2021/05/day5_1.hs
Normal file
38
Haskell/2021/05/day5_1.hs
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
fand :: Bool -> Bool -> Bool
|
||||||
|
fand a b
|
||||||
|
| a == b = True
|
||||||
|
| otherwise = false
|
||||||
|
|
||||||
|
numberIntersections :: ((Integer, Integer), (Integer, Integer)) -> ((Integer, Integer), (Integer, Integer)) -> Int
|
||||||
|
numberIntersections ls1 ls2
|
||||||
|
| fand (orien11 /= orien12) (orien21 /= orien22) = 1
|
||||||
|
|
|
||||||
|
where orien11 = orientation (fst ls1) (snd ls1) (fst ls2)
|
||||||
|
orien12 = orientation (fst ls1) (snd ls1) (snd ls2)
|
||||||
|
orien21 = orientation (fst ls2) (snd ls2) (fst ls1)
|
||||||
|
orien22 = orientation (fst ls2) (snd ls2) (snd ls1)
|
||||||
|
|
||||||
|
onSegment :: (Integer, Integer) -> (Integer, Integer) -> (Integer, Integer) -> Int
|
||||||
|
onSegment p q r =
|
||||||
|
|
||||||
|
onSegmentFst :: Integer -> Integer -> Integer -> Bool
|
||||||
|
onSegmentFst p q r = fand (onSegmentLE (fst q) (fst p) (fst r)) (onSegmentGE (fst q) (fst p) (fst r))
|
||||||
|
|
||||||
|
onSegmentSnd :: Integer -> Integer -> Integer -> Bool
|
||||||
|
onSegmentSnd p q r = fand (onSegmentLE (snd q) (snd p) (snd r)) (onSegmentGE (snd q) (snd p) (snd r))
|
||||||
|
|
||||||
|
onSegmentGE :: Integer -> Integer -> Integer -> Bool
|
||||||
|
onSegmentGE a b c = a >= (min b c)
|
||||||
|
|
||||||
|
onSegmentLE :: Integer -> Integer -> Integer -> Bool
|
||||||
|
onSegmentLE a b c = a <= (max b c)
|
||||||
|
|
||||||
|
orientation :: (Integer, Integer) -> (Integer, Integer) -> (Integer, Integer) -> Int
|
||||||
|
orientation p q r
|
||||||
|
| value == 0 = 0 -- Collinear
|
||||||
|
| value > 0 = 1 -- Clockwise
|
||||||
|
| otherwise = -1 -- CounterClockwise
|
||||||
|
where value = ((snd q) - (snd p)) * ((fst r) - (fst q)) - ((fst q) - (fst p)) * ((snd r) - (snd q))
|
||||||
|
|
||||||
|
|
||||||
|
dummyInput = [((0,9),(5,9)),((8,0),(0,8)),((9,4),(3,4)),((2,2),(2,1)),((7,0),(7,4)),((6,4),(2,0)),((0,9),(2,9)),((3,4),(1,4)),((0,0),(8,8)),((5,5),(8,2))]
|
||||||
49
Haskell/2021/05/day5_2.hs
Normal file
49
Haskell/2021/05/day5_2.hs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
fand :: Bool -> Bool -> Bool
|
||||||
|
fand a b
|
||||||
|
| a == b = a
|
||||||
|
| otherwise = False
|
||||||
|
|
||||||
|
betweenxAndy :: Int -> Int -> Int -> Bool
|
||||||
|
betweenxAndy x y n = fand (n >= x) (n <= y)
|
||||||
|
|
||||||
|
between0And1 :: Int -> Bool
|
||||||
|
between0And1 = betweenxAndy 0 1
|
||||||
|
|
||||||
|
crossProduct :: (Integer, Integer) -> (Integer, Integer) -> Integer
|
||||||
|
crossProduct (x1, y1) (x2, y2) = x1*y2 - x2*y1
|
||||||
|
|
||||||
|
dotProduct :: (Integer, Integer) -> (Integer, Integer) -> Integer
|
||||||
|
dotProduct (x1, y1) (x2, y2) = x1*x2 + y1*y2
|
||||||
|
|
||||||
|
pointDifference :: (Integer, Integer) -> (Integer, Integer) -> (Integer, Integer)
|
||||||
|
pointDifference (x1, y1) (x2, y2) = (x1-x2, y1-y2)
|
||||||
|
|
||||||
|
pointSum :: (Integer, Integer) -> (Integer, Integer) -> (Integer, Integer)
|
||||||
|
pointSum (x1, y1) (x2, y2) = (x1+x2, y1+y2)
|
||||||
|
|
||||||
|
overlapNums :: ((Integer,Integer),(Integer,Integer)) -> ((Integer,Integer),(Integer,Integer)) -> Int
|
||||||
|
overlapNums (p, r) (q, s)
|
||||||
|
| collinear =
|
||||||
|
| paraNonInter = 0
|
||||||
|
| intersect = 1
|
||||||
|
| otherwise = 0
|
||||||
|
where bottom = crossProduct r s
|
||||||
|
tTop = crossProduct (pointDifference q p) s
|
||||||
|
top = crossProduct (pointDifference q p) r
|
||||||
|
collinear = fand (bottom == 0) (top == 0)
|
||||||
|
paraNonInter = fand (bottom == 0) (top /= 0)
|
||||||
|
u = top / bottom
|
||||||
|
t = tTop / bottom
|
||||||
|
intersect = fand (bottom /= 0) (fand (between0And1 u) (between0And1 t))
|
||||||
|
|
||||||
|
collineComp :: ((Integer,Integer),(Integer,Integer)) -> ((Integer,Integer),(Integer,Integer)) -> Int
|
||||||
|
collineComp (p, r) (q, s)
|
||||||
|
| fand (t0' < 0) (t1 >= 0) = 0-- overlapping
|
||||||
|
| fand (t0' >= 0) (t1 <= 1) = 0-- overlapping
|
||||||
|
| otherwise = 0 -- Disjoint
|
||||||
|
where t0 = (dotProduct (pointDifference p q) r) / dotProduct r r
|
||||||
|
t1 = (dotProduct (pointDifference (pointSum q s) p) r) / dotProduct r r
|
||||||
|
t0' = if t0 > t1 then t1 else t0
|
||||||
|
t1' = if t0 > t1 then t0 else t1
|
||||||
|
|
||||||
|
collineComp :: ((Integer,Integer),(Integer,Integer)) -> ((Integer,Integer),(Integer,Integer)) -> Int
|
||||||
18
Haskell/2021/06/day6.hs
Normal file
18
Haskell/2021/06/day6.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
fishGrowth :: [Integer] -> [Integer]
|
||||||
|
fishGrowth [] = []
|
||||||
|
fishGrowth (x:xs)
|
||||||
|
| x == 0 = 6 : 8 : fishGrowth xs
|
||||||
|
| otherwise = (x-1) : fishGrowth xs
|
||||||
|
|
||||||
|
runFishGrowth :: [Integer] -> Int -> [Integer]
|
||||||
|
runFishGrowth ls 0 = ls
|
||||||
|
runFishGrowth ls x = runFishGrowth (fishGrowth ls) (x-1)
|
||||||
|
|
||||||
|
fishGrowth' :: [(Int, Integer)] -> Int -> Int -> [(Int, Integer)]
|
||||||
|
fishGrowth' [] new _ = (8,x) : []
|
||||||
|
fishGrowth' (stage,num):xs new reset
|
||||||
|
| stage == 0 = fishGrowth' xs new num
|
||||||
|
| stage == 7 = (6, num + reset) : fishGrowth' xs new 0
|
||||||
|
| otherwise = ((stage-1), num) : fishGrowth' xs new 0
|
||||||
|
|
||||||
|
input = [1,2,1,3,2,1,1,5,1,4,1,2,1,4,3,3,5,1,1,3,5,3,4,5,5,4,3,1,1,4,3,1,5,2,5,2,4,1,1,1,1,1,1,1,4,1,4,4,4,1,4,4,1,4,2,1,1,1,1,3,5,4,3,3,5,4,1,3,1,1,2,1,1,1,4,1,2,5,2,3,1,1,1,2,1,5,1,1,1,4,4,4,1,5,1,2,3,2,2,2,1,1,4,3,1,4,4,2,1,1,5,1,1,1,3,1,2,1,1,1,1,4,5,5,2,3,4,2,1,1,1,2,1,1,5,5,3,5,4,3,1,3,1,1,5,1,1,4,2,1,3,1,1,4,3,1,5,1,1,3,4,2,2,1,1,2,1,1,2,1,3,2,3,1,4,5,1,1,4,3,3,1,1,2,2,1,5,2,1,3,4,5,4,5,5,4,3,1,5,1,1,1,4,4,3,2,5,2,1,4,3,5,1,3,5,1,3,3,1,1,1,2,5,3,1,1,3,1,1,1,2,1,5,1,5,1,3,1,1,5,4,3,3,2,2,1,1,3,4,1,1,1,1,4,1,3,1,5,1,1,3,1,1,1,1,2,2,4,4,4,1,2,5,5,2,2,4,1,1,4,2,1,1,5,1,5,3,5,4,5,3,1,1,1,2,3,1,2,1,1]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue