From 67b268be1a7b9f4bba197b6351bf8d28efca695c Mon Sep 17 00:00:00 2001 From: kwout Date: Sun, 4 Sep 2022 11:18:44 -0400 Subject: [PATCH] completed day 3 --- 2021/day3/.3b.py.swp | Bin 0 -> 12288 bytes 2021/day3/3b.py | 37 +++++++++++++++++++++++++++++++++++++ 2021/day3/input.txt.sample | 12 ++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 2021/day3/.3b.py.swp create mode 100644 2021/day3/3b.py create mode 100644 2021/day3/input.txt.sample diff --git a/2021/day3/.3b.py.swp b/2021/day3/.3b.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..6975fbad9c5854df12418d4e4e5824105bf1f4be GIT binary patch literal 12288 zcmeI1&ui2`6vt=vRJ2z70UoCnX`*d*Hw7ux6+{t=2p+1Jx`*s$cAagQgzTiX2klAx z6TEn}coV!9K|xUb0iq|p`8Rm*pa;K`WLuk-w9t!%m-6XuCiC9B`Mk`bv{k!3dxK9q zF2i+{v1hMOwC{eq&sG|YEi8qJHsd>7f#}LmIYHR=g0iU((u7sdUldAL-I`mk`rh(X zb*kk=%VmM|j0}*0y%|V!XL5R&O}X`&NpyN_lpjBKYi~IznhcNuGC&5%02v?yWPl8i zfqi8_M-{e$iVx&fpUFQfh0pw!KF9zWAOmE843GgbKnBPF86X2>fDDj@xTm=>|^_>F0Q0rao~vKfw#|4$wsg$N(8217v^UYyZ zvbns$vu2r1=OwN`F%2EqXorc?{NnajsxF5{u0oA+gU#vlBq%6Laj8+7BabhNN1gMM zcfy!Uw2*IkjPx>H9}gG3Hu~92o0P424ox$+X2*l=zDr>f{(}JwgXeXWjp;SkUE`e+3YV77nKtL literal 0 HcmV?d00001 diff --git a/2021/day3/3b.py b/2021/day3/3b.py new file mode 100644 index 0000000..0826221 --- /dev/null +++ b/2021/day3/3b.py @@ -0,0 +1,37 @@ +lines = open("input.txt", 'r').read().splitlines() +leng = len(lines) +bits = len(lines[0]) + +def fun(bol): + targetCandidates = list(range(0,leng)) + rating = 0 + target = True + for i in range(0, bits): + count = 0 + vacate = [] + for j in targetCandidates: + if len(targetCandidates) == 1: + o2 = int(lines[j], 2) + break + elif i != 0 and bool(int(lines[j][i-1])) != target : + vacate.append(j) + elif bool(int(lines[j][i])): + count += 1 + targetCandidates = [i for i in targetCandidates if i not in vacate] + if count >= len(targetCandidates)/2 and bol: + target = True + elif count < len(targetCandidates)/2 and not bol: + target = True + else: + target = False + if len(targetCandidates) == 2: + if bool(int(lines[targetCandidates[0]][i])) == target: + return int(lines[targetCandidates[0]], 2) + else: + return int(lines[targetCandidates[1]], 2) + elif len(targetCandidates) == 1: + return int(lines[targetCandidates[0]], 2) + + +print(fun(True)*fun(False)) + diff --git a/2021/day3/input.txt.sample b/2021/day3/input.txt.sample new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/2021/day3/input.txt.sample @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010