19
\$\begingroup\$

Introduction

I have a lot of ASCII rocks. They are built with dashes, pipes, Vs, carets, angle brackets, slashes and spaces. Example:

/--\
|  |
|  |
\--/

I want to erode them, like this:

 /\
/  \
\  /
 \/

All of the corners have become rounder. A more complicated example:

/----\
|     \------\
|            |
|            |
\------------/

 /--\
/    \------\
|            \
\            /
 \----------/

After another erosion, it would become

  /\
 /  \------\
<           \
 \          /
  \--------/

And another:

  /\------\
 <         \
  \        /
   \------/

Challenge

Your challenge is to write a program that can erode an input once. You can assume there is only one rock and you can assume it is one closed loop. The input will only contain the chars /\ -| <> ^V \n and will have trailing spaces to create a rectangle. The program can either get input from STDIN and output to STDOUT or can be a function. At the end of each row is a newline character. The erosion must follow the rules outlined below (Note: in the examples the rock isn't completed, this is for ease of explanation). The output must be in the same format as the input, with the same size as the input. However, trailing spaces may be omitted.

The slashes will spread to pipelines and dashes and move across.

/---
|
|

 /--
/
|

  /-
 /
/

If two slashes merge together, the appropriate character out of <>^V is used.

/-----\
|     |
|     |
|     |
\-----/

 /---\
/     \
|     |
\     /
 \---/

  /-\
 /   \
<     >
 \   /
  \-/

   ^
  / \
 <   >
  \ /
   V

If a part of the rock can merge, it will. Note: if one part can merge but the other can't (i.e, /\ in the second line of the example), then the one that can merge will (see example).

          /\
 /\-^-/\-/  \--
 |
 <
 |
 /
 \
 |
 /
/
\
 \
 |
 |


  /-------/\--
 /
 |
 |
 |
 |
 |
 |
 /
 \
 |
 |
 |

Eventually, all rocks will become nothing.

<>  ^  /\
    V  \/

Test cases

Test 1:

/----\
|     \------\
|            |
|            |
\------------/

 /--\
/    \------\
|            \
\            /
 \----------/

  /\
 /  \------\
<           \
 \          /
  \--------/


  /\------\
 <         \
  \        /
   \------/


   /-----\
  <       \
   \      /
    \----/

    /---\
   <     \
    \    /
     \--/

     /-\
    <   \
     \  /
      \/

      ^
     < \
      \/

Test 2:

/----\
|    |
|    |
|    |
|    |
\----/

 /--\
/    \
|    |
|    |
\    /
 \--/

  /\
 /  \
/    \
\    /
 \  /
  \/

  /\
 /  \
 \  /
  \/

  /\
  \/

Test 3:

          ^    /\
 /\--/\--/ \--/  \-\
 \                 |
 |                 |
 /                 |
 \                 |
 |                 |
 |                 |
 /                 |
<                  |
 \                 |
 |                 |
 |                 |
 /                 |
/                  |
\                  |
 \-----------------/


  /-------^----/\-\
 /                 \
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 <                 |
 |                 |
 |                 |
 |                 |
 |                 |
 /                 |
 \                 /
  \---------------/


   /-------------\
  /               \
 /                 \
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 \                 /
  \               /
   \-------------/

    /-----------\
   /             \
  /               \
 /                 \
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 \                 /
  \               /
   \             /
    \-----------/

     /---------\
    /           \
   /             \
  /               \
 /                 \
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 |                 |
 \                 /
  \               /
   \             /
    \           /
     \---------/

      /-------\
     /         \
    /           \
   /             \
  /               \
 /                 \
 |                 |
 |                 |
 |                 |
 |                 |
 \                 /
  \               /
   \             /
    \           /
     \         /
      \-------/

       /-----\
      /       \
     /         \
    /           \
   /             \
  /               \
 /                 \
 |                 |
 |                 |
 \                 /
  \               /
   \             /
    \           /
     \         /
      \       /
       \-----/

        /---\
       /     \
      /       \
     /         \
    /           \
   /             \
  /               \
 /                 \
 \                 /
  \               /
   \             /
    \           /
     \         /
      \       /
       \     /
        \---/

         /-\
        /   \
       /     \
      /       \
     /         \
    /           \
   /             \
  /               \
  \               /
   \             /
    \           /
     \         /
      \       /
       \     /
        \   /
         \-/

          ^
         / \
        /   \
       /     \
      /       \
     /         \
    /           \
   /             \
   \             /
    \           /
     \         /
      \       /
       \     /
        \   /
         \ /
          V

          ^
         / \
        /   \
       /     \
      /       \
     /         \
    /           \
    \           /
     \         /
      \       /
       \     /
        \   /
         \ /
          V

          ^
         / \
        /   \
       /     \
      /       \
     /         \
     \         /
      \       /
       \     /
        \   /
         \ /
          V

          ^
         / \
        /   \
       /     \
      /       \
      \       /
       \     /
        \   /
         \ /
          V

          ^
         / \
        /   \
       /     \
       \     /
        \   /
         \ /
          V

          ^
         / \
        /   \
        \   /
         \ /
          V

          ^
         / \
         \ /
          V

          ^
          V

Scoring

This is , so the program with the smallest number of bytes wins!

Standard loopholes are not allowed.

\$\endgroup\$
  • 1
    \$\begingroup\$ What is the output for "\x20\x20\x20\x20\x20\x20/\\\n/-\\\x20\x20/\x20\x20\\-\\\n|\x20\x20\\/\x20\x20\x20\x20\x20|\n\\---------/\n" \$\endgroup\$ – tsh Apr 3 '18 at 12:05
  • \$\begingroup\$ I don't really think the rules make sense. Rocks erode differently if they're rotated 45 degrees. But anyway. \$\endgroup\$ – user202729 Apr 11 '18 at 14:51
  • \$\begingroup\$ Retina would work really well for this I think. \$\endgroup\$ – Jerry Jeremiah Jun 25 '18 at 3:38
1
\$\begingroup\$

Rust

I gave up on this after a while, the instructions kind of seem ambiguous. However i did get some shrinky-looking rocks (see output). I also am pretty sure this needs to be done in a 2 dimensional space rather than in a 1 dimensional string as i tried here. My main "issue" is that I transform the given input by marking space as 'inside' or 'outside' the rock, unfortunately at this point it cannot distinguish between inside and the top/bottom edges.

enum State {
 OutsideLeft,CrossLI,Inside,CrossRI,OutsideRight,
}    

const INPUT: &str = r#"
/----\
|     \------\
|            |
|            |
\------------/
"#;


fn precycle( mut data:Vec<char> ) -> Vec<char> {
    let mut state = State::OutsideLeft;
    let mut curcol = 0;
    let mut curline:Vec<char> = Vec::new();
    let mut lastline:Vec<char> = Vec::new();
    for mut i in 0..data.len() {
        if data[i]=='\n' { 
            lastline = curline.to_vec();
            lastline.push(' ');
            curline.clear();
            curline.push(' ');
            curcol = 0;
        } else {
            curline.push(data[i]);
            curcol += 1;
        }
        let n = curcol%lastline.len();
        let n2 = lastline.len()-n;
        //println!("[{}]",lastline.iter().collect::<String>());
        //println!("[{}]",curline.iter().collect::<String>());
        //println!("({}{})({}{})>",data[i],state,lastline[n],curline[n]);
        //print!("{}{}>",data[i],state);
        match state {
        State::OutsideLeft  =>  {
            if      data[i]=='/' { state = State::CrossLI; }
            else if data[i]=='\\' { state = State::CrossLI; }
            else if data[i]=='|' { state = State::CrossLI; }
            else if data[i]=='<' { state = State::CrossLI; }
            if      data[i]==' ' { }
        } State::CrossLI => {
            if      data[i]=='/' { state = State::CrossRI; }
            else if data[i]=='\\' { state = State::CrossRI; }
            else if data[i]=='|' { state = State::CrossRI; }
            else if data[i]=='>' { state = State::CrossRI; }
            if data[i]==' ' { data[i]='-'; }
            if data[i]=='-' { state = State::Inside; }
        } State::Inside => {
            if      data[i]=='/' { state = State::CrossRI; }
            else if data[i]=='\\' { state = State::CrossRI; }
            else if data[i]=='|' { state = State::CrossRI; }
            else if data[i]=='>' { state = State::CrossRI; }
            if      data[i]==' ' { data[i] = '-'; }
        } State::CrossRI => {
            if  data[i]==' ' { state = State::OutsideRight; }
            if  data[i]=='\n' { state = State::OutsideLeft; }
        } State::OutsideRight => {
            if  data[i]==' ' { }
            if  data[i]=='\n' { state = State::OutsideLeft; }
        } } 
        match state {
        State::CrossLI => {
            if curline[n]=='|' && lastline[n]=='.' { data[i]='9'; curline[n]='9'; }
        }
        State::CrossRI => {
            if curline[n]=='|' && lastline[n]=='.' { data[i]='8'; curline[n]='8'; }
            if curline[n]=='.' && lastline[n]=='8' { data[i-n-n2+1]='>'; }
            if curline[n]=='.' && lastline[n]=='|' { data[i-n-n2+1]='6'; }
        }
        State::OutsideLeft => {
            if curline[n]=='.' && lastline[n]=='|' { data[i-n-n2+1]='7'; curline[n] = '7'; }
            if curline[n]=='.' && lastline[n]=='9' { data[i-n-n2+1]='<'; }
        }
        _ => { }
        }
        //print!("{} ",state);

    }
    for mut i in 0..data.len() {
        if data[i]=='9' {data[i]='/'}
        if data[i]=='8' {data[i]='\\'}
        if data[i]=='7' {data[i]='\\'}
        if data[i]=='6' {data[i]='/'}
    }
    data
}

fn cycle( data:String ) -> String {
    data
    .replace(r#"^"#,r#"."#)
    .replace(r#"V"#,r#"."#)
    .replace(r#"/-\"#,r#".^."#)
    .replace(r#"\-/"#,r#".V."#)
    .replace(r#"-/\-"#,r#"----"#)
    .replace(r#"-\/-"#,r#"----"#)
    .replace(r#"/\-"#,r#"/--"#)
    .replace(r#"-/\"#,r#"--\"#)
    .replace(r#"\/-"#,r#"\--"#)
    .replace(r#"-\/"#,r#"--/"#)
    .replace(r#"./\."#,r#"...."#)
    .replace(r#".\/."#,r#"...."#)
    .replace(r#"/\"#,r#".."#)
    .replace(r#"\/"#,r#".."#)
    .replace(r#"-\"#,r#"\."#)
    .replace(r#"-/"#,r#"/."#)
    .replace(r#"/-"#,r#"./"#)
    .replace(r#"\-"#,r#".\"#)
    .replace(r#"->"#,r#">."#)
    .replace(r#"<-"#,r#".<"#)
    .replace(r#".-"#,r#"--"#)
    .replace(r#"-."#,r#"--"#)
}

fn main() {
    let mut inputv: Vec<char> = INPUT.chars().collect();
    let mut input0: Vec<char> = inputv;
    for j in 1..18 {
        let mut inputa = precycle( input0 );
        let pdata = inputa.iter().collect::<String>().replace("."," ");
        println!("{}",pdata);
        let mut input2 = cycle( inputa.iter().collect::<String>() );
        input0 = input2.chars().collect();
    }
}

output

/----\
|-----\------\
|------------|
|------------|
\------------/


 /--\ 
/----\------\ 
|------------\
\------------/
 \----------/ 


  /\  
 /--\------\  
<-----------\ 
 \----------/ 
  \--------/  



  /\------\   
 <---------\  
  \--------/  
   \------/   



   /-----\    
  <-------\   
   \------/   
    \----/    



    /---\     
   <-----\    
    \----/    
     \--/     



     /-\      
    <---\     
     \--/     
      \/      



      ^       
     <-\      
      \/      





     <\       






     <\       






     <\       






     <\       






     <\       






     <\       






     <\       






     <\       






     <\       
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.