BookmarkSubscribeRSS Feed
tc
Lapis Lazuli | Level 10 tc
Lapis Lazuli | Level 10

Merry Xmas to all and to all a good night!Merry Xmas to all and to all a good night!

 

Packages all wrapped, so figured I'd take a crack at the final Advent of Code problem. SAS solution below - check out Reddit for Day 25 solutions in other programming languages. Merry Xmas!

 

* "Sea Cucumber" - Day 25 - Advent of Code 2021
  See https://adventofcode.com/2021/day/25 for problem statement;
  
data _null_;
array cell{137,139} $1. _temporary_;        * Holds map data (137 rows by 139 columns); 
array newcell{137,139} $1. _temporary_;     * Updates first applied to copy of map data ; 

do r=1 to dim(cell,1);                      * Read Day 25 data into array;
  infile '~/day25data.txt';
  input;
  do c=1 to dim(cell,2);
    cell(r,c)=substr(_infile_,c,1);
  end;
end; 

do loop=1 to 2000;                           * Stop after 2,000 tries, just in case an infinite loop; 
  moved=0;                                   * Count of how many sea cucumbers moved;
                                             * Copy original array to array that will be updated;
  do r=1 to dim(cell,1); do c=1 to dim(cell,2); newcell(r,c)=cell(r,c); end; end; 

  do r=1 to dim(cell,1);                     * Update horizontal moves;
    do c=1 to dim(cell,2);
      if cell(r,c)='>' then do;              * Horizonal movement sea cucumber?;
        nc=ifn(c<dim(cell,2),c+1,1);         * Move if next cell unoccupied;
        if cell(r,nc)='.' then do; newcell(r,nc)='>'; newcell(r,c)='.'; moved+1; end;
        end;
    end;   
  end;   
                                             * Copy updated array back to original array; 
  do r=1 to dim(cell,1); do c=1 to dim(cell,2); cell(r,c)=newcell(r,c); end; end;
  
  do c=1 to dim(cell,2);                     * Update vertical moves;
    do r=1 to dim(cell,1);
      if cell(r,c)='v' then do;              * Vertical movement sea cucumber?;
        nr=ifn(r<dim(cell,1),r+1,1);         * Move if next cell unoccupied;
        if cell(nr,c)='.' then do; newcell(nr,c)='v'; newcell(r,c)='.'; moved+1; end;
        end;
    end;   
  end;                                         
                                             * Copy updated array back to original array;
  do r=1 to dim(cell,1); do c=1 to dim(cell,2); cell(r,c)=newcell(r,c); end; end;

  put loop= moved=;                          * Report iteration #, # of sea cucumbers moved;
  if moved=0 then stop;                      * Stop if no moves (iteration # is answer);
end;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 0 replies
  • 831 views
  • 3 likes
  • 1 in conversation