From d7fef5244c43526e4f1102f816130f55cf20e8a7 Mon Sep 17 00:00:00 2001 From: ihaveslowbrain Date: Fri, 20 Oct 2023 21:32:41 +0000 Subject: renamed few files --- 2018/aoc2018-d05-2.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 2018/aoc2018-d05-2.py (limited to '2018/aoc2018-d05-2.py') diff --git a/2018/aoc2018-d05-2.py b/2018/aoc2018-d05-2.py new file mode 100644 index 0000000..be01aa0 --- /dev/null +++ b/2018/aoc2018-d05-2.py @@ -0,0 +1,72 @@ + +f = open("input"); +#f = open("testinput"); + +''' +test1 = ord("a") - ord("A"); +test2 = ord("b") - ord("B"); +test3 = ord("c") - ord("C"); + +print(test1, " ",test2, " ",test3, " ") + +''' + +cdiff = 32; + +def polymer_d(a,b): + return (cdiff == abs(ord(a) - ord(b))); +''' +def polymer_strip(inp): + is_poly = True; + while is_poly: + is_poly = False; + sub = ""; + for x in range(2,len(inp)): + + x1 = inp[x-2]; + x2 = inp[x-1]; + if polymer_d(x1,x2): + inp = sub + inp[x:]; + is_poly = True; + break; + else: + sub += x1; + return inp; +''' +def polymer_strip(inp): + is_poly = True; + while is_poly: + is_poly = False; + for x in range(1,len(inp)): + x1 = inp[x-1]; + x2 = inp[x]; + if polymer_d(x1,x2): + #print((x1+x2)); + inp = inp.replace((x1+x2),""); + #myinput = myinput[ + is_poly = True; + break; + return inp; + #is_poly = False; + +myinput = f.read(); + +#print("A", ord("A"), "Z", ord("Z")); + +inp_max = 999999; +inp_c = ""; + +for c in range(ord("A"),ord("Z")+1,1): + #print(chr(c)); + subinput = myinput; + subinput = subinput.replace(chr(c),""); + subinput = subinput.replace(chr(c+cdiff),""); + subinput = polymer_strip(subinput); + if (len(subinput) < inp_max): + inp_max = len(subinput); + inp_c = c; + +print("part 2 ",inp_max -1); + +#again, needed minus 1 on answer +#not sure why is that but it works -- cgit v1.2.3