From 9289b7558157d411d678cce9eb1756c61eabc4c1 Mon Sep 17 00:00:00 2001 From: Jake Charman Date: Wed, 30 Nov 2022 21:05:44 +0000 Subject: [PATCH] Directory Structure for 2022 --- 2021/Day-1/day1-1.py | 28 ++++++++++++++ 2021/Day-1/day1-2.py | 33 ++++++++++++++++ 2021/Day-10/day10-1.py | 34 +++++++++++++++++ 2021/Day-10/day10-2.py | 43 +++++++++++++++++++++ 2021/Day-2/day2-1.py | 24 ++++++++++++ 2021/Day-2/day2-2.py | 26 +++++++++++++ 2021/Day-3/day3-1.py | 33 ++++++++++++++++ 2021/Day-3/day3-2.py | 67 ++++++++++++++++++++++++++++++++ 2021/Day-4/day4-1.py | 78 ++++++++++++++++++++++++++++++++++++++ 2021/Day-4/day4-2.py | 86 ++++++++++++++++++++++++++++++++++++++++++ 2021/Day-5/day5-1.py | 68 +++++++++++++++++++++++++++++++++ 2021/Day-5/day5-2.py | 79 ++++++++++++++++++++++++++++++++++++++ 2021/Day-6/day6-1.py | 21 +++++++++++ 2021/Day-6/day6-2.py | 38 +++++++++++++++++++ 2021/Day-7/day7-1.py | 20 ++++++++++ 2021/Day-7/day7-2.py | 21 +++++++++++ 2021/Day-8/day8-1.py | 12 ++++++ 2021/Day-8/day8-2.py | 64 +++++++++++++++++++++++++++++++ 2021/Day-9/day9-1.py | 31 +++++++++++++++ 19 files changed, 806 insertions(+) create mode 100644 2021/Day-1/day1-1.py create mode 100644 2021/Day-1/day1-2.py create mode 100644 2021/Day-10/day10-1.py create mode 100644 2021/Day-10/day10-2.py create mode 100644 2021/Day-2/day2-1.py create mode 100644 2021/Day-2/day2-2.py create mode 100644 2021/Day-3/day3-1.py create mode 100644 2021/Day-3/day3-2.py create mode 100644 2021/Day-4/day4-1.py create mode 100644 2021/Day-4/day4-2.py create mode 100644 2021/Day-5/day5-1.py create mode 100644 2021/Day-5/day5-2.py create mode 100644 2021/Day-6/day6-1.py create mode 100644 2021/Day-6/day6-2.py create mode 100644 2021/Day-7/day7-1.py create mode 100644 2021/Day-7/day7-2.py create mode 100644 2021/Day-8/day8-1.py create mode 100644 2021/Day-8/day8-2.py create mode 100644 2021/Day-9/day9-1.py diff --git a/2021/Day-1/day1-1.py b/2021/Day-1/day1-1.py new file mode 100644 index 0000000..7ac6a64 --- /dev/null +++ b/2021/Day-1/day1-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 + +# Variables to hold our totals. +increased = 0 +decreased = 0 + +# Read in the input file and make it an array. +with open('input.txt', 'r') as inputFile: + values = [] + for line in inputFile: + values.append(int(line)) + +# Loop through the array skipping the first value. +for v in range(0, len(values)): + if(v == 0): + continue + + # Check each value against the one before it. + if(values[v - 1] < values[v]): + increased+=1 + elif(values[v - 1] > values[v]): + decreased+=1 + else: + pass + +# Print the output. +print("Decreased: " + str(decreased)) +print("Increased: " + str(increased)) \ No newline at end of file diff --git a/2021/Day-1/day1-2.py b/2021/Day-1/day1-2.py new file mode 100644 index 0000000..0309acd --- /dev/null +++ b/2021/Day-1/day1-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 + +# Variables to hold our totals. +increased = 0 +decreased = 0 + +# Read in the input file and make it an array. +with open('input.txt', 'r') as inputFile: + rawValues = [] + for line in inputFile: + rawValues.append(int(line)) + +# Sum up numbers in 3's and add them to a new array. +values = [] +for i in range(0, len(rawValues) - 2): + values.append(rawValues[i] + rawValues[i+1] + rawValues[i+2]) + +# Loop through the array skipping the first value. +for v in range(0, len(values)): + if(v == 0): + continue + + # Check each value against the one before it. + if(values[v - 1] < values[v]): + increased+=1 + elif(values[v - 1] > values[v]): + decreased+=1 + else: + pass + +# Print the output. +print("Decreased: " + str(decreased)) +print("Increased: " + str(increased)) \ No newline at end of file diff --git a/2021/Day-10/day10-1.py b/2021/Day-10/day10-1.py new file mode 100644 index 0000000..ae62c21 --- /dev/null +++ b/2021/Day-10/day10-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +totalScore = 0 + +closers = { + '(':')', + '{':'}', + '[':']', + '<':'>' +} + +points = { + ')':3, + ']':57, + '}':1197, + '>':25137 +} + +with open('/home/jake/Documents/AoC-2021/Day-10/input.txt') as inputFile: + for line in inputFile: + line = line.strip('\n') + i = 0 + expecting = [] + i = 0 + for char in line: + if(char in closers): + expecting.insert(0,closers[char]) + else: + if(expecting[i] != char): + totalScore += points[char] + break + else: + del expecting[i] +print("Our total score was: " + str(totalScore)) \ No newline at end of file diff --git a/2021/Day-10/day10-2.py b/2021/Day-10/day10-2.py new file mode 100644 index 0000000..cd97d31 --- /dev/null +++ b/2021/Day-10/day10-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/python3 + +from statistics import median + +lineScores = [] + +closers = { + '(':')', + '{':'}', + '[':']', + '<':'>' +} + +points = { + ')':1, + ']':2, + '}':3, + '>':4 +} + +with open('/home/jake/Documents/AoC-2021/Day-10/input.txt') as inputFile: + for line in inputFile: + line = line.strip('\n') + expecting = [] + invalid = False + for char in line: + if(char in closers): + expecting.insert(0,closers[char]) + else: + if(expecting[0] != char): + invalid = True + break + else: + del expecting[0] + if(invalid): + continue + else: + totalScore = 0 + for e in expecting: + totalScore*=5 + totalScore+=points[e] + lineScores.append(totalScore) +print("The middle score is: " + str(median(lineScores))) \ No newline at end of file diff --git a/2021/Day-2/day2-1.py b/2021/Day-2/day2-1.py new file mode 100644 index 0000000..c11806b --- /dev/null +++ b/2021/Day-2/day2-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 + +# Variables to hold our position. +x = 0 +z = 0 + +# Read in the input file. Split each line on the space. +with open('input.txt', 'r') as inputFile: + values = [] + for line in inputFile: + splitLine = line.split() + + # Check the first word of each line to decide what to do. + if splitLine[0] == "forward": + x = x + int(splitLine[1]) + elif splitLine [0] == "down": + z = z + int(splitLine[1]) + elif splitLine [0] == "up": + z = z - int(splitLine[1]) + +# Print our final values. +print("X: " + str(x)) +print("Z: " + str(z)) +print("X * Z = " + str(x*z)) \ No newline at end of file diff --git a/2021/Day-2/day2-2.py b/2021/Day-2/day2-2.py new file mode 100644 index 0000000..f3688b1 --- /dev/null +++ b/2021/Day-2/day2-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +# Variables to hold our position. +x = 0 +z = 0 +aim = 0 + +# Read in the input file. Split each line on the space. +with open('input.txt', 'r') as inputFile: + values = [] + for line in inputFile: + splitLine = line.split() + + # Check the first word of each line to decide what to do. + if splitLine[0] == "forward": + x = x + int(splitLine[1]) + z = z + (aim * int(splitLine[1])) + elif splitLine [0] == "down": + aim = aim + int(splitLine[1]) + elif splitLine [0] == "up": + aim = aim - int(splitLine[1]) + +# Print our final values. +print("X: " + str(x)) +print("Z: " + str(z)) +print("X * Z = " + str(x*z)) \ No newline at end of file diff --git a/2021/Day-3/day3-1.py b/2021/Day-3/day3-1.py new file mode 100644 index 0000000..0a357c9 --- /dev/null +++ b/2021/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 diff --git a/2021/Day-3/day3-2.py b/2021/Day-3/day3-2.py new file mode 100644 index 0000000..9cf76b6 --- /dev/null +++ b/2021/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 diff --git a/2021/Day-4/day4-1.py b/2021/Day-4/day4-1.py new file mode 100644 index 0000000..3710507 --- /dev/null +++ b/2021/Day-4/day4-1.py @@ -0,0 +1,78 @@ +#!/usr/bin/python3 + +numbersDrawn = [] +boards = [] + +# Function to check a given board for completed columns +def checkColumns(board): + match = False + for i in range(0,len(board[0])): + for line in board: + if(line[i] == '*'): + match = True + continue + else: + match = False + break + if(match): + return True + else: + continue + return False + +# Function to check a given board for completed rows. +def checkRows(board): + for line in board: + match = False + for num in line: + if(num == '*'): + match = True + continue + else: + match = False + break + if(match): + return True + else: + continue + return False + +# Open the file. Put the numbers to be drawn in one array and the boards in another. +with open('/home/jake/Documents/AoC-2021/Day-4/input.txt', 'r') as inputFile: + numbersDrawn = inputFile.readline().split(',') + boardsFromFile = inputFile.readlines()[1:] + + currentBoard = [] + for line in boardsFromFile: + currentLine = [] + + if(line == '\n'): + boards.append(currentBoard) + currentBoard = [] + continue + + currentLine = line.split() + currentBoard.append(currentLine) + boards.append(currentBoard) + +# Mark off the numbers drawn on the boards. +for i in range(0, len(numbersDrawn)): + for board in boards: + for line in board: + for num in range(len(line)): + if(line[num] == numbersDrawn[i]): + line[num] = '*' + + # Check for a winning board + if(checkRows(board) or checkColumns(board)): + print("Found winning board: " + str(board)) + total = 0 + for line in board: + for num in line: + if(num != '*'): + total = total+int(num) + score = total * int(numbersDrawn[i]) + print("Total score of winning board is: " + str(score)) + exit() + else: + continue \ No newline at end of file diff --git a/2021/Day-4/day4-2.py b/2021/Day-4/day4-2.py new file mode 100644 index 0000000..7aa4083 --- /dev/null +++ b/2021/Day-4/day4-2.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3 + +numbersDrawn = [] +boards = [] +winningBoards = [] + +# Function to check a given board for completed columns +def checkColumns(board): + match = False + for i in range(0,len(board[0])): + for line in board: + if(line[i] == '*'): + match = True + continue + else: + match = False + break + if(match): + return True + else: + continue + return False + +# Function to check a given board for completed rows. +def checkRows(board): + for line in board: + match = False + for num in line: + if(num == '*'): + match = True + continue + else: + match = False + break + if(match): + return True + else: + continue + return False + +# Open the file. Put the numbers to be drawn in one array and the boards in another. +with open('/home/jake/Documents/AoC-2021/Day-4/input.txt', 'r') as inputFile: + numbersDrawn = inputFile.readline().split(',') + boardsFromFile = inputFile.readlines()[1:] + + currentBoard = [] + for line in boardsFromFile: + currentLine = [] + + if(line == '\n'): + boards.append(currentBoard) + currentBoard = [] + continue + + currentLine = line.split() + currentBoard.append(currentLine) + boards.append(currentBoard) + +# Mark off the numbers drawn on the boards. +for i in range(0, len(numbersDrawn)): + for board in boards: + for line in board: + for num in range(len(line)): + if(line[num] == numbersDrawn[i]): + line[num] = '*' + + # Check for a winning board + if(checkRows(board) or checkColumns(board)): + if(board in winningBoards): + continue + else: + winningBoards.append(board) + if(len(winningBoards) < len(boards)): + continue + else: + print("Found final winning board: " + str(board)) + total = 0 + for line in board: + for num in line: + if(num != '*'): + total = total+int(num) + score = total * int(numbersDrawn[i]) + print("Total score of final winning board is: " + str(score)) + exit() + else: + continue \ No newline at end of file diff --git a/2021/Day-5/day5-1.py b/2021/Day-5/day5-1.py new file mode 100644 index 0000000..7ea0489 --- /dev/null +++ b/2021/Day-5/day5-1.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 + +diagram = [] +totalPoints = 0 + +# Read all the line segments into an array. +segments = [] +with open('/home/jake/Documents/AoC-2021/Day-5/input.txt') as inputFile: + for line in inputFile: + segment = line.strip('\n').split(' -> ') + segments.append(segment) + +# Work out the bounds of our diagram and create an array for it. +largestX = 0 +largestY = 0 +for segment in segments: + if(int(segment[0].split(',')[0]) > largestX): + largestX = int(segment[0].split(',')[0]) + + if(int(segment[1].split(',')[0]) > largestX): + largestX = int(segment[1].split(',')[0]) + + if(int(segment[0].split(',')[1]) > largestY): + largestY = int(segment[0].split(',')[1]) + + if(int(segment[1].split(',')[1]) > largestX): + largestY = int(segment[1].split(',')[1]) + + +for i in range(0, largestY+1): + diagLine = [] + for i in range(0,largestX+1): + diagLine.append(0) + diagram.append(diagLine) + +# Run through the line segments and plot them on the diagram if they are horizontal or vertical. +for segment in segments: + startingX = int(segment[0].split(',')[0]) + startingY = int(segment[0].split(',')[1]) + endingX = int(segment[1].split(',')[0]) + endingY = int(segment[1].split(',')[1]) + if((startingX == endingX) or (startingY == endingY)): + endX = endingX +1 + startX = startingX + endY = endingY +1 + startY = startingY + + # Flip the start and end if the end is lower so our for loop will still run. + if(startingX > endingX): + endX = startingX +1 + startX = endingX + if(startingY > endingY): + endY = startingY +1 + startY = endingY + + for x in range(startX, endX): + for y in range(startY, endY): + diagram[y][x]+=1 + +# Find points where two or more line segments overlap. +for line in diagram: + for num in line: + if(num > 1): + totalPoints+=1 + +# Print the results +print("There are " + str(totalPoints) + " points where two or more lines overlap.") + diff --git a/2021/Day-5/day5-2.py b/2021/Day-5/day5-2.py new file mode 100644 index 0000000..a77d508 --- /dev/null +++ b/2021/Day-5/day5-2.py @@ -0,0 +1,79 @@ +#!/usr/bin/python3 + +diagram = [] +totalPoints = 0 + +# Read all the line segments into an array. +segments = [] +with open('/home/jake/Documents/AoC-2021/Day-5/input.txt') as inputFile: + for line in inputFile: + segment = line.strip('\n').split(' -> ') + segments.append(segment) + +# Work out the bounds of our diagram and create an array for it. +largestX = 0 +largestY = 0 +for segment in segments: + if(int(segment[0].split(',')[0]) > largestX): + largestX = int(segment[0].split(',')[0]) + + if(int(segment[1].split(',')[0]) > largestX): + largestX = int(segment[1].split(',')[0]) + + if(int(segment[0].split(',')[1]) > largestY): + largestY = int(segment[0].split(',')[1]) + + if(int(segment[1].split(',')[1]) > largestX): + largestY = int(segment[1].split(',')[1]) + +for i in range(0, largestY+1): + diagLine = [] + for i in range(0,largestX+1): + diagLine.append(0) + diagram.append(diagLine) + +# Run through the line segments and plot them on the diagram. +for segment in segments: + startingX = int(segment[0].split(',')[0]) + startingY = int(segment[0].split(',')[1]) + endingX = int(segment[1].split(',')[0]) + endingY = int(segment[1].split(',')[1]) + + # Create lists for the X and Y points our lines will go through. + if(startingX > endingX): + xStep = -1 + endingX-=1 + else: + xStep = 1 + endingX+=1 + if(startingY > endingY): + yStep = -1 + endingY-=1 + else: + yStep=1 + endingY+=1 + + xList = list(range(startingX, endingX, xStep)) + yList = list(range(startingY, endingY, yStep)) + + # If the line is horizontal or vertical there will be differing numbers of indexes so we need to loop though them separately. + if(len(xList) != len(yList)): + for x in xList: + for y in yList: + diagram[y][x]+=1 + # Otherwise they will be identical and we need to increment both lists together. + else: + for i in range(0, len(xList)): + currentX = xList[i] + currentY = yList[i] + diagram[currentY][currentX]+=1 + +# Find points where two or more line segments overlap. +for line in diagram: + for num in line: + if(num > 1): + totalPoints+=1 + +# Print the results. +print("There are " + str(totalPoints) + " points where two or more lines overlap.") + diff --git a/2021/Day-6/day6-1.py b/2021/Day-6/day6-1.py new file mode 100644 index 0000000..7e69f13 --- /dev/null +++ b/2021/Day-6/day6-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +# Open the file and read the one line into an array. +fish = [] +with open('/home/jake/Documents/AoC-2021/Day-6/input.txt') as inputFile: + fish = inputFile.readline().strip('\n').split(',') + fish = [ int(x) for x in fish ] + +# Loop over the array decrementing and adding fish as required for 80 days. +days = 0 +while days < 80: + for i in range(0, len(fish)): + if(fish[i] == 0): + fish[i] = 6 + fish.append(8) + else: + fish[i] = fish[i] -1 + days+=1 + +# Print the results. +print("Total fish afer " + str(days) + " days: " + str(len(fish))) \ No newline at end of file diff --git a/2021/Day-6/day6-2.py b/2021/Day-6/day6-2.py new file mode 100644 index 0000000..85c5f59 --- /dev/null +++ b/2021/Day-6/day6-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +# Open the file and read the one line into an array. +fish = [] +with open('/home/jake/Documents/AoC-2021/Day-6/input.txt') as inputFile: + fish = inputFile.readline().strip('\n').split(',') + fish = [ int(x) for x in fish ] + +fishFrequency = [] +for i in range(0, 9): + fishFrequency.append(0) + +for f in fish: + fishFrequency[f]+=1 + +# Loop over the array decrementing and adding fish as required for 80 days. +days = 0 +origFish = [] +for num in fishFrequency: + origFish.append(num) +while days < 256: + for i in range(1,len(fishFrequency)): + fishFrequency[i-1] = origFish[i] + fishFrequency[6] += origFish[0] + fishFrequency[8] = origFish[0] + + origFish=[] + for num in fishFrequency: + origFish.append(num) + + days+=1 + +totalFish = 0 +for total in fishFrequency: + totalFish += total + +# Print the results. +print("Total fish afer " + str(days) + " days: " + str(totalFish)) \ No newline at end of file diff --git a/2021/Day-7/day7-1.py b/2021/Day-7/day7-1.py new file mode 100644 index 0000000..001b1b2 --- /dev/null +++ b/2021/Day-7/day7-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 + +# Read in the file and split it into an array. +crabPositions = [] +with open('/home/jake/Documents/AoC-2021/Day-7/input.txt') as inputFile: + crabPositions = inputFile.readline().strip('\n').split(',') + crabPositions = [ int(x) for x in crabPositions] + +possiblePositions = list(range(min(crabPositions), max(crabPositions))) +fuelForPositions = [] + +# Calculate the lowest possible fuel usage. +for i in possiblePositions: + fuelUsed = 0 + for crab in crabPositions: + fuelUsed += abs(crab - i) + fuelForPositions.append(fuelUsed) + +# Print the results. +print("Lowest possible fuel usage is " + str(min(fuelForPositions)) + " by moving to position " + str(possiblePositions[fuelForPositions.index(min(fuelForPositions))])) \ No newline at end of file diff --git a/2021/Day-7/day7-2.py b/2021/Day-7/day7-2.py new file mode 100644 index 0000000..c688c1c --- /dev/null +++ b/2021/Day-7/day7-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 + +# Read in the file and split it into an array. +crabPositions = [] +with open('/home/jake/Documents/AoC-2021/Day-7/input.txt') as inputFile: + crabPositions = inputFile.readline().strip('\n').split(',') + crabPositions = [ int(x) for x in crabPositions ] + +possiblePositions = list(range(min(crabPositions), max(crabPositions))) +fuelForPositions = [] + +# Calculate the lowest possible fuel usage. +for i in possiblePositions: + fuelUsed = 0 + for crab in crabPositions: + postitionsToMove = abs(crab - i) + fuelUsed += postitionsToMove/2*(1+postitionsToMove) + fuelForPositions.append(int(fuelUsed)) + +# Print the results. +print("Lowest possible fuel usage is " + str(min(fuelForPositions)) + " by moving to position " + str(possiblePositions[fuelForPositions.index(min(fuelForPositions))])) \ No newline at end of file diff --git a/2021/Day-8/day8-1.py b/2021/Day-8/day8-1.py new file mode 100644 index 0000000..2b5621f --- /dev/null +++ b/2021/Day-8/day8-1.py @@ -0,0 +1,12 @@ +#!/usr/bin/python3 + +totalUnique = 0 + +# Read in the input file. +with open('/home/jake/Documents/AoC-2021/Day-8/input.txt') as inputFile: + for line in inputFile: + for output in line.strip('\n').split('|')[1].split(): + if(len(output) in [2,3,4,7]): + totalUnique += 1 + +print(totalUnique) \ No newline at end of file diff --git a/2021/Day-8/day8-2.py b/2021/Day-8/day8-2.py new file mode 100644 index 0000000..7965767 --- /dev/null +++ b/2021/Day-8/day8-2.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 + +finalTotal = 0 + +# Function to find and return common chars in a string. +def findCommon(s1, s2): + common = "" + for i in s1: + if(i in s2): + common += i + return common + +# Read in the input file. Put the signals and outputs in two +with open('/home/jake/Documents/AoC-2021/Day-8/input.txt') as inputFile: + for line in inputFile: + newNums = ['0','0','0','0','0','0','0','0','0','0'] + out = "" + signals = line.strip('\n').split('|')[0].split() + output = line.strip('\n').split('|')[1].split() + + for signal in signals: + if(len(signal) == 2): + newNums[1] = "".join(sorted(signal)) + elif(len(signal) == 3): + newNums[7] = "".join(sorted(signal)) + elif(len(signal) == 4): + newNums[4] = "".join(sorted(signal)) + elif(len(signal) == 7): + newNums[8] = "".join(sorted(signal)) + else: + continue + + for signal in signals: + sortedSignal = "".join(sorted(signal)) + if(sortedSignal in newNums): + continue + + if(len(signal) == 5): + # Check for 2. + if(len(findCommon(signal, newNums[4])) == 2 and len(findCommon(signal, newNums[1])) == 1 and len(findCommon(signal, newNums[7])) == 2): + newNums[2] = "".join(sorted(signal)) + # Check for 3. + elif(len(findCommon(signal, newNums[7])) == 3 and len(findCommon(signal, newNums[1])) == 2): + newNums[3] = "".join(sorted(signal)) + # Check for 5. + elif(len(findCommon(signal, newNums[4])) == 3 and len(findCommon(signal, newNums[1])) == 1): + newNums[5] = "".join(sorted(signal)) + elif(len(signal) == 6): + # Check for 6. + if(len(findCommon(signal, newNums[1])) == 1 and len(findCommon(signal, newNums[8])) == 6 and len(findCommon(signal, newNums[4])) == 3): + newNums[6] = "".join(sorted(signal)) + # Check for 9. + elif(len(findCommon(signal, newNums[7])) == 3 and len(findCommon(signal, newNums[4])) == 4): + newNums[9] = "".join(sorted(signal)) + # Check for 0. + elif(len(findCommon(signal, newNums[4])) == 3 and len(findCommon(signal, newNums[8])) == 6 and len(findCommon(signal, newNums[1])) == 2): + newNums[0] = "".join(sorted(signal)) + + for num in output: + sortedNum = "".join(sorted(num)) + displayedNum = newNums.index(sortedNum) + out = str(out) + str(displayedNum) + finalTotal += int(out) + print(finalTotal) \ No newline at end of file diff --git a/2021/Day-9/day9-1.py b/2021/Day-9/day9-1.py new file mode 100644 index 0000000..8110f36 --- /dev/null +++ b/2021/Day-9/day9-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +sumOfLowest = 0 +map = [] + +def checkSurrounding(list, x, y): + possibleX = [x, x-1, x+1] + possibleY = [y, y-1, y+1] + + for newY in possibleY: + if(newY < 0 or newY >= len(list)): + continue + for newX in possibleX: + if(newX < 0 or newX >= len(list[0])): + continue + if(list[y][x] > list[newY][newX]): + return False + return True + +with open('/home/jake/Documents/AoC-2021/Day-9/input.txt') as inputFile: + for line in inputFile: + lineList = list(line.strip('\n')) + lineList = [int(x) for x in lineList] + map.append(lineList) + +for x in range(len(map[0])): + for y in range(len(map)): + if(checkSurrounding(map, x, y)): + sumOfLowest += (map[y][x] + 1) + +print("Sum of risk levels is: " + str(sumOfLowest)) \ No newline at end of file