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