Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. Param(
  3.     [string[]]
  4.     $song
  5. )
  6.  
  7. if (-not $song) {
  8.     $song = gc .\day18.txt
  9.     #$song = "set a 1,add a 2,mul a a,mod a 5,snd a,set a 0,rcv a,jgz a -1,set a 1,jgz a -2" -split ','
  10.     #$song = "snd 1,snd 2,snd p,rcv a,rcv b,rcv c,rcv d" -split ','
  11. }
  12.  
  13. class program {
  14.     [hashtable]$register = @{}
  15.     [system.collections.queue]$rcvValues= @{}
  16.     [bool]$waiting = $false
  17.     [bool]$finished = $false
  18.     [int]$position=0
  19. }
  20.  
  21. $actions = $song | % {
  22.     if ($_ -match "(?<instruciton>\w{3}) (?<x>[-\d]+|[\w])( (?<y>[-\d]+|[\w])|)") {
  23.         [pscustomobject]$Matches | select instruciton, x, y
  24.     }
  25. }
  26.  
  27. $programs = [System.Collections.ArrayList]@()
  28. [void]$programs.Add(
  29.     [program]@{
  30.     Register = @{p=0}
  31.     rcvValues= @{}
  32.     waiting = $false
  33.     finished = $false
  34.     position=0
  35. })
  36. [void]$programs.Add([program]@{
  37.     Register = @{p=1}
  38.     rcvValues= @{}
  39.     waiting = $false
  40.     finished = $false
  41.     position=0
  42. })
  43.  
  44. $digit = [regex]"[-\d]+"
  45.  
  46. $currentprogram = 1
  47. $otherprogram = 0
  48. $moretodo = $true
  49.  
  50. $onesend = 0
  51.  
  52. while ( $moretodo ) {
  53.     if ( $programs[0].finished -and $programs[1].finished ){
  54.         $moretodo = $false
  55.         continue
  56.     }
  57.     if ( ($programs[0].waiting -and $programs[0].rcvValues.count -eq 0) -and ($programs[1].waiting -and $programs[1].rcvValues.count -eq 0) ) {
  58.         $moretodo = $false
  59.     } else {
  60.         if ($programs[$currentprogram].waiting -or $programs[$currentprogram].finished){
  61.             if ($programs[$currentprogram].rcvValues.count -gt 0 -and (-not $programs[$currentprogram].finished)){
  62.                 $programs[$currentprogram].waiting = $false
  63.             } else {
  64.                 $currentprogram= ($currentprogram+1) %2
  65.                 $otherprogram= ($otherprogram+1) %2
  66.             }
  67.         } else {
  68.             $thisprog = $programs[$currentprogram]
  69.             $currenta = $actions[$thisprog.position]
  70.             $x = if ($digit.Match( $currenta.x ).Success) { [int]$currenta.x } else { if ($thisprog.register[$currenta.x]) {$thisprog.register[$currenta.x]} else {0} }
  71.             $y = if ($digit.Match( $currenta.y ).Success) { [int]$currenta.y } else { if ($currenta.y) {if ($thisprog.register[$currenta.y]) {$thisprog.register[$currenta.y]} else {0} } }
  72.  
  73.             switch ($currenta.instruciton) {
  74.                 jgz {
  75.                     $addvalue = if ($x -gt 0) { $y } else { 1 }
  76.                     $thisprog.position = $thisprog.position + $addvalue
  77.                 }
  78.                 snd {
  79.                     $programs[$otherprogram].rcvValues.enqueue($x)
  80.                     $thisprog.position = $thisprog.position +1
  81.                     if ($currentprogram -eq 1){
  82.                         $onesend++
  83.                     }
  84.                 }
  85.                 set {
  86.                     $thisprog.register[$currenta.x] = $y
  87.                     $thisprog.position = $thisprog.position +1
  88.                 }
  89.                 add {
  90.                     $thisprog.register[$currenta.x] = $thisprog.register[$currenta.x] + $y
  91.                     $thisprog.position = $thisprog.position +1
  92.                 }
  93.                 mul {
  94.                     $thisprog.register[$currenta.x] = $thisprog.register[$currenta.x] * $y
  95.                     $thisprog.position = $thisprog.position +1
  96.                 }
  97.                 mod {
  98.                     $thisprog.register[$currenta.x] = $thisprog.register[$currenta.x] % $y
  99.                     $thisprog.position = $thisprog.position +1
  100.                 }
  101.                 rcv {
  102.                     if ($programs[$currentprogram].rcvValues.count -gt 0){
  103.                         $value = $thisprog.rcvValues.dequeue()
  104.                         $thisprog.register[$currenta.x] = $value
  105.                         $thisprog.position = $thisprog.position +1
  106.                     } else {
  107.                         $thisprog.waiting = $true
  108.                     }
  109.                 }
  110.             }
  111.            
  112.             if ($thisprog.position -lt 0 -or $thisprog.position -ge $actions.Count ) {
  113.                 $thisprog.finished = $true
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119. $onesend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement