@@ -384,11 +384,11 @@ instance Eq Color where
384384 Red == Yellow = False
385385 Red == Blue = False
386386 Yellow == Yellow = True
387- Yellow == Blue = True
387+ Yellow == Blue = False
388388 Yellow == Red = False
389389 Blue == Blue = True
390- Blue == Yellow = True
391- Blue == Red = True
390+ Blue == Yellow = False
391+ Blue == Red = False
392392
393393data Direction = Up | Down | PLeft | PRight
394394
@@ -402,6 +402,13 @@ image = V.fromList
402402 V. fromList [Yellow , Yellow , Blue ]
403403 ]
404404
405+ image1 :: Image
406+ image1 = V. fromList
407+ [
408+ V. fromList [Red , Red ],
409+ V. fromList [Red , Yellow ]
410+ ]
411+
405412-- Paint a color in one location
406413paint :: Image -> (Int , Int ) -> Color -> Image
407414paint vs (i, j) c =
@@ -415,6 +422,7 @@ paint vs (i, j) c =
415422-- Find all locations which need to paint
416423findArea :: Image -> (Int , Int ) -> [(Int , Int )]
417424findArea img (i,j) = uniq (
425+ (i,j):
418426 findAreaOnDir img (i,j) boundC Up ++
419427 findAreaOnDir img (i,j) boundC Down ++
420428 findAreaOnDir img (i,j) boundC PLeft ++
@@ -429,40 +437,40 @@ uniq (x:xs) buf
429437
430438findAreaOnDir :: Image -> (Int , Int ) -> Color -> Direction -> [(Int , Int )]
431439findAreaOnDir img (i,j) c Up
432- | isInBound img (i,j- 1 ) && selectC == c =
440+ | isInBoundAndSameColor img (i,j- 1 ) c =
433441 (i,j- 1 ): findAreaOnDir img (i,j- 1 ) c PLeft
434- | isInBound img (i- 1 ,j) && selectC == c =
442+ | isInBoundAndSameColor img (i- 1 ,j) c =
435443 (i- 1 ,j): findAreaOnDir img (i- 1 ,j) c Up
436- | isInBound img (i,j+ 1 ) && selectC == c =
444+ | isInBoundAndSameColor img (i,j+ 1 ) c =
437445 (i,j+ 1 ): findAreaOnDir img (i,j+ 1 ) c PRight
438446 | otherwise = []
439- where selectC = img V. ! i V. ! j
440447findAreaOnDir img (i,j) c Down
441- | isInBound img (i,j- 1 ) && selectC == c =
448+ | isInBoundAndSameColor img (i,j- 1 ) c =
442449 (i,j- 1 ): findAreaOnDir img (i,j- 1 ) c PLeft
443- | isInBound img (i+ 1 , j) && selectC == c =
450+ | isInBoundAndSameColor img (i+ 1 ,j) c =
444451 (i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
445- | isInBound img (i,j+ 1 ) && selectC == c =
452+ | isInBoundAndSameColor img (i,j+ 1 ) c =
446453 (i,j+ 1 ): findAreaOnDir img (i,j+ 1 ) c PRight
447454 | otherwise = []
448- where selectC = img V. ! i V. ! j
449455findAreaOnDir img (i,j) c PLeft
450- | isInBound img (i- 1 , j) && selectC == c =
456+ | isInBoundAndSameColor img (i- 1 ,j) c =
451457 (i- 1 ,j): findAreaOnDir img (i- 1 ,j) c Up
452- | isInBound img (i,j- 1 ) && selectC == c =
458+ | isInBoundAndSameColor img (i,j- 1 ) c =
453459 (i,j- 1 ): findAreaOnDir img (i,j- 1 ) c PLeft
454- | isInBound img (i+ 1 ,j) && selectC == c =
460+ | isInBoundAndSameColor img (i+ 1 ,j) c =
455461 (i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
456462 | otherwise = []
457- where selectC = img V. ! i V. ! j
458463findAreaOnDir img (i,j) c PRight
459- | isInBound img (i- 1 ,j) && selectC == c =
464+ | isInBoundAndSameColor img (i- 1 ,j) c =
460465 (i- 1 ,j): findAreaOnDir img (i- 1 ,j) c Up
461- | isInBound img (i,j+ 1 ) && selectC == c =
466+ | isInBoundAndSameColor img (i,j+ 1 ) c =
462467 (i,j+ 1 ): findAreaOnDir img (i,j+ 1 ) c PRight
463- | isInBound img (i+ 1 ,j) && selectC == c =
468+ | isInBoundAndSameColor img (i+ 1 ,j) c =
464469 (i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
465470 | otherwise = []
471+
472+ isInBoundAndSameColor :: Image -> (Int , Int ) -> Color -> Bool
473+ isInBoundAndSameColor img (i,j) c = isInBound img (i,j) && selectC == c
466474 where selectC = img V. ! i V. ! j
467475
468476isInBound :: Image -> (Int , Int ) -> Bool
0 commit comments