From 1777f5bb246c251d8d737aa589802ea62bedf9eb Mon Sep 17 00:00:00 2001 From: kwout Date: Sun, 4 Sep 2022 11:26:12 -0400 Subject: [PATCH] added vim swap to gitignore completed day 3 --- .gitignore | 2 ++ 2021/day3/3b.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 .gitignore create mode 100644 2021/day3/3b.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3819313 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +*.swo 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)) +