Part 1 wasn't so hard, I basically just followed the description.
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };my($depth)=<>=~/\d+/g;my($tx,$ty)=<>=~/(\d+),(\d+)/;my@cave;sub level{my($x,$y)=@_;($cave[$x][$y]+$depth)%20_183;}$cave[0][0]=0;$cave[$tx][$ty]=0;formy$x(0..$tx){$cave[$x][0]//=$x*16_807;formy$y(1..$ty){$cave[$x][$y]//=$x==0?$y*48_271:level($x-1,$y)*level($x,$y-1);}}my$s=0;formy$y(0..$ty){formy$x(0..$tx){my$l=level($x,$y)%3;print+('.','=','|')[$l];$s+=$l;}print"\n";}say$s;
Part 2, on the other hand, was quite a challenge. I implemented kind of a priority queue (not the real one, but I knew there wouldn't be almost any gaps between the priorities). I also memoized the geologic index and level functions to speed it up, but it still took a bit over 1 minute.
Part 1 wasn't so hard, I basically just followed the description.
Part 2, on the other hand, was quite a challenge. I implemented kind of a priority queue (not the real one, but I knew there wouldn't be almost any gaps between the priorities). I also memoized the geologic index and level functions to speed it up, but it still took a bit over 1 minute.