From 0fb0967e449440daef757e8d2b827a16c0b50126 Mon Sep 17 00:00:00 2001 From: Jake Date: Fri, 3 Dec 2021 07:58:14 +0000 Subject: [PATCH] Completed day3, task 1 --- Day-3/day3-1.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Day-3/day3-1.py diff --git a/Day-3/day3-1.py b/Day-3/day3-1.py new file mode 100644 index 0000000..0a357c9 --- /dev/null +++ b/Day-3/day3-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 + +# Variables to hold our totals. +gamma = '' +epsilon = '' + +# Read in the input file. Split the file up into an array. +values = [] +with open('input.txt', 'r') as inputFile: + for line in inputFile: + values.append(line.strip('\n')) + +# Add up all the 1 values for each bit, then use the length of the array to calculate the most common bit. +for i in range(0, len(values[0])): + currentBitCount = 0 + for value in values: + currentBitCount = currentBitCount + int(value[i]) + + if((len(values) - currentBitCount) < currentBitCount): + gamma = gamma + '1' + epsilon = epsilon + '0' + elif((len(values) - currentBitCount) > currentBitCount): + gamma = gamma + '0' + epsilon = epsilon + '1' + +# Convert the binary strings to decimal ints. +gammaInt = int(gamma, 2) +epsilonInt = int(epsilon, 2) + +# Print results. +print("Gamma is " + str(gamma) + " (" + str(gammaInt) + ")") +print("Epsilon is " + str(epsilon) + " (" + str(epsilonInt) + ")") +print("Gamma multiplied by Epsilon is: " + str(gammaInt * epsilonInt)) \ No newline at end of file