From 2df9ab79138db9ae1b9f8465299ee80e892e3f64 Mon Sep 17 00:00:00 2001 From: Jake Date: Fri, 3 Dec 2021 08:47:35 +0000 Subject: [PATCH] Completed day 3, task 2 --- Day-3/day3-2.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Day-3/day3-2.py diff --git a/Day-3/day3-2.py b/Day-3/day3-2.py new file mode 100644 index 0000000..9cf76b6 --- /dev/null +++ b/Day-3/day3-2.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 + +# Variables to hold our totals. +keptNumbers = [] +co2 = 0 +o2 = 0 + +# Read in the input file. Split the file up into an array. +rawValues = [] +with open('input.txt', 'r') as inputFile: + for line in inputFile: + rawValues.append(line.strip('\n')) + +# Loop through twice, collecting the opposite set of results each time. +for i in range(0,2): + values = rawValues + mostPopular=0 + + # Determine the most popular bit. + for bit in range(0, len(values[0])): + currentBitCount = 0 + for value in values: + currentBitCount = currentBitCount + int(value[bit]) + + # Keep values which contain the most popular bit. + if((len(values) - currentBitCount) < currentBitCount): + mostPopular = 1 + for value in values: + if(value[bit] == str(mostPopular) and i == 1): + keptNumbers.append(value) + elif(value[bit] != str(mostPopular) and i == 0): + keptNumbers.append(value) + + elif((len(values) - currentBitCount) > currentBitCount): + mostPopular = 0 + for value in values: + if(value[bit] == str(mostPopular) and i == 1): + keptNumbers.append(value) + elif(value[bit] != str(mostPopular) and i == 0): + keptNumbers.append(value) + + elif((len(values) - currentBitCount) == currentBitCount): + for value in values: + if(value[bit] == str(i)): + keptNumbers.append(value) + + values = keptNumbers + keptNumbers = [] + + # Break if we only have one value left. + if(len(values) == 1): + break + + # Put the value in the right variable. + if(i == 0): + co2 = values[0] + elif(i == 1): + o2 = values[0] + +# Convert the binary numbers to ints. +o2Int = int(o2, 2) +co2Int = int(co2, 2) + +# Print results. +print("O2 Generator reading is " + str(o2) + " (" + str(o2Int) + ")") +print("CO2 Scrubber reading is " + str(co2) + " (" + str(co2Int) + ")") +print("The readings multipied together is " + str(o2Int * co2Int)) \ No newline at end of file