1 | PROBLEM ArtificialAnt
|
---|
2 |
|
---|
3 | CODE <<
|
---|
4 | public struct Point {
|
---|
5 | public int x; public int y;
|
---|
6 | public Point(int x, int y) {
|
---|
7 | this.x = x;
|
---|
8 | this.y = y;
|
---|
9 | }
|
---|
10 | };
|
---|
11 |
|
---|
12 | class World {
|
---|
13 |
|
---|
14 | private static string[] LosAltosTrail = new string[] {
|
---|
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 | " # ",
|
---|
73 | " # ",
|
---|
74 | " # ",
|
---|
75 | " # ",
|
---|
76 | " # ",
|
---|
77 | " # ",
|
---|
78 | " # ",
|
---|
79 | " # ",
|
---|
80 | " # "
|
---|
81 | };
|
---|
82 | private char[][] world = LosAltosTrail.Select(l=>l.ToArray()).ToArray();
|
---|
83 | public int TotalFood = LosAltosTrail.Select(l=>l.Where(c=>c=='#').Count()).Sum();
|
---|
84 | public int Width { get { return 100; } }
|
---|
85 | public int Height { get { return 100; } }
|
---|
86 |
|
---|
87 | public void Reset() {
|
---|
88 | world = LosAltosTrail.Select(l=>l.ToArray()).ToArray();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public bool FoodAt(Point p) {
|
---|
92 | if(p.x < 0 || p.y < 0) return false;
|
---|
93 | if(world.Length <= p.y) return false;
|
---|
94 | if(world[p.y].Length <= p.x) return false;
|
---|
95 | return world[p.y][p.x] == '#';
|
---|
96 | }
|
---|
97 | public void RemoveFoodAt(Point p) {
|
---|
98 | if(world.Length < p.y) return;
|
---|
99 | if(world[p.y].Length < p.x) return;
|
---|
100 | world[p.y][p.x] = ' ';
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | enum Direction {West, East, North, South};
|
---|
105 |
|
---|
106 | class AntState {
|
---|
107 | public Point Position { get; set;}
|
---|
108 | public int MaxSteps { get; set;}
|
---|
109 | public Direction Direction { get; set; }
|
---|
110 | public World World { get; set; }
|
---|
111 | public int Steps { get; set; }
|
---|
112 | public int FoodEaten { get; set; }
|
---|
113 |
|
---|
114 | public AntState() {
|
---|
115 | Steps = 0;
|
---|
116 | FoodEaten = 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void TurnLeft() {
|
---|
120 | Steps++;
|
---|
121 | switch(Direction) {
|
---|
122 | case Direction.West: Direction = Direction.South; break;
|
---|
123 | case Direction.South: Direction = Direction.East; break;
|
---|
124 | case Direction.East: Direction = Direction.North; break;
|
---|
125 | case Direction.North: Direction = Direction.West; break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | public void TurnRight() {
|
---|
129 | Steps++;
|
---|
130 | switch(Direction) {
|
---|
131 | case Direction.West: Direction = Direction.North; break;
|
---|
132 | case Direction.South: Direction = Direction.West; break;
|
---|
133 | case Direction.East: Direction = Direction.South; break;
|
---|
134 | case Direction.North: Direction = Direction.East; break;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | public void MoveForward() {
|
---|
138 | Position = NextPosition;
|
---|
139 | Steps++;
|
---|
140 | if(World.FoodAt(Position)) {
|
---|
141 | World.RemoveFoodAt(Position);
|
---|
142 | FoodEaten++;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | public Point NextPosition {
|
---|
146 | get {
|
---|
147 | int x, y;
|
---|
148 | switch(Direction) {
|
---|
149 | case Direction.West:
|
---|
150 | x = (Position.x + World.Width - 1) % World.Width;
|
---|
151 | y = Position.y;
|
---|
152 | return new Point(x, y);
|
---|
153 | case Direction.South:
|
---|
154 | x = Position.x;
|
---|
155 | y = (Position.y + World.Height + 1) % World.Height;
|
---|
156 | return new Point(x, y);
|
---|
157 | case Direction.East:
|
---|
158 | x = (Position.x + World.Width + 1) % World.Width;
|
---|
159 | y = Position.y;
|
---|
160 | return new Point(x, y);
|
---|
161 | case Direction.North:
|
---|
162 | x = Position.x;
|
---|
163 | y = (Position.y + World.Height - 1) % World.Height;
|
---|
164 | return new Point(x, y);
|
---|
165 | }
|
---|
166 | return new Point(-1,-1);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | World CreateWorld() {
|
---|
172 | var w = new World();
|
---|
173 | Console.WriteLine("Total food: "+w.TotalFood);
|
---|
174 | return w;
|
---|
175 | }
|
---|
176 |
|
---|
177 | World world;
|
---|
178 | >>
|
---|
179 | INIT <<
|
---|
180 | world = CreateWorld();
|
---|
181 | >>
|
---|
182 |
|
---|
183 | NONTERMINALS
|
---|
184 | Ant<<World world, AntState state>>.
|
---|
185 | IfFoodAhead<<World world, AntState state>>.
|
---|
186 | Prog2<<World world, AntState state>>.
|
---|
187 | Prog3<<World world, AntState state>>.
|
---|
188 |
|
---|
189 | TERMINALS
|
---|
190 | Left. Right. Move.
|
---|
191 |
|
---|
192 | RULES
|
---|
193 | Ant<<World world, AntState state>> =
|
---|
194 | IfFoodAhead<<world, state>>
|
---|
195 | | Prog2<<world, state>>
|
---|
196 | | Prog3<<world, state>>
|
---|
197 | | Left SEM << state.TurnLeft(); >>
|
---|
198 | | Right SEM << state.TurnRight(); >>
|
---|
199 | | Move SEM << state.MoveForward(); >>
|
---|
200 | .
|
---|
201 |
|
---|
202 | IfFoodAhead<<World world, AntState state>> =
|
---|
203 | SEM <<
|
---|
204 | if(world.FoodAt(state.NextPosition)) {
|
---|
205 | >>
|
---|
206 |
|
---|
207 | Ant<<world, state>> SEM << } else { >>
|
---|
208 | Ant<<world, state>> SEM << } >>
|
---|
209 | .
|
---|
210 |
|
---|
211 | Prog2<<World world, AntState state>> =
|
---|
212 | Ant<<world, state>> Ant<<world, state>>
|
---|
213 | .
|
---|
214 |
|
---|
215 | Prog3<<World world, AntState state>> =
|
---|
216 | Ant<<world, state>> Ant<<world, state>> Ant<<world, state>>
|
---|
217 | .
|
---|
218 |
|
---|
219 |
|
---|
220 | MAXIMIZE /* could also use the keyword MINIMIZE here */
|
---|
221 | <<
|
---|
222 | world.Reset();
|
---|
223 | var state = new AntState() {Position = new Point(0,0), MaxSteps=400, Direction=Direction.East, World = world};
|
---|
224 | while(state.Steps < state.MaxSteps) {
|
---|
225 | Ant(world, state);
|
---|
226 | }
|
---|
227 | return (double)state.FoodEaten;
|
---|
228 | >>
|
---|
229 | END ArtificialAnt.
|
---|