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 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 2018/aoc2018-d06.py (limited to '2018/aoc2018-d06.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); -- cgit v1.2.3