From 9156db6ef8d19b28185c23cade4bcbbdc948f736 Mon Sep 17 00:00:00 2001 From: DomJen00 Date: Tue, 24 Oct 2023 17:54:40 +0200 Subject: [PATCH] Finished day 1 and day 2 --- day01/day01.py | 33 +++++++++++++++++++++++------ day02/day02.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/day01/day01.py b/day01/day01.py index 9f69ffd..aa43e63 100644 --- a/day01/day01.py +++ b/day01/day01.py @@ -1,7 +1,5 @@ - from typing import List; import os; -import time; def read_input(file_name): current_path = os.path.dirname(__file__) @@ -19,10 +17,33 @@ class Frequency: def __init__(self): self.currentFrequency = 0 - def calibrate(self, num: int): - pass + def calibrate(self, num: int): + self.currentFrequency += num # put your inputs file next to this file! -lines = read_input('input.txt'); -# solve the problem here! \ No newline at end of file +lines = read_input('input.txt') + +# Part 1 +f = Frequency() +for freq in lines: + f.calibrate(freq) + +print("Part 1:", f.currentFrequency) + +# Part 2 +f_ = Frequency() +foundDuplicate = False +freqSet = set() + +while foundDuplicate == False: + for freq in lines: + f_.calibrate(freq) + + if f_.currentFrequency in freqSet: + foundDuplicate = True + break + + freqSet.add(f_.currentFrequency) + +print("Part 2:", f_.currentFrequency) \ No newline at end of file diff --git a/day02/day02.py b/day02/day02.py index 7ea619d..f74ed76 100644 --- a/day02/day02.py +++ b/day02/day02.py @@ -12,6 +12,59 @@ def read_input(file_name): print(f"An error occurred: {e}") return lines -#a + lines = read_input('input.txt') -# solve the problem here! \ No newline at end of file +# solve the problem here +# Part 1 +def count_letter_occurrences(): + count_doubles = 0 + count_tripples = 0 + # Iterate through the input list + for line in lines: + letter_counts = {} + + for letter in line: + if letter in letter_counts: + letter_counts[letter] += 1 + else: + letter_counts[letter] = 1 + + # Check if any letter appears exactly two or three times + if 2 in letter_counts.values(): + count_doubles += 1 + if 3 in letter_counts.values(): + count_tripples += 1 + + return count_doubles, count_tripples + +def calc_checksum(num1, num2): + return num1 * num2 + +double,tripple = count_letter_occurrences() + +# Calculate the checksum by multiplying the counts +checksum = calc_checksum(double, tripple) +print(checksum) + +# Part 2 +# Function to find the common letters between two box IDs +def find_common_letters(line, line_): + common_letter = [] + for letter, letter_ in zip(line, line_): + if letter == letter_: + common_letter.append(letter) + + if len(common_letter) == len(lines[0]) - 1: + for letters in common_letter: + print(letters, end="") + return + +# Iterate through the list to find the correct pair +i = 0 +common_letters = [] +while i < len(lines): + j = i + 1 + while j < len(lines): + find_common_letters(lines[i], lines[j]) + j += 1 + i += 1 \ No newline at end of file