diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cacf4d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv/ +.vscode/ +2023 \ No newline at end of file diff --git a/2024/01/1-1.py b/2024/01/1-1.py new file mode 100644 index 0000000..c93b79b --- /dev/null +++ b/2024/01/1-1.py @@ -0,0 +1,19 @@ +col_one = []; +col_two = []; + +with open('2024/01/input.txt') as f: + for line in f: + row = line.split(); + col_one.append(int(row[0])); + col_two.append(int(row[1])); + +col_one.sort(); +col_two.sort(); + +sum = 0 + +for i, value in enumerate(col_one): + sum += abs(value - col_two[i]); + +print(sum); + \ No newline at end of file diff --git a/2024/01/1-2.py b/2024/01/1-2.py new file mode 100644 index 0000000..e7fad51 --- /dev/null +++ b/2024/01/1-2.py @@ -0,0 +1,33 @@ +left_dict = {}; +right_dict = {}; + +sum = 0; + +def getUniqueCountsOfKeys(list, dict): + for key in list: + if key in dict.keys(): + dict[key] = dict[key] + 1; + else: + dict[key] = 1; + +def getSimilarityScores(list): + for key in list: + if key in right_dict.keys(): + global sum; + sum += int(key) * right_dict[key]; + +col_one = []; +col_two = []; + +with open('2024/01/input.txt') as f: + for line in f: + row = line.split(); + col_one.append(row[0]); + col_two.append(row[1]); + +getUniqueCountsOfKeys(col_one, left_dict); +getUniqueCountsOfKeys(col_two, right_dict); + +getSimilarityScores(col_one); + +print(sum); \ No newline at end of file diff --git a/2024/02/2-1.py b/2024/02/2-1.py new file mode 100644 index 0000000..08cef5d --- /dev/null +++ b/2024/02/2-1.py @@ -0,0 +1,33 @@ +lines = []; +goodLines = 0; + +def testLine(line): + numbers = line.split(' '); + + increasing = bool(int(numbers[0]) < int(numbers[1])) + + for i, number in enumerate(numbers): + if i == 0: + continue; + + diff = int(numbers[i]) - int(numbers[i - 1]); + + if increasing and diff < 0: + return False; + + if not increasing and diff > 0: + return False; + + if abs(diff) > 3 or diff == 0: + return False; + return True; + + +with open('2024/02/input.txt') as f: + for line in f: + if testLine(line): + goodLines = goodLines + 1; + + +print(goodLines); + diff --git a/2024/02/2-2.py b/2024/02/2-2.py new file mode 100644 index 0000000..07e4b9c --- /dev/null +++ b/2024/02/2-2.py @@ -0,0 +1,44 @@ +lines = []; +goodLines = 0; + +def testTwoNumbers(increasing, first, second): + diff = int(second) - int(first); + if increasing and diff < 0: + return False; + + if not increasing and diff > 0: + return False; + + if abs(diff) > 3 or diff == 0: + return False; + return True; + + +def testLine(numbers, savedOnce): + increasing = bool(int(numbers[0]) < int(numbers[1])); + + for i, number in enumerate(numbers): + if i == 0: + continue; + + if not testTwoNumbers(increasing, numbers[i - 1], numbers[i]): + if not savedOnce: + for j, _ in enumerate(numbers): + newArray = numbers[:]; + newArray.pop(j); + if testLine(newArray, True): + return True; + return False; + else: + return False; + return True; + + +with open('2024/02/input.txt') as f: + for line in f: + line = line.replace('\n','') + numbers = line.split(' ') + if testLine(numbers, False): + goodLines = goodLines + 1; + +print(goodLines); \ No newline at end of file diff --git a/2024/03/3-1.py b/2024/03/3-1.py new file mode 100644 index 0000000..0915793 --- /dev/null +++ b/2024/03/3-1.py @@ -0,0 +1,18 @@ +import re + +total = 0; + +def findMuls(line): + pattern = re.findall(r"mul\([0-9]+,[0-9]+\)", line) + return pattern + + +with open('2024/03/input.txt') as f: + for line in f: + operations = findMuls(line); + + for operation in operations: + mults = re.findall(r'\d+', operation); + total += int(mults[0]) * int(mults[1]); + +print(total) \ No newline at end of file diff --git a/2024/03/3-2.py b/2024/03/3-2.py new file mode 100644 index 0000000..4450990 --- /dev/null +++ b/2024/03/3-2.py @@ -0,0 +1,29 @@ +import re + +total = 0; + +def findMuls(line): + pattern = re.findall(r"mul\([0-9]+,[0-9]+\)", line) + return pattern + +with open('2024/03/input.txt') as f: + for line in f: + operations = findMuls(line); + instructions = re.findall(r"(mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don\'t\(\))", line); + + enabled = True; + + for instruction in instructions: + match instruction[0]: + case 'do()': + enabled = True; + case 'don\'t()': + enabled = False; + case _: + if enabled: + mults = findMuls(instruction[0]) + total += int(instruction[1]) * int(instruction[2]); + + + +print(total) \ No newline at end of file diff --git a/2024/04/4-1.py b/2024/04/4-1.py new file mode 100644 index 0000000..2886e8b --- /dev/null +++ b/2024/04/4-1.py @@ -0,0 +1,50 @@ + +width = 0; +height = 0; + +with open('2024/04/input.txt') as f: + for line in f: + width = len(line); + height += 1; + +stringToFind = 'XMAS' + +def inBounds(number, length): + if number < 0 or number >= length: + return False; + return True; + +# Recursively find the string +def findIfContainsSubstring(fromX, fromY, xDir, yDir, substring): + if len(substring) == 0: + return True; + + global width, height; + # ensure new spot isn't OoB + newX = fromX + xDir; + if not inBounds(newX, width): + return False; + newY = fromY + yDir; + if not inBounds(newY, height): + return False; + + if matrix[newX, newY] == substring[0]: + return findIfContainsSubstring(newX, newY, xDir, yDir, substring[1:]); + + +matrix = {} +sequencesFound = 0; +with open('2024/04/input.txt') as f: + for i, line in enumerate(f): + for j, char in enumerate(line.strip()): + matrix[i,j] = char; + +for i in range(width): + for j in range(height): + if matrix[i,j] == stringToFind[0]: + for xDir in range(-1,2): + for yDir in range(-1,2): + if findIfContainsSubstring(i,j, xDir, yDir, stringToFind[1:]): + sequencesFound += 1; + +print(sequencesFound); \ No newline at end of file diff --git a/2024/04/4-2.py b/2024/04/4-2.py new file mode 100644 index 0000000..347dd8b --- /dev/null +++ b/2024/04/4-2.py @@ -0,0 +1,64 @@ +width = 0; +height = 0; + +with open('2024/04/input.txt') as f: + for line in f: + width = len(line); + height += 1; + +charsToFind = []; + +def resetCharsToFind(): + global charsToFind; + charsToFind = ['M', 'S']; + +def inBounds(number, length): + if number < 0 or number >= length: + return False; + return True; + +def cornerIsGood(xPos, yPos): + global charsToFind; + global width, height; + + if not (inBounds(xPos, width) and inBounds(yPos, height)): + return False; + + charAtPoint = matrix[xPos, yPos] + if charAtPoint in charsToFind: + return True; + +# Recursively find the string +def charHasGoodDiagonal(xPos, yPos, xDir, yDir): + global charsToFind; + resetCharsToFind(); + + # ensure new spot isn't OoB + cornerX = xPos + xDir; + cornerY = yPos + yDir; + + if cornerIsGood(cornerX, cornerY): + charsToFind.remove(matrix[cornerX, cornerY]); + + cornerX = xPos + (xDir * -1); + cornerY = yPos + (yDir * -1); + + if cornerIsGood(cornerX, cornerY): + return True; + return False; + + +matrix = {} +crossesFound = 0; +with open('2024/04/input.txt') as f: + for i, line in enumerate(f): + for j, char in enumerate(line.strip()): + matrix[i,j] = char; + +for i in range(width): + for j in range(height): + if matrix[i,j] == 'A': + if charHasGoodDiagonal(i, j, -1, -1) and charHasGoodDiagonal(i, j, 1, -1): + crossesFound += 1; + +print(crossesFound); \ No newline at end of file diff --git a/2024/05/5-1.py b/2024/05/5-1.py new file mode 100644 index 0000000..13df437 --- /dev/null +++ b/2024/05/5-1.py @@ -0,0 +1,54 @@ +instructions = []; + +allLines = []; +goodLines = []; + +sum = 0; + +def testInput(num, numsBefore, numsAfter): + for numBefore in numsBefore: + if f"{num}|{numBefore}" in instructions: + return False; + + for numAfter in numsAfter: + if f"{numAfter}|{num}" in instructions: + return False; + + return True; + + +def testLine(line): + numsBefore = [] + numsAfter = [int(numeric_string) for numeric_string in line.split(',')]; + + while len(numsAfter) > 0: + num = numsAfter.pop(0); + if not testInput(num, numsBefore, numsAfter): + return False; + numsBefore.append(num); + + return True; + +with open('2024/05/input.txt') as f: + for i, line in enumerate(f): + line = line.strip(); + if len(line) == 0: + continue; + + if '|' not in line: + # it's an input + allLines.append(line); + else: + # it's an instruction + instructions.append(line); + +for line in allLines: + if testLine(line): + goodLines.append(line) + +for line in goodLines: + nums = [int(numeric_string) for numeric_string in line.split(',')]; + index = int(len(nums) / 2); + sum += nums[index]; + +print(sum) \ No newline at end of file diff --git a/2024/05/5-2.py b/2024/05/5-2.py new file mode 100644 index 0000000..b0c950d --- /dev/null +++ b/2024/05/5-2.py @@ -0,0 +1,73 @@ +instructions = []; + +allLines = []; +goodLines = []; +badLines = []; + +sum = 0; + +def testInput(num, numsBefore, numsAfter): + for numBefore in numsBefore: + if f"{num}|{numBefore}" in instructions: + return False; + + for numAfter in numsAfter: + if f"{numAfter}|{num}" in instructions: + return False; + + return True; + + +def testLine(line): + numsBefore = [] + numsAfter = [int(numeric_string) for numeric_string in line.split(',')]; + + while len(numsAfter) > 0: + num = numsAfter.pop(0); + if not testInput(num, numsBefore, numsAfter): + return False; + numsBefore.append(num); + + return True; + +def fixLine(line): + allNums = [int(numeric_string) for numeric_string in line.split(',')]; + + length = len(allNums) + + # bubble sort and flip the order of neighbours if they have flipped instructions + for _ in range(length - 1): + swapped = False; + for i in range(len(allNums) - 1): + if f"{allNums[i+1]}|{allNums[i]}" in instructions: + swapped = True; + allNums[i], allNums[i+1] = allNums[i+1], allNums[i]; + length = length - 1; + if not swapped: + break; + + return allNums; + +with open('2024/05/input.txt') as f: + for i, line in enumerate(f): + line = line.strip(); + if len(line) == 0: + continue; + + if '|' not in line: + # it's an input + allLines.append(line); + else: + # it's an instruction + instructions.append(line); + +for line in allLines: + if not testLine(line): + badLines.append(line); + +for line in badLines: + nums = fixLine(line); + index = int(len(nums) / 2); + sum += nums[index]; + +print(sum) \ No newline at end of file diff --git a/2024/05/testinput.txt b/2024/05/testinput.txt new file mode 100644 index 0000000..d4d4441 --- /dev/null +++ b/2024/05/testinput.txt @@ -0,0 +1,28 @@ +47|53 +97|13 +97|61 +97|47 +75|29 +61|13 +75|53 +29|13 +97|29 +53|29 +61|53 +97|53 +61|29 +47|13 +75|47 +97|75 +47|61 +75|61 +47|29 +75|13 +53|13 + +75,47,61,53,29 +97,61,53,29,13 +75,29,13 +75,97,47,61,53 +61,13,29 +97,13,75,29,47 \ No newline at end of file diff --git a/2024/06/6-1.py b/2024/06/6-1.py new file mode 100644 index 0000000..820d767 --- /dev/null +++ b/2024/06/6-1.py @@ -0,0 +1,73 @@ +# get the map into a 2D array +# keep going up until you get to a hash +# leave X's behind at last spot +# rotate the map 90 degrees like it's a matrix +# thanks stack overflow: +# [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)] +# keep going till out of bounds +# count X's + +map = []; +startX = -1; +startY = -1; + +def printMap(): + for row in map: + print(row) + print('===============') + +def findCaret(): + global startX, startY; + for i, row in enumerate(map): + for j, char in enumerate(row): + if char == "^": + startY, startX = i, j; + +def countXs(): + total = 0; + for i, row in enumerate(map): + for j, char in enumerate(row): + if char == "X": + total += 1; + return total + +def rotateMapCounterClockwise(): + global map; + map = [[map[j][i] for j in range(len(map))] for i in range(len(map[0])-1,-1,-1)] + +def goUpUntilStopped(x, y): + global map; + + if y == 0: + # Found a way out of the map + map[y][x] = 'X'; + return True; + + newY = y-1; + if map[newY][x] == '#': + map[y][x] = '^' + return False; # Can't go anymore up, but not OoB + + map[y][x] = 'X' + return goUpUntilStopped(x, newY); # Not done yet + +def traverseMap(): + while True: + findCaret(); + done = goUpUntilStopped(startX, startY); + if done: + break; + rotateMapCounterClockwise(); + +with open('2024/06/testinput.txt') as f: + for line in f: + row = []; + for c in line.strip(): + row.append(c); + map.append(row); + + traverseMap(); + for row in map: + print(row) + print(countXs()); + diff --git a/2024/06/6-2.py b/2024/06/6-2.py new file mode 100644 index 0000000..efe8670 --- /dev/null +++ b/2024/06/6-2.py @@ -0,0 +1,75 @@ +import stopit # get some help + +originalMap = []; +map = []; +startX = -1; +startY = -1; + +def printMap(): + for row in map: + print(row) + print('===============') + +def findCaret(): + global startX, startY; + for i, row in enumerate(map): + for j, char in enumerate(row): + if char == "^": + startY, startX = i, j; + +def rotateMapCounterClockwise(): + global map; + map = [[map[j][i] for j in range(len(map))] for i in range(len(map[0])-1,-1,-1)] + +def goUpUntilStopped(x, y): + global map; + + if y == 0: + # Found a way out of the map + map[y][x] = 'X'; + return True; + + newY = y-1; + if map[newY][x] == '#': + map[y][x] = '^' + return False; # Can't go anymore up, but not OoB + + map[y][x] = 'X' + return goUpUntilStopped(x, newY); # Not done yet + +@stopit.threading_timeoutable(default=False) +def traverseMap(): + while True: + findCaret(); + done = goUpUntilStopped(startX, startY); + if done: + break; + rotateMapCounterClockwise(); + return True; + +# This one's real shit. If you're running it, clear your day. +with open('2024/06/input.txt') as f: + for i, line in enumerate(f): + row = []; + for c in line.strip(): + row.append(c); + originalMap.append(row); + + + loops = 0; + for i, row in enumerate(originalMap): + for j, char in enumerate(row): + map = [row[:] for row in originalMap]; + print(map) + if map[i][j] != '.': + continue; + + map[i][j] = '#'; + if not traverseMap(timeout=1): + print(f'{i}, {j} was good') + loops += 1; + else: + print(f'{i}, {j} sucked') + + + print(loops); diff --git a/2024/06/testinput.txt b/2024/06/testinput.txt new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/2024/06/testinput.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/2024/07/7-1.py b/2024/07/7-1.py new file mode 100644 index 0000000..e3c85c0 --- /dev/null +++ b/2024/07/7-1.py @@ -0,0 +1,39 @@ +import itertools + +idealResult = []; + +def applyOperator(left, right, add): + if add == '0': + return left + right; + elif add == '1': + return left * right; + else: + raise Exception("bruh"); + +def testSequence(desiredResult, nums): + length = len(nums) + for op in map(''.join, itertools.product('01', repeat=(length-1))): + calculatedResult = nums[0]; + for i in range(1, length): + calculatedResult = applyOperator(calculatedResult, nums[i], op[i-1]) + if calculatedResult > desiredResult: + break; + if calculatedResult == desiredResult: + return True; + return False; + + + +with open('2024/07/input.txt') as f: + total = 0; + for i, line in enumerate(f): + split = line.split(':'); + + idealResult = int(split[0]); + + nums = [int(numeric_string) for numeric_string in split[1].split()]; + if testSequence(idealResult, nums): + total += idealResult; + print(total) + + diff --git a/2024/07/7-2.py b/2024/07/7-2.py new file mode 100644 index 0000000..db89234 --- /dev/null +++ b/2024/07/7-2.py @@ -0,0 +1,41 @@ +import itertools + +idealResult = []; + +def applyOperator(left, right, op): + if op == '0': + return left + right; + elif op == '1': + return left * right; + elif op == '2': + return int(str(left) + str(right)); + else: + raise Exception("bruh"); + +def testSequence(desiredResult, nums): + length = len(nums) + for op in map(''.join, itertools.product('012', repeat=(length-1))): + calculatedResult = nums[0]; + for i in range(1, length): + calculatedResult = applyOperator(calculatedResult, nums[i], op[i-1]) + if calculatedResult > desiredResult: + break; + if calculatedResult == desiredResult: + return True; + return False; + + + +with open('2024/07/input.txt') as f: + total = 0; + for i, line in enumerate(f): + split = line.split(':'); + + idealResult = int(split[0]); + + nums = [int(numeric_string) for numeric_string in split[1].split()]; + if testSequence(idealResult, nums): + total += idealResult; + print(total) + + diff --git a/2024/07/testinput.txt b/2024/07/testinput.txt new file mode 100644 index 0000000..87b8b25 --- /dev/null +++ b/2024/07/testinput.txt @@ -0,0 +1,9 @@ +190: 10 19 +3267: 81 40 27 +83: 17 5 +156: 15 6 +7290: 6 8 6 15 +161011: 16 10 13 +192: 17 8 14 +21037: 9 7 18 13 +292: 11 6 16 20 \ No newline at end of file diff --git a/2024/08/8-1.py b/2024/08/8-1.py new file mode 100644 index 0000000..0eb8bf3 --- /dev/null +++ b/2024/08/8-1.py @@ -0,0 +1,55 @@ + +antiNodes = {}; +antenna = {} +map = []; +width = -1; +height = -1; + +def inBounds(x, y): + if x < 0 or x >= width: + return False + if y < 0 or y >= height: + return False + return True + +def makeNode(fromX, fromY, xDir, yDir): + newX = fromX + xDir; + newY = fromY + yDir; + if inBounds(newX, newY): + antiNodes[newX+1, newY+1] = True; + +with open('2024/08/input.txt') as f: + total = 0; + for x, line in enumerate(f): + line = line.strip() + map.append(line) + for y, char in enumerate(line): + if char != '.': + if char not in antenna.keys(): + antenna[char] = [] + antenna[char].append(f'{x},{y}'); + + height = len(map) + width = len(map[0]) + + + for key in antenna.keys(): + spots = antenna[key]; + for i, first in enumerate(spots): + firstX = int(first.split(',')[0]) + firstY = int(first.split(',')[1]) + for j in range(i+1, len(spots)): + second = spots[j] + secondX = int(second.split(',')[0]) + secondY = int(second.split(',')[1]) + + xDiff = firstX - secondX; + yDiff = firstY - secondY; + + # first antinode + makeNode(firstX, firstY, xDiff, yDiff) + + # second antinode + makeNode(secondX, secondY, -xDiff, -yDiff) + +print(len(antiNodes)) \ No newline at end of file diff --git a/2024/08/8-2.py b/2024/08/8-2.py new file mode 100644 index 0000000..7c6b8ab --- /dev/null +++ b/2024/08/8-2.py @@ -0,0 +1,71 @@ + +antiNodes = {}; +antenna = {} +map = []; +width = -1; +height = -1; + +def inBounds(x, y): + if x < 0 or x >= width: + return False + if y < 0 or y >= height: + return False + return True + +def makeNode(fromX, fromY, xDir, yDir): + newX = fromX + xDir; + newY = fromY + yDir; + if inBounds(newX, newY): + antiNodes[newX, newY] = True; + return [True, newX, newY]; + return [False]; + +def nodeToTheEnd(fromX, fromY, xDir, yDir): + while True: + nodeDeets = makeNode(fromX, fromY, xDir, yDir) + if nodeDeets[0] == False: + break; + fromX = nodeDeets[1]; + fromY = nodeDeets[2]; + +with open('2024/08/input.txt') as f: + total = 0; + for x, line in enumerate(f): + line = line.strip() + map.append(line) + for y, char in enumerate(line): + if char != '.': + if char not in antenna.keys(): + antenna[char] = [] + antenna[char].append(f'{x},{y}'); + + height = len(map) + width = len(map[0]) + + + for key in antenna.keys(): + spots = antenna[key]; + for i, first in enumerate(spots): + firstX = int(first.split(',')[0]) + firstY = int(first.split(',')[1]) + antiNodes[firstX, firstY] = True; + for j in range(i+1, len(spots)): + second = spots[j] + secondX = int(second.split(',')[0]) + secondY = int(second.split(',')[1]) + antiNodes[secondX, secondY] = True; + + xDiff = firstX - secondX; + yDiff = firstY - secondY; + + # first antinode + nodeToTheEnd(firstX, firstY, xDiff, yDiff) + + # second antinode + nodeToTheEnd(secondX, secondY, -xDiff, -yDiff) + +for i in range(len(antiNodes)): + for j in range(len(antiNodes)): + if (i,j) in antiNodes.keys(): + print((i,j)) +print(len(antiNodes)) \ No newline at end of file diff --git a/2024/08/testinput.txt b/2024/08/testinput.txt new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/2024/08/testinput.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file diff --git a/2024/08/testinput2.txt b/2024/08/testinput2.txt new file mode 100644 index 0000000..023061c --- /dev/null +++ b/2024/08/testinput2.txt @@ -0,0 +1,10 @@ +T......... +...T...... +.T........ +.......... +.......... +.......... +.......... +.......... +.......... +.......... \ No newline at end of file diff --git a/2024/09/9-1.py b/2024/09/9-1.py new file mode 100644 index 0000000..53872b0 --- /dev/null +++ b/2024/09/9-1.py @@ -0,0 +1,43 @@ +currentValue = 0; +fileArray = []; +total = 0; + +def createBlockArray(line): + global currentValue + for i, char in enumerate(line): + freeSpace = i % 2 == 1; + + if freeSpace: + for _ in range(int(char)): + fileArray.append(".") + else: + for _ in range(int(char)): + fileArray.append(currentValue) + currentValue += 1 + +def sortfileString(): + global fileString; + while True: + firstDotIndex = next((idx, space) for idx, space in enumerate(fileArray) if str(space) == ".")[0] + lastCharIndex = next((idx, space) for idx, space in enumerate(fileArray[::-1]) if str(space) != ".")[0] + lastCharIndex = len(fileArray) - lastCharIndex - 1 + if firstDotIndex > lastCharIndex: + break + fileArray[firstDotIndex] = fileArray[lastCharIndex] + fileArray[lastCharIndex] = "." + +def checksum(): + global total; + counter = 0; + for i, char in enumerate(fileArray): + if char == ".": + return + total += counter * int(char) + counter += 1 + +with open("2024/09/input.txt") as f: + for line in f: + createBlockArray(line) + sortfileString() + checksum() + print(total) \ No newline at end of file diff --git a/2024/09/9-2.py b/2024/09/9-2.py new file mode 100644 index 0000000..6a18ec9 --- /dev/null +++ b/2024/09/9-2.py @@ -0,0 +1,45 @@ +import re; + +currentValue = 0; +fileArray = []; +total = 0; + +def createBlockArray(line): + global currentValue + for i, char in enumerate(line): + freeSpace = i % 2 == 1; + + if freeSpace: + for _ in range(int(char)): + fileArray.append(".") + else: + for _ in range(int(char)): + fileArray.append(currentValue) + currentValue += 1 + +def sortfileString(): + global fileString; + while True: + firstDotIndex = next((idx, space) for idx, space in enumerate(fileArray) if str(space) == ".")[0] + lastCharIndex = next((idx, space) for idx, space in enumerate(fileArray[::-1]) if str(space) != ".")[0] + lastCharIndex = len(fileArray) - lastCharIndex - 1 + if firstDotIndex > lastCharIndex: + break + fileArray[firstDotIndex] = fileArray[lastCharIndex] + fileArray[lastCharIndex] = "." + +def checksum(): + global total; + counter = 0; + for i, char in enumerate(fileArray): + if char == ".": + return + total += counter * int(char) + counter += 1 + +with open("2024/09/input.txt") as f: + for line in f: + createBlockArray(line) + sortfileString() + checksum() + print(total) \ No newline at end of file diff --git a/2024/09/testinput.txt b/2024/09/testinput.txt new file mode 100644 index 0000000..5ff5aae --- /dev/null +++ b/2024/09/testinput.txt @@ -0,0 +1 @@ +2333133121414131402 \ No newline at end of file diff --git a/2024/10/10-1.py b/2024/10/10-1.py new file mode 100644 index 0000000..8c9731c --- /dev/null +++ b/2024/10/10-1.py @@ -0,0 +1,45 @@ +map = {} +highestFound = 0 +length = 0 + +peaks= {} + +def findRoutes(point, previousValue): + global peaks + x, y = point + + if point not in map.keys(): + return + + valueAtPoint = map[point] + + if valueAtPoint != previousValue + 1: + return + + if valueAtPoint == 9: + peaks[point] = True + return + + findRoutes((x + 1, y), valueAtPoint) + findRoutes((x - 1, y), valueAtPoint) + findRoutes((x, y + 1), valueAtPoint) + findRoutes((x, y - 1), valueAtPoint) + + + +with open("2024/10/input.txt") as f: + for i, line in enumerate(f): + line = line.strip() + length = len(line) + for j, num in enumerate(line): + map[i,j] = int(num) + + starts = [k for k,v in map.items() if v == 0] + totalTrailheads = 0 + for startPoint in starts: + findRoutes(startPoint, -1) + totalTrailheads += len(peaks) + peaks = {} + print(totalTrailheads) + + \ No newline at end of file diff --git a/2024/10/10-2.py b/2024/10/10-2.py new file mode 100644 index 0000000..2524738 --- /dev/null +++ b/2024/10/10-2.py @@ -0,0 +1,44 @@ +import functools +map = {} +highestFound = 0 +length = 0 + +@functools.cache +def findRoutes(point, previousValue): + x, y = point + + if point not in map.keys(): + return 0 + + valueAtPoint = map[point] + + if valueAtPoint != previousValue + 1: + return 0 + + if valueAtPoint == 9: + return 1 + + routesFound = 0 + routesFound += findRoutes((x + 1, y), valueAtPoint) + routesFound += findRoutes((x - 1, y), valueAtPoint) + routesFound += findRoutes((x, y + 1), valueAtPoint) + routesFound += findRoutes((x, y - 1), valueAtPoint) + return routesFound + + + +with open("2024/10/input.txt") as f: + for i, line in enumerate(f): + line = line.strip() + length = len(line) + for j, num in enumerate(line): + map[i,j] = int(num) + + starts = [k for k,v in map.items() if v == 0] + totalTrailheads = 0 + for startPoint in starts: + trailheadsFound = findRoutes(startPoint, -1) + totalTrailheads += trailheadsFound + print(totalTrailheads) + + \ No newline at end of file diff --git a/2024/10/testinput.txt b/2024/10/testinput.txt new file mode 100644 index 0000000..7bb1248 --- /dev/null +++ b/2024/10/testinput.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 \ No newline at end of file diff --git a/2024/11/11-1.py b/2024/11/11-1.py new file mode 100644 index 0000000..8555209 --- /dev/null +++ b/2024/11/11-1.py @@ -0,0 +1,33 @@ +blinks = 25 +stones = [] + +def doStoneyThings(): + global stones + newStones = [] + for stone in stones: + if stone == 0: + newStones.append(1) + elif len(str(stone)) % 2 == 0: + stone = str(stone) + midpoint = int(len(stone) / 2) + left, right = int(stone[:midpoint]), int(stone[midpoint:]) + newStones.append(left) + newStones.append(right) + else: + newVal = stone * 2024 + newStones.append(newVal) + stones = newStones + print(stones) + +with open("2024/11/testinput.txt") as f: + for line in f: + stones = line.split() + stones = list(map(int, stones)) + print("Initial arrangement:") + print(stones) + + for i in range(blinks): + doStoneyThings() + print(f"After {i} blinks:") + print(stones) + print(len(stones)) \ No newline at end of file diff --git a/2024/11/11-2.py b/2024/11/11-2.py new file mode 100644 index 0000000..4067fd3 --- /dev/null +++ b/2024/11/11-2.py @@ -0,0 +1,28 @@ +import functools +import time + +blinks = 75 + +@functools.cache +def doStoneyThings(stone, times): + if times == 0: + return 1 + + if stone == 0: + return doStoneyThings(1, times - 1) + elif len(str(stone)) % 2 == 0: + stone = str(stone) + midpoint = int(len(stone) / 2) + return doStoneyThings(int(stone[:midpoint]), times - 1) + doStoneyThings(int(stone[midpoint:]), times - 1) + else: + return doStoneyThings(stone * 2024, times - 1) + +with open("2024/11/input.txt") as f: + for line in f: + stones = line.split() + stones = list(map(int, stones)) + + total = 0; + for stone in stones: + total += doStoneyThings(stone, blinks) + print(total) diff --git a/2024/11/testinput.txt b/2024/11/testinput.txt new file mode 100644 index 0000000..528f9d5 --- /dev/null +++ b/2024/11/testinput.txt @@ -0,0 +1 @@ +125 17 \ No newline at end of file diff --git a/2024/12/12-1.py b/2024/12/12-1.py new file mode 100644 index 0000000..c902572 --- /dev/null +++ b/2024/12/12-1.py @@ -0,0 +1,8 @@ +width = 0 +map = {} + +with open("2024/12/testinput.txt") as f: + for i, line in enumerate(f): + width = i + 1; + + \ No newline at end of file diff --git a/2024/12/testinput.txt b/2024/12/testinput.txt new file mode 100644 index 0000000..cc5f968 --- /dev/null +++ b/2024/12/testinput.txt @@ -0,0 +1,4 @@ +AAAA +BBCD +BBCC +EEEC \ No newline at end of file diff --git a/2024/12/testinput2.txt b/2024/12/testinput2.txt new file mode 100644 index 0000000..0b328f1 --- /dev/null +++ b/2024/12/testinput2.txt @@ -0,0 +1,10 @@ +RRRRIICCFF +RRRRIICCCF +VVRRRCCFFF +VVRCCCJFFF +VVVVCJJCFE +VVIVCCJJEE +VVIIICJJEE +MIIIIIJJEE +MIIISIJEEE +MMMISSJEEE \ No newline at end of file diff --git a/2024/13/13-1.py b/2024/13/13-1.py new file mode 100644 index 0000000..687c7c9 --- /dev/null +++ b/2024/13/13-1.py @@ -0,0 +1,50 @@ +import re + +a1 = a2 = b1 = b2 = c1 = c2 = 0 +totalTokensSpent = 0 + +def nums(string): + return tuple(int(string) for string in re.findall(r"(\-*\d+)", string)) + +def doCramersRule(): + # Will he say it? + det = (a1 * b2) - (a2 * b1) + detX = (c1 * b2) - (c2 * b1) + detY = (a1 * c2) - (a2 * c1) + + x = detX / det + y = detY / det + + return (x, y) + +def seeIfSpentTokens(): + global totalTokensSpent + x, y = doCramersRule() + if x.is_integer() and y.is_integer(): + tokensSpent = x * 3 + y + totalTokensSpent += tokensSpent + +with open("2024/13/input.txt") as f: + for line in f: + line = line.strip() + + if len(line) == 0: + seeIfSpentTokens() + continue + + line = line.split(':') + label, input = line[0], line[1] + + match label: + case "Button A": + a1, a2 = nums(input) + case "Button B": + b1, b2 = nums(input) + case "Prize": + c1, c2 = nums(input) + +seeIfSpentTokens() +print(totalTokensSpent) + + + \ No newline at end of file diff --git a/2024/13/13-2.py b/2024/13/13-2.py new file mode 100644 index 0000000..f5322d2 --- /dev/null +++ b/2024/13/13-2.py @@ -0,0 +1,52 @@ +import re + +a1 = a2 = b1 = b2 = c1 = c2 = 0 +totalTokensSpent = 0 + +def nums(string): + return tuple(int(string) for string in re.findall(r"(\-*\d+)", string)) + +def doCramersRule(): + # Will he say it? + det = (a1 * b2) - (a2 * b1) + detX = (c1 * b2) - (c2 * b1) + detY = (a1 * c2) - (a2 * c1) + + x = detX / det + y = detY / det + + return (x, y) + +def seeIfSpentTokens(): + global totalTokensSpent + x, y = doCramersRule() + if x.is_integer() and y.is_integer(): + tokensSpent = x * 3 + y + totalTokensSpent += tokensSpent + +with open("2024/13/input.txt") as f: + for line in f: + line = line.strip() + + if len(line) == 0: + seeIfSpentTokens() + continue + + line = line.split(':') + label, input = line[0], line[1] + + match label: + case "Button A": + a1, a2 = nums(input) + case "Button B": + b1, b2 = nums(input) + case "Prize": + c1, c2 = nums(input) + c1 += 10_000_000_000_000 + c2 += 10_000_000_000_000 + +seeIfSpentTokens() +print(totalTokensSpent) + + + \ No newline at end of file diff --git a/2024/13/testinput.txt b/2024/13/testinput.txt new file mode 100644 index 0000000..f10c5a6 --- /dev/null +++ b/2024/13/testinput.txt @@ -0,0 +1,15 @@ +Button A: X+94, Y+34 +Button B: X+22, Y+67 +Prize: X=8400, Y=5400 + +Button A: X+26, Y+66 +Button B: X+67, Y+21 +Prize: X=12748, Y=12176 + +Button A: X+69, Y+23 +Button B: X+27, Y+71 +Prize: X=18641, Y=10279 + +Button A: X+17, Y+86 +Button B: X+84, Y+37 +Prize: X=7870, Y=6450