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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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