Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.37 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. open(INPUT, "aoc_7_A_input.txt");
  4. @input = <INPUT>;
  5. close(INPUT);
  6.  
  7. #The real deal
  8.  
  9. @allsteps = ('A'..'Z');
  10. $finishline = 26;
  11. $numberofworkers = 5;
  12. $initialworktime = 60;
  13.  
  14. #Description Example
  15.  
  16. #@allsteps = ('A'..'F');
  17. #$finishline = 6;
  18. #$numberofworkers = 2;
  19. #$initialworktime = 0;
  20.  
  21. ## PART 1
  22.  
  23. $usteps = "";
  24. %stepcompleted = ();
  25.  
  26. do {
  27.  
  28. %lockedsteps = ();
  29. foreach $step (@input) {
  30. #Lock all steps whose previous step has not been completed.
  31. if ($step =~ m/^Step ([A-Z]) must be finished before step ([A-Z]) can begin.$/) {
  32. if ($stepcompleted{$1} != 1) {
  33. $lockedsteps{$2} = 1;
  34. }
  35. }
  36. }
  37.  
  38. $nomore = 0;
  39. for ($z = 0; $z < $#allsteps + 1; $z++) {
  40.  
  41. #Find first step whose is neither locked or completed.
  42. if (($lockedsteps{$allsteps[$z]} != 1)&&($stepcompleted{$allsteps[$z]} != 1)&&($nomore == 0)) {
  43. $stepcompleted{$allsteps[$z]} = 1;
  44. $usteps = $usteps . $allsteps[$z];
  45. $nomore = 1; #We only add one step at a time, then we must parse the locks again if any previous step become available.
  46. }
  47. }
  48.  
  49. } until (length($usteps) == $finishline);
  50.  
  51. # PART 2
  52.  
  53. %stepcompleted = ();
  54. $steps = 0;
  55. $totaltime = 0;
  56. @workers = ();
  57. @workduration = ();
  58.  
  59. do {
  60. %lockedsteps = ();
  61. foreach $step (@input) {
  62. #Lock all steps whose previous step has not been completed.
  63. if ($step =~ m/^Step ([A-Z]) must be finished before step ([A-Z]) can begin.$/) {
  64. if ($stepcompleted{$1} != 1) {
  65. $lockedsteps{$2} = 1;
  66. }
  67. }
  68. }
  69.  
  70. for ($z = 0; $z < $#allsteps + 1; $z++) { #Scan for new work
  71.  
  72. #Find a nonlocked noncompleted step
  73. if (($lockedsteps{$allsteps[$z]} != 1)&&($stepcompleted{$allsteps[$z]} != 1)) {
  74. #If we have free workers, start assign process
  75. if ($#workers < ($numberofworkers - 1)) {
  76.  
  77. $alreadyinprogress = 0;
  78.  
  79. foreach $wrk (@workers) {
  80. if ($wrk eq $allsteps[$z]) {
  81. $alreadyinprogress = 1; #Ensure we don't assign a work whose is already in progress by another elf.
  82. }
  83. }
  84.  
  85. if ($alreadyinprogress == 0) {
  86. #Add work to a elf.
  87. push(@workers, $allsteps[$z]);
  88. push(@workduration, $initialworktime + int($z + 1));
  89. #print "$totaltime .. Started work on $allsteps[$z], ". ($initialworktime + int($z + 1)) . " left\n";
  90. }
  91. }
  92. }
  93.  
  94. } #For loop - done scanning for new work
  95.  
  96.  
  97. for ($i = 0; $i < $numberofworkers; $i++) {
  98. if (($workduration[$i] > 0)&&(length($workers[$i]) > 0)) {
  99. $workduration[$i] = $workduration[$i] - 1; #decrease work in progress timer for all elfes that are currently working.
  100. }
  101. }
  102.  
  103. #increase total time worked.
  104. $totaltime++;
  105. #print "$totaltime .. W1(".$workers[0].",".$workduration[0].") W2(".$workers[1].",".$workduration[1].") W3(".$workers[2].",".$workduration[2].") W4(".$workers[3].",".$workduration[3].") W5(".$workers[4].",".$workduration[4].")\n";
  106.  
  107. for ($i = 0; $i < $numberofworkers; $i++) {
  108.  
  109. #If there is an assigned work ($workers[$i] with a length larger than 0) with a time left of 0, then that work is now complete.
  110. if (($workduration[$i] == 0)&&(length($workers[$i]) > 0)) {
  111. #Add step to completed work, so we know which steps that should not be locked during next iteration.
  112. $stepcompleted{$workers[$i]} = 1;
  113. #Increase count of completed steps so we know when we are completely done.
  114. $steps++;
  115. #print "$totaltime .. Finished work on $workers[$i]\n";
  116. splice(@workers, $i, 1); #Delete work from the elf
  117. splice(@workduration, $i, 1);
  118. }
  119. }
  120.  
  121. } until ($steps == $finishline);
  122.  
  123. print "PART 1: ".$usteps."\n";
  124. print "PART 2: ".$totaltime."\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement