Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare
- {
- record State
- {
- seq<string> lo;
- uint x;
- uint y;
- string code;
- }
- }
- function is_valid(State s, uint nx, uint ny) : bool
- {
- if(ny>=seqlen(s.lo)) { return false; }
- if(nx>=strlen(s.lo[ny])) { return false; }
- return ' ' != s.lo[ny][nx];
- }
- function step(mutable State s, char ch)
- {
- uint nx, ny = s.x, s.y;
- if('R'==ch)
- {
- nx = nx + 1u;
- }
- elif('L'==ch)
- {
- nx = nx - 1u;
- }
- elif('D'==ch)
- {
- ny = ny + 1u;
- }
- elif('U'==ch)
- {
- ny = ny - 1u;
- }
- if(is_valid(s, nx, ny))
- {
- s.x, s.y = nx, ny;
- }
- }
- function append(mutable State s)
- {
- s.code = s.code + tos(s.lo[s.y][s.x]);
- }
- impure function part_1_and_2(mutable State s1, mutable State s2)
- {
- file f = fopen("input", 'r');
- string ln;
- for(ln=freadln(f);!empty(ln);ln=freadln(f))
- {
- foreach(ch in ln)
- {
- step(s1, ch);
- step(s2, ch);
- }
- append(s1);
- append(s2);
- }
- exception(IOException)
- {
- // skip
- }
- }
- main
- {
- State s1 = State
- {
- .x = 1u;
- .y = 1u;
- .lo = seq
- {
- "123",
- "456",
- "789"
- };
- };
- State s2 = State
- {
- .x = 0u;
- .y = 2u;
- .lo = seq
- {
- " 1 ",
- " 234 ",
- "56789",
- " ABC ",
- " D "
- };
- };
- part_1_and_2(s1, s2);
- write(s1.code + "\n");
- write(s2.code + "\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment