+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
38c0746
master
1import os 2 3instructions = map (str .split ,open ("input.txt" ).readlines ()) 4 5reg = {chr (i ):0 for i in range (ord ('a' ),ord ('z' )+ 1 )} 6 7freq = 0 8line = 0 9firstTime = True 10soundEnabled = True 11 12 13def fset (x ,y ): 14reg [x ]= int (y ) 15 16 17def fadd (x ,y ): 18reg [x ]+= int (y ) 19 20 21def fmul (x ,y ): 22reg [x ]*= int (y ) 23 24 25def fmod (x ,y ): 26reg [x ]%= int (y ) 27 28 29def fjgz (x ,y ): 30global line 31if int (x )> 0 : 32line += int (y )- 1 33 34 35def frcv (x ,y ): 36global firstTime 37if reg [x ]!= 0 and firstTime : 38firstTime = False 39return True 40 41 42def fsnd (x ,y ): 43global freq ,soundEnabled 44freq = int (x ) 45if soundEnabled : 46os .system ("beep -f" + str (x )) 47 48 49functions = { 50"set" :fset , 51"add" :fadd , 52"mul" :fmul , 53"mod" :fmod , 54"jgz" :fjgz , 55"rcv" :frcv , 56"snd" :fsnd 57} 58 59while line < len (instructions ): 60current = instructions [line ] 61arg1 = current [1 ] 62if 'a' <= arg1 <= 'z' and current [0 ]in ("snd" ,"jgz" ): 63arg1 = reg [arg1 ] 64arg2 = None 65if len (current )> 2 : 66arg2 = current [2 ] 67if 'a' <= arg2 <= 'z' : 68arg2 = reg [arg2 ] 69if functions [current [0 ]](arg1 ,arg2 ): 70break 71line += 1 72 73"Part 1:" ,freq 74 75waiting = [False ,False ] 76reg = {i :0 for i in reg } 77altreg = dict (reg ) 78altreg ['p' ]= 1 79line = 0 80altline = 0 81from collections import deque 82queue = [deque (),deque ()] 83prog1sent = 0 84 85 86def fsend (x ,y ): 87global prog ,prog1sent 88queue [not prog ].append (int (x )) 89prog1sent += (prog == 1 ) 90 91 92def freceive (x ,y ): 93global prog 94waiting [prog ]= x 95 96 97functions ["snd" ]= fsend 98functions ["rcv" ]= freceive 99 100while False in waiting or any (len (i )> 0 for i in queue ): 101for prog in range (2 ): 102if waiting [prog ]: 103if len (queue [prog ]): 104reg [waiting [prog ]]= queue [prog ].popleft () 105waiting [prog ]= False 106else : 107reg ,altreg = altreg ,reg 108line ,altline = altline ,line 109continue 110 111current = instructions [line ] 112arg1 = current [1 ] 113if 'a' <= arg1 <= 'z' and current [0 ]in ("snd" ,"jgz" ): 114arg1 = reg [arg1 ] 115arg2 = None 116if len (current )> 2 : 117arg2 = current [2 ] 118if 'a' <= arg2 <= 'z' : 119arg2 = reg [arg2 ] 120functions [current [0 ]](arg1 ,arg2 ) 121line += 1 122line ,altline = altline ,line 123reg ,altreg = altreg ,reg 124 125"Part 2:" ,prog1sent