From 3d1bbf7d8f051a8b8c8473cac699a91f8e87dfda Mon Sep 17 00:00:00 2001 From: b-idea Date: Sat, 15 Jul 2023 22:40:33 +0200 Subject: code update again --- 2018/aoc2018-d06.py | 140 +++++++++++++++++++++++++++++++++++++ 2018/aoc2018-d07-1.py | 116 ++++++++++++++++++++++++++++++ 2018/aoc2018-d07-2.py | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2018/aoc2018-d08-1.py | 31 ++++++++ 2018/aoc2018-d08-2.py | 51 ++++++++++++++ 5 files changed, 528 insertions(+) create mode 100755 2018/aoc2018-d06.py create mode 100755 2018/aoc2018-d07-1.py create mode 100755 2018/aoc2018-d07-2.py create mode 100755 2018/aoc2018-d08-1.py create mode 100755 2018/aoc2018-d08-2.py diff --git a/2018/aoc2018-d06.py b/2018/aoc2018-d06.py new file mode 100755 index 0000000..c3cf756 --- /dev/null +++ b/2018/aoc2018-d06.py @@ -0,0 +1,140 @@ +print("aoc 2018 day 06"); + +import numpy + + +f = open("input.txt", 'r'); +#f = open("testinput.txt", 'r'); + +myinput = {}; + +for l,line in enumerate(f): + myinput[chr(l+65)] = eval("[" + line + "]"); + print(myinput[chr(l+65)]); + +for p in myinput: + print(p); + +f.close(); + +Bounds = [0,0]; + +for p in myinput.keys(): + if myinput.get(p)[0] > Bounds[0]: + Bounds[0] = myinput.get(p)[0]; + if myinput.get(p)[1] > Bounds[1]: + Bounds[1] = myinput.get(p)[1]; + +Bounds[0] += 1; +Bounds[1] += 1; + +print("max x ", Bounds[0], "max y ", Bounds[1]); + + +class mappoint: + def __init__(self, cX, cY): + self.cords = [cX, cY]; + self.IsBoundary = False; + self.closest_point = []; #[cX, cY]; + self.closest_dist = 65000; + #closest_ID = ""; + def checkdistance(self, md, pname): #x, y, pname): + if md < self.closest_dist: + self.closest_dist = md; + self.closest_point.clear(); + self.closest_point.append(pname); # = #[x,y]; + #self.closest_ID = pname; + elif md == self.closest_dist: + self.closest_point.append(pname); + def sign(self): + s = self.closest_point[0]; + if ( len(self.closest_point) > 1): + s = "."; + return s; + #def setBounds(self, bounds): + #closest_point = [0,0]; + #closest_dist = 0; + +mymap = []; + +for x in range(Bounds[0]): + sublist = []; + for y in range(Bounds[1]): + sublist.append(mappoint(x,y)); + mymap.append(sublist); + +#for x in mymap: +# print(len(x)); + +#for x in mymap: +# print(x); + +for p in myinput.keys(): + for x in range(len(mymap)): + for y in range(len(mymap[x])): + manh_dist = abs(x - myinput[p][0] ) + abs( y - myinput[p][1] ); + mymap[x][y].checkdistance(manh_dist, p); + #if (manh_dist + #print(manh_dist); + +boundset = set(); + +for x in mymap: + for y in x: + print(y.sign(), end=""); + if (y.cords[0] == 0 or y.cords[1] == 0 or y.cords[0] == Bounds[0]-1 or y.cords[1] == Bounds[1]-1): + boundset.add(y.sign()); + print(end="\n"); + + +print("###########################################"); +print(boundset); + +finitepoints = set(); + +for p in myinput.keys(): + if not (p in boundset): + finitepoints.add(p); + + +print("###########################################"); +print(finitepoints); + +maxfp = ","; +maxfpscore = 0; + +for fp in finitepoints: + fpscore = 0; + print(fp); + for x in mymap: + for y in x: + if (y.sign() == fp): + fpscore += 1; + #print(x.count(fp)); + if (fpscore > maxfpscore): + maxfpscore = fpscore; + maxfp = fp; + +print("###"); +#print(maxfp, "\t part 1 =", maxfpscore); + + +###########PART 2 + +part2limit = 10000; +#part2limit = 32; #testinput + +regionsize = 0; + +for x in range(len(mymap)): + for y in range(len(mymap[x])): + localsum = 0; + for p in myinput.keys(): + d = abs(x - myinput[p][0] ) + abs( y - myinput[p][1] ); + localsum += d; + if localsum < part2limit: + regionsize += 1; + + +print("part 1 =", maxfpscore); +print("part 2 =", regionsize); diff --git a/2018/aoc2018-d07-1.py b/2018/aoc2018-d07-1.py new file mode 100755 index 0000000..b734e18 --- /dev/null +++ b/2018/aoc2018-d07-1.py @@ -0,0 +1,116 @@ +#class step: +# def __init__(self, name, unlocks): +# self.name = name; +# self.unlocks = unlocks; + + +def TransInstr (instruction): + return [instruction[5], instruction[36]]; + + +myinput = {}; + +currentfilename = "input.txt"; +#currentfilename = "testinput.txt"; + +f = open(currentfilename, 'r'); + +#list1 = []; +#list2 = {}; +list2 = set(); + +for line in f: + nowinstr = TransInstr(line); + #myinput[nowinstr[1]].append(nowinstr[0]); + list2.add(nowinstr[0]); + +#''' + try: + myinput[nowinstr[1]].append(nowinstr[0]); + #myinput[nowinstr[1]] = nowinstr[0]; + except: + myinput[nowinstr[1]] = []; + myinput[nowinstr[1]].append(nowinstr[0]); +#''' + +print(myinput.keys()); +print("##############"); +print(list2); +print("##############"); + +pr1 = list(myinput.keys()); +pr1.sort(); +pr2 = list(list2); +pr2.sort(); +print(pr1); +print(pr2); +print("##############"); + +print("##############"); +print("finding the first step"); + +CurrentStep = ''; + +AvailableSteps = []; +#AvailableSteps.append(CurrentStep); + +for l in list2: + IsUnique = True; + for k in myinput.keys(): + #print("L ", l, "\tK ",k, "\t",l == k); + if (l == k): + IsUnique = False; + break; + if IsUnique: + #CurrentStep = l; + #break; + AvailableSteps.append(l); + +print("Unique values are ", AvailableSteps); + +print("##############"); +print(myinput); +print("##############"); +part1 = ""; #CurrentStep; + +#list1 = myinput.keys(); +#list1.append(CurrentStep); +#print(list1); + + +while (len(AvailableSteps) != 0): + #input(); + AvailableSteps.sort(); + CurrentStep = AvailableSteps[0]; + part1 += CurrentStep; + print("now ", CurrentStep); + for step in myinput: + #x = myinput[step].index(CurrentStep); + #print(x, "\t",myinput[step]); + #myinput[step].pop(0); + #print(x, "\t",myinput[step]); + try: + #print("try worked"); + myinput[step].pop(myinput[step].index(CurrentStep)); + except: + #print("skip"); + pass; + if (len(myinput[step]) == 0 and part1.find(step) == -1): + AvailableSteps.append(step); + + AvailableSteps.pop(0); + +print("##################################"); + +def RemoveDupsOrder(TheString): + a = ""; + for letter in TheString: + if not (letter in a): + a += letter; + return a; + +print(part1); + +part1 = RemoveDupsOrder(part1); + +print(part1); diff --git a/2018/aoc2018-d07-2.py b/2018/aoc2018-d07-2.py new file mode 100755 index 0000000..2196e47 --- /dev/null +++ b/2018/aoc2018-d07-2.py @@ -0,0 +1,190 @@ +class elf: + #HasWork = False; + #WorkTime = 0; + #WorkStep = ''; + + def WorkAssign(self,s): + AocOffset = 60; #correct value + #AocOffset = 0; #example only + self.WorkStep = s; + self.WorkTime = ord(s) -64 + AocOffset; + self.HasWork = True; + + def WorkReset(self): + self.HasWork = False; + #self.WorkTime = 0; + self.WorkStep = ''; + def DoWork(self): + self.WorkTime += -1; + #if + # return + def __init__(self):#, name, unlocks): + self.HasWork = False; + self.WorkTime = 1; + self.WorkStep = ''; + + def __str__(self): + return f"{self.WorkStep}\t{self.WorkTime}\t{self.HasWork}"; + +def WorkInProgress(elflist): + w = False; + for e in elflist: + w += e.HasWork; + return w; + +def TransInstr (instruction): + return [instruction[5], instruction[36]]; + + +myinput = {}; + +currentfilename = "input.txt"; +numberofworkers = 5; +#currentfilename = "testinput.txt"; #example only +#numberofworkers = 2;#example only + +f = open(currentfilename, 'r'); + +#list1 = []; +#list2 = {}; +list2 = set(); + +for line in f: + nowinstr = TransInstr(line); + #myinput[nowinstr[1]].append(nowinstr[0]); + list2.add(nowinstr[0]); + +#''' + try: + myinput[nowinstr[1]].append(nowinstr[0]); + #myinput[nowinstr[1]] = nowinstr[0]; + except: + myinput[nowinstr[1]] = []; + myinput[nowinstr[1]].append(nowinstr[0]); +#''' + +print(myinput.keys()); +print("##############"); +print(list2); +print("##############"); + +pr1 = list(myinput.keys()); +pr1.sort(); +pr2 = list(list2); +pr2.sort(); +print(pr1); +print(pr2); +print("##############"); + +print("##############"); +print("finding the first step"); + +CurrentStep = ''; + +workers = []; + +for i in range(numberofworkers): + workers.append(elf()); + + +AvailableSteps = []; +#AvailableSteps.append(CurrentStep); + +for l in list2: + IsUnique = True; + for k in myinput.keys(): + #print("L ", l, "\tK ",k, "\t",l == k); + if (l == k): + IsUnique = False; + break; + if IsUnique: + #CurrentStep = l; + #break; + AvailableSteps.append(l); + +print("Unique values are ", AvailableSteps); + +print("##############"); +print(myinput); +print("##############"); +part1 = ""; #CurrentStep; + +#list1 = myinput.keys(); +#list1.append(CurrentStep); +#print(list1); +part2 = -1; +IsWork = False; +print("whileloop begins"); +while (len(AvailableSteps) != 0 or IsWork): + + part2 += 1;#input(); + #IsWork = False; + AvailableSteps.sort(); + #CurrentStep = AvailableSteps[0]; + print(part2, " - steps sorted:",AvailableSteps); + for worker in workers: + #print(worker); + #print(worker.WorkTime); + worker.DoWork(); + if (worker.WorkTime == 0): # and worker.WorkStep != ''): + part1 += worker.WorkStep; + #if (worker.WorkSte + if (worker.WorkStep != ''): + for step in myinput: + try: + myinput[step].pop(myinput[step].index(worker.WorkStep)); + except: + pass; + CurrentStepsCheck = "";# workers[0].WorkStep + workers[1].WorkStep + workers[2].WorkStep + workers[3].WorkStep; + for w in workers: + CurrentStepsCheck += w.WorkStep; + if (len(myinput[step]) == 0 and part1.find(step) == -1 and CurrentStepsCheck.find(step) == -1 ): #and AvailableSteps.find(step) == -1): + AvailableSteps.append(step); + worker.WorkReset(); + #worker.WorkStep() = ''; + #worker.HasWork() = False; + AvailableSteps = list(dict.fromkeys(AvailableSteps)); + + for worker in workers: + if not worker.HasWork: + if (len(AvailableSteps) > 0): + worker.WorkAssign(AvailableSteps[0]); + print("\tWorker begins step", AvailableSteps[0], "at second ", part2); + AvailableSteps.pop(0); + #else: + #worker.DoWork(); + #worker.WorkTime() += -1; + + #part1 += CurrentStep; + #print("now ", CurrentStep); + ''' + for step in myinput: + try: + myinput[step].pop(myinput[step].index(CurrentStep)); + except: + pass; + if (len(myinput[step]) == 0 and part1.find(step) == -1): #and AvailableSteps.find(step) == -1): + AvailableSteps.append(step); + ''' + + IsWork = WorkInProgress(workers); + #AvailableSteps.pop(0); + +print("whileloop finished"); +print("##################################"); + +def RemoveDupsOrder(TheString): + a = ""; + for letter in TheString: + if not (letter in a): + a += letter; + return a; + +print(part1); + +part1 = RemoveDupsOrder(part1); + +print(part1); +print("total time part2 = ", part2); +#207 too low (because I forgot to add 60secs per step, fixed now +#987 correct diff --git a/2018/aoc2018-d08-1.py b/2018/aoc2018-d08-1.py new file mode 100755 index 0000000..2d0110d --- /dev/null +++ b/2018/aoc2018-d08-1.py @@ -0,0 +1,31 @@ +currentfilename = "input.txt"; +#currentfilename = "testinput.txt"; #example only + +f = open(currentfilename, 'r'); +myinput = eval(f.read().replace(" ",",")); +f.close(); + +#for i in myinput: +# print(i); + +def NodeAnalysis(License, index): + NumberOfChilds = License[index[0]]; + index[0] += 1; + NumberofEntries = License[index[0]]; + index[0] += 1; + #print(index, " - this node has ", NumberOfChilds, " and ", NumberofEntries); + for child in range(NumberOfChilds): + NodeAnalysis(License, index); + + for entry in range(NumberofEntries): + index[1] += License[index[0]]; + index[0] += 1; + #print("\tcurrent index ", index[0]); + + +i = [0,0]; + +NodeAnalysis(myinput, i); +print("input length\t", len(myinput)); +print("final index\t", i[0]); +print("part1 answer\t", i[1]); diff --git a/2018/aoc2018-d08-2.py b/2018/aoc2018-d08-2.py new file mode 100755 index 0000000..17ce2cc --- /dev/null +++ b/2018/aoc2018-d08-2.py @@ -0,0 +1,51 @@ +currentfilename = "input.txt"; +#currentfilename = "testinput.txt"; #example only + +f = open(currentfilename, 'r'); +myinput = eval(f.read().replace(" ",",")); +f.close(); + +#for i in myinput: +# print(i); + +def NodeAnalysis(License, index): + NodeVal = 0; + NumberOfChilds = License[index[0]]; + index[0] += 1; + NumberofEntries = License[index[0]]; + index[0] += 1; + print(index, " - this node has ", NumberOfChilds, " and ", NumberofEntries); + ListOfEntries = []; + ListOfChilds = []; + for child in range(NumberOfChilds): + ListOfChilds.append(NodeAnalysis(License, index)); + + + for entry in range(NumberofEntries): + ListOfEntries.append(License[index[0]]); + #index[1] += License[index[0]]; + index[0] += 1; + #print("\tcurrent index ", index[0]); + if(NumberOfChilds == 0): + NodeVal = sum(ListOfEntries); + #for entry in range(NumberofEntries): + # NodeVal += License[index[0]]; + # index[0] += 1; + else: + for e in ListOfEntries: + if (e == 0 or e >NumberOfChilds): + continue; + NodeVal+= ListOfChilds[e-1]; + print("now ", NodeVal); + index.append(NodeVal); + return NodeVal; + + +i = [0,0]; + +part2 = NodeAnalysis(myinput, i); +print("input length\t", len(myinput)); +print("final index\t", i[0]); +print("part1 answer\t", i[1]); +print("part2 answer\t", i[1]); +print("part2 answer\t", i[-1]); -- cgit v1.2.3