@@ -384,11 +384,11 @@ instance Eq Color where
384
384
Red == Yellow = False
385
385
Red == Blue = False
386
386
Yellow == Yellow = True
387
- Yellow == Blue = True
387
+ Yellow == Blue = False
388
388
Yellow == Red = False
389
389
Blue == Blue = True
390
- Blue == Yellow = True
391
- Blue == Red = True
390
+ Blue == Yellow = False
391
+ Blue == Red = False
392
392
393
393
data Direction = Up | Down | PLeft | PRight
394
394
@@ -402,6 +402,13 @@ image = V.fromList
402
402
V. fromList [Yellow , Yellow , Blue ]
403
403
]
404
404
405
+ image1 :: Image
406
+ image1 = V. fromList
407
+ [
408
+ V. fromList [Red , Red ],
409
+ V. fromList [Red , Yellow ]
410
+ ]
411
+
405
412
-- Paint a color in one location
406
413
paint :: Image -> (Int , Int ) -> Color -> Image
407
414
paint vs (i, j) c =
@@ -415,6 +422,7 @@ paint vs (i, j) c =
415
422
-- Find all locations which need to paint
416
423
findArea :: Image -> (Int , Int ) -> [(Int , Int )]
417
424
findArea img (i,j) = uniq (
425
+ (i,j):
418
426
findAreaOnDir img (i,j) boundC Up ++
419
427
findAreaOnDir img (i,j) boundC Down ++
420
428
findAreaOnDir img (i,j) boundC PLeft ++
@@ -429,40 +437,40 @@ uniq (x:xs) buf
429
437
430
438
findAreaOnDir :: Image -> (Int , Int ) -> Color -> Direction -> [(Int , Int )]
431
439
findAreaOnDir img (i,j) c Up
432
- | isInBound img (i,j- 1 ) && selectC == c =
440
+ | isInBoundAndSameColor img (i,j- 1 ) c =
433
441
(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 =
435
443
(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 =
437
445
(i,j+ 1 ): findAreaOnDir img (i,j+ 1 ) c PRight
438
446
| otherwise = []
439
- where selectC = img V. ! i V. ! j
440
447
findAreaOnDir img (i,j) c Down
441
- | isInBound img (i,j- 1 ) && selectC == c =
448
+ | isInBoundAndSameColor img (i,j- 1 ) c =
442
449
(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 =
444
451
(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 =
446
453
(i,j+ 1 ): findAreaOnDir img (i,j+ 1 ) c PRight
447
454
| otherwise = []
448
- where selectC = img V. ! i V. ! j
449
455
findAreaOnDir img (i,j) c PLeft
450
- | isInBound img (i- 1 , j) && selectC == c =
456
+ | isInBoundAndSameColor img (i- 1 ,j) c =
451
457
(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 =
453
459
(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 =
455
461
(i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
456
462
| otherwise = []
457
- where selectC = img V. ! i V. ! j
458
463
findAreaOnDir img (i,j) c PRight
459
- | isInBound img (i- 1 ,j) && selectC == c =
464
+ | isInBoundAndSameColor img (i- 1 ,j) c =
460
465
(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 =
462
467
(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 =
464
469
(i+ 1 ,j): findAreaOnDir img (i+ 1 ,j) c Down
465
470
| otherwise = []
471
+
472
+ isInBoundAndSameColor :: Image -> (Int , Int ) -> Color -> Bool
473
+ isInBoundAndSameColor img (i,j) c = isInBound img (i,j) && selectC == c
466
474
where selectC = img V. ! i V. ! j
467
475
468
476
isInBound :: Image -> (Int , Int ) -> Bool
0 commit comments