diff options
author | b-idea <test@test.com> | 2023-07-15 21:11:33 +0200 |
---|---|---|
committer | b-idea <test@test.com> | 2023-07-15 21:11:33 +0200 |
commit | 762cbd65915bbb14e05c4f1e6a868fc5e4047f8b (patch) | |
tree | 7ed95eb391300f660d98cdd3a1053abe452ccac7 | |
parent | 711b4271603fce61330b33c67c172e0c580960b8 (diff) |
filling in few days of 2018
-rw-r--r-- | 2018/aoc18-d01-2.py | 2 | ||||
-rw-r--r-- | 2018/aoc18-d03.py | 77 | ||||
-rw-r--r-- | 2018/aoc18-d04-1.py | 123 | ||||
-rw-r--r-- | 2018/aoc18-d05-1.py | 60 | ||||
-rw-r--r-- | 2018/aoc18-d05-2.py | 72 |
5 files changed, 333 insertions, 1 deletions
diff --git a/2018/aoc18-d01-2.py b/2018/aoc18-d01-2.py index 3462b14..ee8bab2 100644 --- a/2018/aoc18-d01-2.py +++ b/2018/aoc18-d01-2.py @@ -1,4 +1,4 @@ - +print(); f = open("input"); part1 = 0; diff --git a/2018/aoc18-d03.py b/2018/aoc18-d03.py new file mode 100644 index 0000000..c304576 --- /dev/null +++ b/2018/aoc18-d03.py @@ -0,0 +1,77 @@ +import numpy as np + +f = open("input"); + +GRIDSIZE = 1000; + +cloth = np.full((GRIDSIZE,GRIDSIZE),0, dtype=int); + + +part1 = np.count_nonzero(cloth); +print(part1); + +for i in f: + for l in range(len(i)): + if i[l] == "@": + claim_id = int(i[1:(l)]); + l_at = l+1; + if i[l] == ",": + claim_l = int(i[l_at:(l)]); + l_at = l+1; + if i[l] == ":": + claim_t = int(i[l_at:(l)]); + l_at = l+1; + if i[l] == "x": + claim_w = int(i[l_at:(l)]); + #l_at = l+1; + claim_h = int(i[l+1:]); + + for x in range(claim_l, claim_l+claim_w, 1): + for y in range(claim_t, claim_t+claim_h, 1): + cloth[y,x] +=1; + +part1 = np.count_nonzero(cloth); +#print(part1); +part1 = 0; +for i in range(GRIDSIZE): + for j in range(GRIDSIZE): + part1 += 1*(cloth[i,j] > 1); + + +print("part1", part1); + + +f = open("input"); +for i in f: + #print(i); + for l in range(len(i)): + #print(l, i[l]); + if i[l] == "@": + claim_id = int(i[1:(l)]); + l_at = l+1; + if i[l] == ",": + claim_l = int(i[l_at:(l)]); + l_at = l+1; + if i[l] == ":": + claim_t = int(i[l_at:(l)]); + l_at = l+1; + if i[l] == "x": + claim_w = int(i[l_at:(l)]); + #l_at = l+1; + claim_h = int(i[l+1:]); + + #checkclaim = "#" + str(claim_id) +" @ "+str(claim_l)+","+str(claim_t)+": "+str(claim_w)+"x"+str(claim_h); + #print("",i, checkclaim); + #print(); + is_overlap = False; + for x in range(claim_l, claim_l+claim_w, 1): + for y in range(claim_t, claim_t+claim_h, 1): + if (cloth[y,x] > 1): + is_overlap = True; + + if not is_overlap: + part2 = claim_id; + break; + +print("part2", part2); + diff --git a/2018/aoc18-d04-1.py b/2018/aoc18-d04-1.py new file mode 100644 index 0000000..6f28be5 --- /dev/null +++ b/2018/aoc18-d04-1.py @@ -0,0 +1,123 @@ +import numpy as np + + +def datecompact(log): + d = log[1:5] + log[6:8] + log[9:11] + log[12:14] + log[15:17]; + return int(d); + +def action(log): + if (log[19] == "f"): + return 1; + elif (log[19] == "w"): + return 2; + else: + #return 3; + return int(log[24:].strip("\n # begins shift")); + return (log[24:].strip("\n # begins shift")); + #return int(log[24:].strip("#beginshft\n")); + +f = open("input"); +#f = open("testinput"); + +myinput = []; +guards = {}; + +for i in f: + #print(datecompact(i)); + myinput.append([datecompact(i),action(i)]); + if (action(i) != 1 and action(i) != 2 ): + try: + guards[action(i)] = 0; + except: + True; + + +for i in range(len(myinput)): + #print(i[0], " -- ", i[1]); + for j in range(len(myinput)): + if (myinput[i][0] < myinput[j][0]): + temp = myinput[i]; + myinput[i] = myinput[j]; + myinput[j] = temp; + + +#print(guards); + +#for i in range(len(myinput) -1): + #if (myinput[i][0] > myinput[i+1][0]): + #print("fuck"); +#for i in myinput: +# print(i[0], " -- ", i[1]); + +x1 = 0; + + +for l in myinput: + if l[1] == 2: + guards[g] += l[0] - x1 ; + elif l[1] == 1: + x1 = l[0]; + else: + g = l[1]; + + +tmax = 0; +bestg = ""; + +for g in guards.keys(): + print(g); + #''' + if (guards[g] >tmax): + tmax = guards[g]; + bestg = g; + #''' + +print("best guard ",bestg," - ", tmax); +#part1 = int(bestg)*tmax; +#print("part1", part1); + +#269357 too high +#256332 too high + + + +is_bestg = False; + +#timestat = np.array(60); +#timestat.fill(0); +timestat = np.full(60,0, dtype=int); + +#bestg = int(bestg); + +for l in myinput: + print("iterating", l[0], " ", l[1], " ", l[0]%100); + + if (is_bestg and (l[1] == 1 or l[1] == 2)): + print("yup"); + if l[1] == 1: + t1 = l[0]; + elif l[1] == 2: + t2 = l[0]; + for t in range(t1%100,t2%100,1): + timestat[t] += 1; + elif l[1] == bestg: + is_bestg = True; + elif l[1] != bestg: + print("not bestg"); + is_bestg = False; + +''' + guards[g] += l[0] - x1 ; + elif l[1] == 1: + x1 = l[0]; + else: + g = l[1]; +''' + +worsttime = (np.argmax(timestat)); + +part1 = int(bestg)*worsttime; + + +print("best guard ",bestg," - ", worsttime); +print("part1", part1); diff --git a/2018/aoc18-d05-1.py b/2018/aoc18-d05-1.py new file mode 100644 index 0000000..b1d8ebc --- /dev/null +++ b/2018/aoc18-d05-1.py @@ -0,0 +1,60 @@ + +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))); + + +myinput = f.read(); +print(myinput); +print(len(myinput)); + +is_poly = True; +''' +while is_poly: + is_poly = False; + for x in range(1,len(myinput)): + x1 = myinput[x-1]; + x2 = myinput[x]; + if polymer_d(x1,x2): + #print((x1+x2)); + #myinput = myinput.replace((x1+x2),""); + myinput = myinput[ + is_poly = True; + break; + #is_poly = False; +''' + +while is_poly: + is_poly = False; + sub = ""; + for x in range(2,len(myinput)): + + x1 = myinput[x-2]; + x2 = myinput[x-1]; + if polymer_d(x1,x2): + myinput = sub + myinput[x:]; + is_poly = True; + break; + else: + sub += x1; +#print(myinput); +print(len(myinput)); +print(myinput); +myinput.rstrip("\n"); + +print("part 1", len(myinput) -1); +#9687 too high +#minus 1 helped lol, not sure why diff --git a/2018/aoc18-d05-2.py b/2018/aoc18-d05-2.py new file mode 100644 index 0000000..be01aa0 --- /dev/null +++ b/2018/aoc18-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 |