-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountisland.ek
72 lines (57 loc) · 1.86 KB
/
countisland.ek
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// EPITECH PROJECT, 2024
// Count Island
// File description:
// A Count Island implementation in EK
//
// To run this program, use:
// ekc countisland.ek -o countisland
// ./countisland < countisland.input
//
import std
import list
struct state {
world: list, // list of list of char
current: int, // current island number
}
fn (state) incIsland : state
= state { state world, state current + 1 }
fn (w) with [(x)][(y)] to (value)
= w with [x] to (w[x] with [y] to value)
fn (l) with [(x)] to (value)
= l take x <> [value] <> l drop (x + 1)
fn (state) setting (x) (y) : state
= state { state world with [x][y] to (state current)
, state current
}
fn (state) propagate (x) (y) : state
= state setting x y
propagateIfPossible (x - 1) y
propagateIfPossible (x + 1) y
propagateIfPossible x (y - 1)
propagateIfPossible x (y + 1)
fn (state) propagateIfPossible (x) (y) : state
= if x < 0 || y < 0 || x >= state world length || y >= state world[x] length
then state
else if state world[x][y] == "X"
then state propagate x y
else state
fn (state) countIslandsFrom (x) : state
= if x >= state world length
then state
else state propagateLine x 0 countIslandsFrom (x + 1)
fn (state) propagateLine (x) (y) : state
= if y >= state world[x] length
then state
else (if state world[x][y] == "X" then state propagate x y incIsland else state)
propagateLine x (y + 1)
fn (w) countIslands : list
= state { w, 0 } countIslandsFrom 0 world
fn readWorld : list
= readWorldN readLine toInt
fn readWorldN (lineCount) : list
= if lineCount == 0
then []
else readLine stringToCharList cons readWorldN (lineCount - 1)
fn (s) dropFirst = 1 - s
fn main = print readWorld countIslands map (\ line = "\n" ++ line map (_ toString) concat) concat dropFirst