Created
December 3, 2024 17:34
-
-
Save matthewbauer/443566110fb9622f79444d4c7f335b66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_num(line, j): | |
num = "" | |
while line[j] in "0123456789": | |
num += line[j] | |
j += 1 | |
return j, int(num) | |
def check(line, j, s): | |
return line[j:j+len(s)] == s | |
file = open("day3.csv", "r") | |
sum = 0 | |
for line in file.read().splitlines(): | |
enabled = True | |
for i in range(len(line)): | |
j = i | |
if enabled: | |
if check(line, j, "mul("): | |
j += 4 | |
if line[j] in "0123456789": | |
j, num1 = read_num(line, j) | |
if check(line, j, ','): | |
j += 1 | |
j, num2 = read_num(line, j) | |
if check(line, j, ')'): | |
j += 1 | |
sum += num1 * num2 | |
elif check(line, j, "don't()"): | |
j += 7 | |
enabled = False | |
elif check(line, j, "do()"): | |
j += 4 | |
enabled = True | |
print(sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment