AdventOfCode/Haskell/2021/05/day5.hs

63 lines
13 KiB
Haskell
Raw Normal View History

horzLineToDiagram :: Integer -> Integer -> Integer -> [(Integer, Integer)]
horzLineToDiagram row current finish
| current == finish = (row, finish) : []
| current > finish = horzLineToDiagram row finish current
| otherwise = (row, current) : horzLineToDiagram row (current + 1) finish
vertLineToDiagram :: Integer -> Integer -> Integer -> [(Integer, Integer)]
vertLineToDiagram column current finish
| current == finish = (finish, column) : []
| current > finish = vertLineToDiagram column finish current
| otherwise = (current, column) : vertLineToDiagram column (current + 1) finish
upRightLineToDiagram :: Integer -> Integer -> Integer -> Integer -> [(Integer, Integer)]
upRightLineToDiagram curRow curColumn finishRow finishColumn
| curRow == finishRow = (finishRow, finishColumn) : []
| curRow > finishRow = upRightLineToDiagram finishColumn finishRow curRow curColumn
| otherwise = (curRow, curColumn) : upRightLineToDiagram (curRow + 1) (curColumn + 1) finishRow finishColumn
downRightLineToDiagram :: Integer -> Integer -> Integer -> Integer -> [(Integer, Integer)]
downRightLineToDiagram curRow curColumn finishRow finishColumn
| curRow == finishRow = (finishRow, finishColumn) : []
| curColumn > finishColumn = downRightLineToDiagram finishColumn finishRow curRow curColumn
| otherwise = (curRow, curColumn) : downRightLineToDiagram (curRow - 1) (curColumn + 1) finishRow finishColumn
calculateDiagram :: [((Integer,Integer),(Integer,Integer))] -> [(Integer, Integer)]
calculateDiagram [] = []
calculateDiagram ls
| x1 == x2 = horzLineToDiagram x1 y1 y2 ++ calculateDiagram (tail ls)
| y1 == y2 = vertLineToDiagram y1 x1 x2 ++ calculateDiagram (tail ls)
| diagUpRight (head ls) = upRightLineToDiagram x1 y1 x2 y2 ++ calculateDiagram (tail ls)
| diagDownRight (head ls) = downRightLineToDiagram x1 y1 x2 y2 ++ calculateDiagram (tail ls)
| otherwise = calculateDiagram (tail ls)
where ((x1,y1),(x2,y2)) = head ls
diagUpRight :: ((Integer,Integer),(Integer,Integer)) -> Bool
diagUpRight ((x1,y1), (x2,y2))
| x1-x2 == y1-y2 = True
| otherwise = False
diagDownRight :: ((Integer,Integer),(Integer,Integer)) -> Bool
diagDownRight ((x1,y1), (x2,y2))
| abs (x1-x2) == abs (y1-y2) = True
| otherwise = False
instances :: (Eq a) => a -> [a] -> Int
instances _ [] = 0
instances x (y:ys)
| x == y = 1 + (instances x ys)
| otherwise = instances x ys
numOccurances :: [(Integer,Integer)] -> Int
numOccurances [] = 0
numOccurances ls
| occurances >= 2 = 1 + numOccurances newLs
| otherwise = numOccurances newLs
where item = head ls
occurances = instances item ls
newLs = [x | x <- ls, x /= item]
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))]
input = [((427,523),(427,790)),((94,639),(94,951)),((757,371),(465,663)),((503,935),(503,148)),((655,565),(655,951)),((167,754),(710,211)),((634,433),(245,433)),((449,889),(449,509)),((152,753),(207,808)),((301,90),(301,982)),((566,405),(482,405)),((758,741),(847,652)),((342,686),(921,107)),((741,317),(741,533)),((976,917),(976,664)),((273,272),(273,899)),((326,616),(326,478)),((483,906),(391,814)),((20,165),(20,349)),((860,533),(860,137)),((846,834),(846,610)),((155,923),(241,923)),((989,984),(17,12)),((711,681),(82,52)),((312,788),(312,281)),((319,746),(892,746)),((568,555),(757,555)),((659,450),(940,450)),((870,260),(870,487)),((110,794),(604,794)),((509,141),(509,99)),((29,68),(29,713)),((340,688),(820,688)),((428,46),(902,520)),((539,525),(539,210)),((181,822),(806,822)),((551,338),(551,79)),((894,542),(894,151)),((700,625),(700,60)),((143,736),(143,770)),((810,975),(54,219)),((44,373),(44,514)),((849,794),(120,794)),((347,690),(97,440)),((10,625),(57,625)),((541,202),(799,202)),((375,491),(809,925)),((271,474),(271,905)),((980,825),(980,908)),((600,751),(624,751)),((120,978),(958,140)),((262,916),(262,794)),((399,350),(399,666)),((623,270),(158,735)),((585,792),(585,381)),((168,34),(168,88)),((256,806),(566,806)),((196,397),(326,397)),((583,677),(172,677)),((45,242),(311,508)),((530,469),(433,566)),((46,101),(877,932)),((607,655),(485,777)),((773,672),(461,360)),((340,30),(659,30)),((846,952),(846,917)),((457,700),(125,368)),((571,656),(561,656)),((251,30),(576,30)),((79,197),(79,87)),((518,51),(518,904)),((916,802),(181,67)),((23,626),(485,626)),((956,106),(956,811)),((849,384),(743,278)),((893,153),(893,608)),((17,989),(933,73)),((701,119),(701,704)),((494,580),(494,821)),((605,740),(605,363)),((823,58),(823,166)),((250,363),(566,363)),((418,178),(783,178)),((273,82),(273,115)),((545,489),(656,489)),((468,671),(966,671)),((376,744),(178,942)),((62,728),(589,201)),((588,150),(467,29)),((202,664),(751,115)),((519,547),(916,150)),((803,483),(380,60)),((599,459),(922,459)),((162,680),(162,385)),((823,94),(18,899)),((802,734),(415,347)),((115,964),(115,935)),((749,595),(749,770)),((229,64),(582,64)),((103,496),(551,48)),((137,581),(707,11)),((501,563),(895,957)),((59,222),(620,222)),((874,249),(874,938)),((201,927),(927,201)),((630,619),(655,619)),((666,331),(413,78)),((517,650),(425,558)),((803,256),(803,598)),((303,460),(606,763)),((124,975),(967,132)),((867,528),(867,363)),((239,140),(239,211)),((975,779),(364,168)),((971,49),(531,49)),((408,932),(143,932)),((927,663),(392,128)),((497,246),(497,389)),((849,935),(15,101)),((582,517),(543,517)),((410,85),(237,85)),((292,316),(841,316)),((753,708),(753,38)),((266,328),(267,327)),((789,980),(16,207)),((514,963),(514,180)),((865,532),(687,710)),((544,620),(956,208)),((132,287),(557,287)),((268,710),(268,684)),((234,746),(234,688)),((51,906),(51,911)),((191,159),(428,159)),((834,956),(834,565)),((916,242),(230,928)),((227,789),(227,689)),((206,767),(705,268)),((291,403),(578,403)),((35,890),(956,890)),((185,618),(402,401)),((989,858),(284,858)),((391,284),(391,74)),((717,158),(670,111)),((188,146),(785,743)),((602,696),(602,367)),((975,972),(975,317)),((146,301),(146,205)),((328,215),(861,215)),((15,653),(990,653)),((48,101),(48,34)),((929,547),(506,970)),((901,519),(670,519)),((801,560),(19,560)),((953,829),(876,829)),((856,317),(856,787)),((618,171),(86,703)),((823,622),(369,168)),((209,291),(846,928)),((944,601),(944,544)),((678,223),(987,223)),((893,143),(54,982)),((30,740),(244,740)),((974,974),(15,15)),((497,557),(63,557)),((846,193),(846,964)),((83,119),(946,982)),((864,179),(864,387)),((583,312),(77,818)),((612,154),(612,641)),((487,136),(487,938)),((502,611),(215,898)),((648,978),(648,885)),((373,372),(318,372)),((582,988),(168,574)),((453,261),(899,261)),((467,578),(33,578)),((876,138),(683,331)),((708,248),(132,824)),((686,56),(686,211)),((687,964),(687,485)),((626,76),(626,222)),((630,176),(630,485)),((968,608),(968,382)),((943,69),(943,856)),((173,344),(173,902)),((193,918),(326,918)),((811,748),(379,316)),((484,941),(113,570)),((277,635),(156,514)),((770,5