Add 2021, 2023
This commit is contained in:
parent
bd9480c6a7
commit
52771588ee
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
def main():
|
||||
with open("day3.sample", "r") as file:
|
||||
lines = file.readlines()
|
||||
|
||||
for line in lines:
|
||||
print(int(line.strip(), 2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,12 @@
|
|||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
numbers = {
|
||||
"one": "1",
|
||||
"two": "2",
|
||||
"three": "3",
|
||||
"four": "4",
|
||||
"five": "5",
|
||||
"six": "6",
|
||||
"seven": "7",
|
||||
"eight": "8",
|
||||
"nine": "9",
|
||||
}
|
||||
|
||||
|
||||
def get_first_and_last(line):
|
||||
for letter in line:
|
||||
if letter.isdigit():
|
||||
first = letter
|
||||
break
|
||||
for letter in line[::-1]:
|
||||
if letter.isdigit():
|
||||
last = letter
|
||||
break
|
||||
return first, last
|
||||
|
||||
|
||||
def find_first(line):
|
||||
first_position = len(line) + 1
|
||||
result = "0"
|
||||
for number in numbers:
|
||||
index = line.find(number)
|
||||
if index >= 0 and index < first_position:
|
||||
first_position = index
|
||||
result = numbers[number]
|
||||
for number in "123456789":
|
||||
index = line.find(number)
|
||||
if index >= 0 and index < first_position:
|
||||
first_position = index
|
||||
result = number
|
||||
return result
|
||||
|
||||
|
||||
def find_last(line):
|
||||
last_position = -1
|
||||
result = "0"
|
||||
for number in numbers:
|
||||
index = line.rfind(number)
|
||||
if index >= 0 and index > last_position:
|
||||
last_position = index
|
||||
result = numbers[number]
|
||||
for number in "123456789":
|
||||
index = line.rfind(number)
|
||||
if index >= 0 and index > last_position:
|
||||
last_position = index
|
||||
result = number
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
with open("day1", "r") as file:
|
||||
lines = file.readlines()
|
||||
total = 0
|
||||
for line in lines:
|
||||
first, last = get_first_and_last(line)
|
||||
total += int(first + last)
|
||||
|
||||
print(total)
|
||||
|
||||
new_total = 0
|
||||
for line in lines:
|
||||
first, last = find_first(line), find_last(line)
|
||||
new_total += int(first + last)
|
||||
|
||||
print(new_total)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
|
@ -0,0 +1,100 @@
|
|||
Game 1: 3 green, 1 blue, 3 red; 3 blue, 1 green, 3 red; 2 red, 12 green, 7 blue; 1 red, 4 blue, 5 green; 7 green, 2 blue, 2 red
|
||||
Game 2: 1 green, 19 blue, 1 red; 8 blue, 4 red; 3 red, 6 blue; 1 green, 1 red, 12 blue
|
||||
Game 3: 3 green, 1 blue, 9 red; 1 blue, 2 green, 8 red; 1 blue, 2 red
|
||||
Game 4: 6 green, 2 red; 2 red, 16 green; 3 red, 1 blue
|
||||
Game 5: 5 blue, 1 green; 3 blue, 3 green, 3 red; 8 red, 1 blue, 2 green; 7 blue, 6 red; 4 red, 4 blue
|
||||
Game 6: 5 red, 20 blue, 3 green; 4 red, 20 blue, 3 green; 12 blue, 3 green, 1 red; 3 red, 3 green, 19 blue
|
||||
Game 7: 5 red, 3 blue, 9 green; 12 red, 3 blue; 5 green, 3 blue, 19 red; 6 red, 1 green, 3 blue
|
||||
Game 8: 9 red; 2 green, 1 blue, 7 red; 5 red, 2 blue; 3 blue, 1 green; 1 green, 14 red, 1 blue; 3 blue, 4 red, 1 green
|
||||
Game 9: 11 red, 2 green; 13 red, 8 green; 15 green, 3 red; 1 blue, 9 red, 18 green; 2 green, 12 red; 15 green, 9 red
|
||||
Game 10: 1 green; 16 green, 3 red, 2 blue; 1 blue, 16 green, 4 red; 16 green, 5 red, 2 blue
|
||||
Game 11: 2 red, 18 blue, 5 green; 4 green, 12 blue, 9 red; 6 red, 4 green, 5 blue; 8 red, 16 blue, 2 green; 1 green, 18 blue, 13 red; 13 blue, 9 red
|
||||
Game 12: 5 red, 10 green, 4 blue; 8 green, 8 red, 14 blue; 10 green, 17 blue, 13 red; 7 blue, 9 red, 13 green; 6 red, 16 blue, 4 green; 16 blue, 14 red, 16 green
|
||||
Game 13: 6 green, 1 red, 1 blue; 10 blue, 15 green; 1 blue, 2 red, 5 green; 2 blue, 1 red, 20 green; 3 blue, 3 red, 10 green
|
||||
Game 14: 2 green, 2 blue; 2 green, 3 red, 4 blue; 8 red, 1 blue, 1 green
|
||||
Game 15: 3 blue, 10 green, 1 red; 16 red, 1 blue, 20 green; 7 green, 6 blue, 13 red; 8 green, 20 red, 5 blue; 8 blue, 8 red, 18 green; 17 green, 8 red, 10 blue
|
||||
Game 16: 6 blue, 5 red; 6 red, 16 blue, 11 green; 1 red, 3 green, 13 blue; 1 red, 5 green, 1 blue; 3 red, 14 green, 16 blue; 1 red, 1 green, 3 blue
|
||||
Game 17: 8 green, 5 red, 7 blue; 2 blue, 2 green, 6 red; 3 green, 4 blue, 15 red
|
||||
Game 18: 5 blue; 2 red, 9 blue, 3 green; 4 green, 20 blue, 2 red; 4 green, 2 red, 5 blue; 16 blue
|
||||
Game 19: 15 red, 1 blue; 3 green, 16 red, 4 blue; 1 blue, 3 green, 4 red; 9 red, 2 green, 6 blue; 2 green, 5 blue, 4 red
|
||||
Game 20: 12 red, 7 blue; 11 blue, 7 red, 1 green; 1 green, 10 red, 4 blue
|
||||
Game 21: 9 blue, 1 green, 1 red; 4 blue, 2 green; 1 blue, 2 red
|
||||
Game 22: 1 red, 10 green; 6 blue, 4 green, 1 red; 6 blue, 12 green, 1 red; 3 red, 4 blue, 10 green; 1 blue, 13 green, 1 red
|
||||
Game 23: 14 red, 2 blue, 3 green; 8 green, 2 blue, 4 red; 2 blue, 7 green, 4 red; 4 blue, 7 red; 1 blue, 8 green, 13 red
|
||||
Game 24: 1 blue, 6 green, 7 red; 6 green, 2 blue, 5 red; 1 blue, 3 green; 2 blue, 9 green; 1 green, 4 red; 5 green, 4 red
|
||||
Game 25: 8 red, 2 green, 6 blue; 3 blue, 15 red, 1 green; 8 blue, 2 red; 2 blue, 1 green; 2 green, 18 red, 1 blue
|
||||
Game 26: 9 red, 11 green, 6 blue; 1 blue, 2 red, 16 green; 15 green, 11 red, 6 blue; 3 red, 13 green, 6 blue; 20 red, 2 blue, 4 green
|
||||
Game 27: 9 red, 10 blue, 17 green; 8 green, 15 blue; 4 green, 3 red; 11 blue; 14 green, 1 blue, 8 red; 10 blue, 5 green, 3 red
|
||||
Game 28: 2 green, 17 red; 7 red, 6 green, 6 blue; 12 green, 16 red; 7 red, 7 green, 7 blue; 7 green, 8 red, 5 blue; 7 red, 5 blue
|
||||
Game 29: 2 red, 2 blue, 3 green; 3 blue, 1 red; 3 green, 2 blue, 1 red; 6 red, 1 green, 4 blue
|
||||
Game 30: 8 red, 15 blue, 4 green; 5 green, 9 red, 15 blue; 1 green, 1 blue, 11 red
|
||||
Game 31: 6 blue, 2 red, 1 green; 2 blue, 2 red, 8 green; 2 blue, 1 red, 7 green
|
||||
Game 32: 6 red, 7 green, 6 blue; 9 red, 6 blue, 6 green; 1 green, 13 red, 4 blue
|
||||
Game 33: 3 green, 1 blue, 9 red; 2 blue, 12 red, 4 green; 1 blue, 5 red, 1 green; 4 green, 5 red, 2 blue; 1 red, 2 blue, 3 green; 3 green, 3 red, 1 blue
|
||||
Game 34: 1 blue, 9 red; 3 blue, 4 red; 3 blue, 5 green, 10 red; 2 blue, 9 red, 5 green
|
||||
Game 35: 3 red, 2 blue; 1 green, 10 blue, 4 red; 1 blue, 5 red, 2 green; 5 blue, 2 green, 1 red
|
||||
Game 36: 9 green, 6 blue, 1 red; 16 blue, 8 green, 3 red; 9 green, 8 blue, 2 red; 3 green, 3 blue, 1 red; 16 blue, 3 red, 3 green
|
||||
Game 37: 1 green, 1 red; 2 blue, 3 green; 1 red, 1 blue, 5 green; 1 red, 9 green, 2 blue; 12 green, 2 blue
|
||||
Game 38: 16 blue, 12 red, 4 green; 15 blue, 5 green, 6 red; 7 red, 12 blue; 19 blue, 15 red, 1 green
|
||||
Game 39: 1 red, 2 blue; 1 green, 10 red, 3 blue; 1 green, 2 red; 1 blue, 3 red
|
||||
Game 40: 11 blue, 6 red, 3 green; 2 blue, 12 green, 1 red; 16 green, 5 red; 5 red, 10 green, 6 blue; 3 red, 13 green, 1 blue; 13 green, 3 blue, 7 red
|
||||
Game 41: 19 red, 1 blue; 9 blue, 6 red; 10 red, 1 green, 17 blue
|
||||
Game 42: 1 red, 8 green, 12 blue; 8 blue, 10 red, 12 green; 9 blue, 8 green, 9 red; 8 red, 11 green; 12 blue, 5 red, 2 green
|
||||
Game 43: 6 blue, 7 red, 9 green; 4 blue, 6 red; 3 red, 4 blue, 5 green; 7 green, 15 blue; 15 blue, 9 green, 6 red; 6 green, 8 red, 7 blue
|
||||
Game 44: 12 blue, 5 red; 7 red, 16 blue; 2 red, 4 blue, 8 green; 3 red, 10 blue, 3 green; 5 blue
|
||||
Game 45: 3 green, 4 red, 6 blue; 1 green, 2 red, 11 blue; 6 red, 9 blue, 1 green; 8 blue, 3 green
|
||||
Game 46: 1 blue, 9 green, 1 red; 1 blue, 2 red, 6 green; 10 green, 3 blue
|
||||
Game 47: 2 green, 4 red; 2 green, 4 blue, 2 red; 2 blue, 3 green, 12 red; 12 red, 3 blue
|
||||
Game 48: 4 blue, 3 green, 16 red; 1 green, 2 blue, 2 red; 9 green, 7 blue, 13 red
|
||||
Game 49: 4 blue, 5 green, 17 red; 1 blue, 13 red, 2 green; 15 red, 1 blue, 5 green; 4 blue, 7 green, 19 red; 4 blue, 3 green; 2 green, 2 red
|
||||
Game 50: 2 red, 3 green, 7 blue; 1 green, 9 blue, 1 red; 19 blue, 4 red; 1 green, 13 blue
|
||||
Game 51: 2 blue, 4 green, 14 red; 8 blue, 17 green, 7 red; 1 blue, 6 green, 19 red; 20 red, 17 green, 6 blue; 2 green, 1 red, 9 blue
|
||||
Game 52: 13 green, 17 blue, 2 red; 18 red, 12 blue, 10 green; 11 green, 17 red, 9 blue; 7 green, 11 red, 9 blue; 12 red, 15 blue; 7 green, 4 blue, 5 red
|
||||
Game 53: 2 green, 1 red, 3 blue; 1 red, 1 blue; 1 blue; 1 blue, 1 green, 1 red
|
||||
Game 54: 2 red, 5 green; 3 blue, 3 red, 2 green; 1 blue, 3 red, 5 green
|
||||
Game 55: 7 green, 5 blue, 4 red; 8 blue, 7 red, 8 green; 12 blue, 2 red, 16 green; 3 green, 8 blue
|
||||
Game 56: 9 green, 2 red, 1 blue; 1 blue, 11 red, 3 green; 9 red, 1 blue, 8 green; 10 red, 16 green
|
||||
Game 57: 1 red, 5 blue, 9 green; 19 blue, 2 green, 5 red; 15 green, 3 red, 7 blue; 2 blue, 15 green, 9 red; 5 red, 9 green, 15 blue
|
||||
Game 58: 5 green, 1 blue; 3 red, 2 blue; 2 blue, 1 red, 12 green; 8 green; 12 green, 2 blue; 4 green, 4 red
|
||||
Game 59: 11 blue, 5 red, 4 green; 6 red, 1 green, 3 blue; 7 red, 10 blue, 4 green; 12 blue, 1 red, 1 green
|
||||
Game 60: 3 green, 10 red, 10 blue; 10 green, 6 blue, 10 red; 1 blue, 6 green, 7 red; 3 red; 8 blue, 7 green, 8 red; 3 red, 19 green
|
||||
Game 61: 11 green, 3 blue, 20 red; 3 green, 3 blue, 20 red; 10 green, 12 red, 8 blue; 4 green, 8 blue, 6 red; 7 blue, 10 red, 5 green; 6 green, 6 red
|
||||
Game 62: 10 green, 9 red; 2 green, 2 blue, 5 red; 4 blue, 11 green, 12 red
|
||||
Game 63: 5 blue, 4 green, 2 red; 5 blue, 3 red, 2 green; 6 blue, 2 green, 2 red; 1 red, 5 blue; 1 green, 3 blue
|
||||
Game 64: 5 blue, 4 green, 8 red; 8 blue, 12 red, 10 green; 8 red, 7 blue; 7 green, 7 red; 5 blue, 1 red, 2 green
|
||||
Game 65: 3 blue, 3 red, 15 green; 12 green, 3 blue, 12 red; 13 green, 6 red, 2 blue; 1 red, 7 blue, 3 green; 9 red, 5 green, 7 blue; 1 green, 5 blue, 9 red
|
||||
Game 66: 1 green, 6 blue; 7 blue, 8 green; 2 blue, 9 red, 14 green
|
||||
Game 67: 1 blue, 8 red, 1 green; 7 red, 10 green, 4 blue; 3 blue, 1 red, 4 green
|
||||
Game 68: 8 blue, 8 green, 10 red; 4 red, 5 green; 4 blue, 12 red, 15 green
|
||||
Game 69: 2 red, 3 blue, 2 green; 1 blue, 15 green, 4 red; 15 red, 20 green; 8 red, 4 green
|
||||
Game 70: 6 red, 4 blue, 10 green; 5 blue, 6 red, 16 green; 9 green, 1 red, 1 blue; 2 blue, 6 green; 1 green, 3 blue, 5 red
|
||||
Game 71: 9 red, 9 green, 4 blue; 1 blue, 5 green; 4 red, 2 blue, 5 green; 1 blue, 3 red, 2 green
|
||||
Game 72: 14 blue, 1 red, 4 green; 18 blue, 1 red, 3 green; 1 red, 1 green, 10 blue
|
||||
Game 73: 7 red, 6 green, 1 blue; 14 green, 1 blue, 4 red; 7 red, 18 green; 1 red, 5 green
|
||||
Game 74: 9 green; 1 red, 7 blue, 4 green; 10 blue
|
||||
Game 75: 4 red, 1 green; 1 green, 4 red, 2 blue; 3 green, 2 red, 7 blue
|
||||
Game 76: 16 green, 7 blue, 1 red; 2 blue, 6 red, 2 green; 7 blue, 17 green; 5 red, 15 blue, 15 green
|
||||
Game 77: 1 red, 7 blue, 8 green; 1 red, 6 blue, 5 green; 1 red, 5 blue, 4 green; 8 green, 1 blue; 2 blue
|
||||
Game 78: 9 green, 3 blue; 6 red, 12 green; 5 red, 3 blue, 10 green; 3 blue, 14 green, 13 red
|
||||
Game 79: 20 green, 1 blue, 3 red; 11 green, 4 red, 2 blue; 11 red, 1 blue, 5 green
|
||||
Game 80: 14 red; 3 green, 2 blue, 7 red; 1 blue, 6 red
|
||||
Game 81: 1 red; 11 blue; 11 blue; 9 blue, 5 green, 1 red
|
||||
Game 82: 13 red, 17 blue, 9 green; 1 blue, 2 green; 9 red, 5 green, 6 blue; 10 green, 14 blue, 14 red; 5 green, 2 blue, 10 red; 4 blue, 4 green, 2 red
|
||||
Game 83: 6 blue, 3 red, 5 green; 3 blue, 6 green; 13 red, 11 green, 1 blue; 7 blue, 1 green, 14 red; 9 green, 2 blue, 3 red; 8 green, 3 red, 2 blue
|
||||
Game 84: 5 green, 8 blue; 7 red, 7 blue, 10 green; 7 blue, 7 green, 7 red; 7 blue, 1 green, 11 red
|
||||
Game 85: 12 blue, 1 red, 2 green; 3 green, 13 red; 17 red, 1 blue, 2 green; 4 blue, 15 red; 9 blue, 7 red; 2 green, 11 red, 4 blue
|
||||
Game 86: 15 green, 1 blue, 8 red; 1 blue, 18 green, 3 red; 3 red, 1 blue, 16 green
|
||||
Game 87: 9 red, 17 blue, 9 green; 4 green, 6 red, 2 blue; 6 red, 5 blue
|
||||
Game 88: 8 red, 6 blue, 17 green; 17 green, 5 blue, 12 red; 2 red, 14 green, 1 blue
|
||||
Game 89: 14 red, 5 blue, 6 green; 1 blue, 6 green; 4 red, 9 green, 8 blue; 2 blue, 4 red, 11 green; 12 red, 1 green, 8 blue; 3 blue, 2 green, 5 red
|
||||
Game 90: 3 red, 3 blue; 14 green, 8 blue; 4 red, 12 green, 8 blue
|
||||
Game 91: 2 blue; 2 blue, 8 red; 4 red; 8 red, 1 blue; 1 green, 2 blue
|
||||
Game 92: 16 green, 16 red; 5 green, 2 blue; 14 red, 16 green; 17 red, 1 blue, 12 green
|
||||
Game 93: 9 blue, 14 red, 6 green; 2 blue, 6 red, 3 green; 1 green, 2 blue, 12 red; 6 green, 8 red, 5 blue; 5 blue, 9 green, 10 red; 7 blue, 10 green, 3 red
|
||||
Game 94: 2 blue, 13 green, 7 red; 5 red, 2 blue, 14 green; 8 red, 9 green; 2 blue, 8 green, 1 red; 7 red, 12 green; 2 blue, 3 green
|
||||
Game 95: 1 red, 8 blue, 4 green; 1 green, 3 blue, 2 red; 6 blue, 2 red, 1 green; 3 blue, 4 green; 3 green, 1 red
|
||||
Game 96: 15 blue, 8 red, 5 green; 15 green, 16 blue, 4 red; 11 blue, 8 red; 16 blue, 6 green, 1 red; 10 blue, 9 red; 1 red, 3 green, 3 blue
|
||||
Game 97: 11 green, 8 blue, 4 red; 12 green, 11 blue, 1 red; 4 red, 1 blue, 11 green; 6 green, 1 red, 7 blue; 5 blue, 12 green, 4 red; 5 blue, 8 green
|
||||
Game 98: 4 green, 15 blue; 13 blue, 8 green; 10 blue, 6 green; 1 red, 7 green
|
||||
Game 99: 1 green, 3 blue, 18 red; 8 blue, 19 red, 5 green; 7 red, 2 blue, 2 green; 10 red, 1 blue, 2 green
|
||||
Game 100: 4 red, 3 green, 4 blue; 8 green, 5 red, 2 blue; 1 red, 2 blue, 7 green; 3 blue, 8 green, 5 red
|
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/env python
|
||||
limits = {"red": 12, "green": 13, "blue": 14}
|
||||
|
||||
|
||||
def game_possible(games):
|
||||
games_list = [pair.strip().split(",") for pair in games.split(";")]
|
||||
is_possible = True
|
||||
for game in games_list:
|
||||
pairs = {pair.split()[1]: int(pair.split()[0]) for pair in game}
|
||||
if not possible(pairs):
|
||||
is_possible = False
|
||||
return is_possible
|
||||
|
||||
|
||||
def possible(totals):
|
||||
is_possible = True
|
||||
for color, limit in limits.items():
|
||||
try:
|
||||
if totals[color] > limit:
|
||||
is_possible = False
|
||||
except KeyError:
|
||||
pass
|
||||
return is_possible
|
||||
|
||||
|
||||
def get_power_of_fewest(games):
|
||||
highest_power = {"red": 0, "green": 0, "blue": 0}
|
||||
games_list = [pair.strip().split(",") for pair in games.split(";")]
|
||||
for game in games_list:
|
||||
pairs = {pair.split()[1]: int(pair.split()[0]) for pair in game}
|
||||
for color, count in pairs.items():
|
||||
if highest_power[color] < count:
|
||||
highest_power[color] = count
|
||||
result = 1
|
||||
for number in highest_power.values():
|
||||
if number:
|
||||
result *= number
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
with open("day2", "r") as file:
|
||||
lines = file.readlines()
|
||||
|
||||
sum_ids = 0
|
||||
for line in lines:
|
||||
game, games = line.split(":")
|
||||
game_id = game.split()[-1]
|
||||
if game_possible(games):
|
||||
sum_ids += int(game_id)
|
||||
print(sum_ids)
|
||||
|
||||
total_powers = 0
|
||||
for line in lines:
|
||||
_, games = line.split(":")
|
||||
total_powers += get_power_of_fewest(games)
|
||||
|
||||
print(total_powers)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,5 @@
|
|||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
|
@ -0,0 +1,140 @@
|
|||
.......358..........31.....339.....669.............598......328.....575......................447..650..............964...........692........
|
||||
...............415..*.........@......*...627*...................945*.............144/.506............................*......514...*...150...
|
||||
.........182..+.....873.756.......737........784..568....667..............258........./.........741...........707*....84........520.........
|
||||
..579...@.........$........*..........258.................*.........274..*.....739.......157./.....*580...........893.....696*........889...
|
||||
.....*.........875.........173.........*..................109...896*......959...*........=...677...........&636.........................$...
|
||||
......961..$............-...............907...569....#...........................756.....................................681................
|
||||
...........478...616.....30.....221*...............552.../33...258*343.....488........*682...526..*.........422...........&..........764....
|
||||
................+..........................................................*................*.....7...550..........=.........878............
|
||||
...171..275............#.....................401....50.$.................43...............123.............../949..135..331.....@.495........
|
||||
........*...48@.662.100...............590...*........#.566.....................15..../426.............774...............+...........*.......
|
||||
...944.28.........*.....................*....307...........411.361....55..................24.447.*......*....787*...266....361.......161....
|
||||
....*...........405.&...=785...146......654.......281.............*.....+...................*....760..80..17.....84....*..+.................
|
||||
.....830...140......745.........%..$..........972.......406*864..762..............695.327.................*...........512...................
|
||||
............*...............#......414...........*..960...............1..............*.................374.....264.............833....336...
|
||||
.......-.252.......10....518..................777...=...811..103.454..*....................774*433.........951*....826........@....=...*....
|
||||
.....44..............*................-....................@..$......957...............216..........=..................614........521..232..
|
||||
.........=232......802...+779.....759..307....................................%941.......*........512.............865.....*.................
|
||||
...................................*.......162...662....816.681.....21*................290.........................*......364..........=....
|
||||
.................258....195.........1..535......*.......*.....*........261............................650.......152.................514.....
|
||||
..425...411@...../.........@...........*.....788.........186...91.279............-.....................*............268.......639.......864.
|
||||
....................................=.933........../.................%...%........73........%203....187..362...861.....*389.....*.......*...
|
||||
...254....847............@........135......6.......529..........202......96...........840.........@........*...*.../.........998.......989..
|
||||
.........*.....288......650............349*.......................%.609...................526.....655.....854....425..............531*......
|
||||
..../....848....&..............505...........................594..........434.................@.......$68............767..............732...
|
||||
.410...............585...............185........................*...91........773..579........454............757.....*....568..+742.........
|
||||
..............................*70.......*......478............769....&.182.......%.*..................568......-.570.861....*...............
|
||||
..............*993.48......893........835....*..*......*.................*..........798..473.........../....................965...691.......
|
||||
...........306.......*............531.......938.200.335.432...343.....360......836......-...........*..........951.691..............*.......
|
||||
...341*360............958............*.........................#.........../.....*.../........728..585.........*.............902=..696......
|
||||
.............190...................808..451........+..................272...59.641...822.........*..........289...36...................509..
|
||||
..464*10.604.+....241.102%..............*...633.450......................+........................797............*.......575..........*.....
|
||||
...........-............................488..........402.....936.847................+.......434.......................&.....&....844...478..
|
||||
......147..............826...297................257...*...........*.....$.....932...492..81...%....829..........560...661.........*.........
|
||||
.........*....379.......*....*...884..700.......*.....646..........914..659...............*....................=..........513/..621..790....
|
||||
.369....472..*.......843....734....#.+....%...738........................................989............501.......817.375...............@...
|
||||
..............727.........................131.....767..........=......665...685..............747..727.......868..*.............478#.........
|
||||
........247.............-...........649.............*..*82..507...............*..................................504........................
|
||||
.........*...........909...............*.....707..922..............701...213.833.866$.630...&..606+....................102.183@..*281.225...
|
||||
...465.108...............772.....915.109..96.%...................../.....*.............*..685........155....811*...220.*...............*....
|
||||
.................106.....=.................&........*........*488.........233.......590........$551.....*..........+...287.#874..%....956...
|
||||
....................*513.........&...............829.487................................350..-........338.......55................464.......
|
||||
....&............@............695......227................$.793.............802.....294...*...822..........960....*....295..946.............
|
||||
....587...........859...204.......246*....*644.........925.........167.210..../..........129.............%....*...102.....*..*..........832.
|
||||
.......................*..............558.......404.......................%..........................817..870.791......120..483..406...$....
|
||||
....797%..68....929-.87....411............*........*.............458*143....721.-....706.......694....*.........................+...........
|
||||
...........*..................*........369..........899..79................*....417.....*930..*........78.....@174.........276..............
|
||||
.313........394...750..........773.........................................916......37+.......384...........-......355.203....*.....180.....
|
||||
....@...902...........829.......................................*823........................*.............408.......*......960......*.......
|
||||
........*......%........#...........@871..........756.........22.........956..............56.622.=461.823..........749...........297..557...
|
||||
......877.......863..............................*.........................=.968..............................12.......946%..........%......
|
||||
..............................123.39.551...12...700..507*....................*........320.........*...54....................................
|
||||
.............583........424..................$...........571......#.......499.......-.............803......92.&......942..........782.......
|
||||
....726.....$...............=....688.580...*...........#..........905............446....397...................629...*............*..........
|
||||
.....*..758.......831........760..-....*.427......473...729............298...618........-.........562*865.........553....267...12...........
|
||||
.................$.....971...........472.....61....#..................*.........@..714.......712............173.............*......-780.....
|
||||
.....*948..........&.....*..750.311..................*425.193....@336..882........*..........*....913.........*...........369...............
|
||||
..942..............209.791....-....%.626.787..812.954......................725/.806.%...............#.......447....679..........706...11....
|
||||
...............922......................*................217@......962..............9.104...806...................%.....278*.....*..........
|
||||
.........382......*........./.......346.......346....448............*....................$...................*................820..454%.....
|
||||
.315......./....418.........748................%...............577..655.......*254..266.....383*...$......316.243.........*.................
|
||||
.....................363................709.....................=..........714..........224......479...................817.428....../.......
|
||||
......................*................*........318................237....................*...................497.152............457........
|
||||
...........911........756..589..........562......=..253........8*................498...392.......394.768...@................................
|
||||
...............$..751..........856...................*...56......202...201.........+................*.......525.......430...................
|
||||
.193........953...*.....456..@................310*........*............../.58@.........*..............863..........$....%..675..+117...447..
|
||||
.......940.................*.905.889......542.....723......591....*956.........743.$..156.........791*......923..84......../..........$.....
|
||||
.........&.....538&......643......*..=......*..................659............@....66.....681...............................................
|
||||
.................................127.585.238.......-866....654......................................393................80..19..979..........
|
||||
.........................300.................102........................465*..............705.332......................%.........*.....*142.
|
||||
......+....#.....*...500..%..336.419........*....+116.719...........237.....81...461*931.....*.........613.#426...................434.......
|
||||
......489.420.459......*......*....*.....277.............%.........=...............................*..............746....512............%848
|
||||
.......................804....445.197.........658.*342....................+461.630......%...930@....804.504........*.............+..299.....
|
||||
...........270.....257........................./.........283....................*.......357.......................702.....644.587......*....
|
||||
...460%...=...........*.......406.........162..............*...259.......*.......121.............360$.#...76..228.....847..*............666.
|
||||
.....................612..178*.............*...527*872.762.267........307.606.........#........$......224....&....13*....-.455.......&......
|
||||
..........152.......................919..485............/.........70.................623....968......................810............281.230.
|
||||
......693.*...520............$......*...........*909.....................586.........................794......229.........88...385..........
|
||||
..363*....636..*......619.....238....45..............982...........*145.....*803......-.......426.......#.....+.......213*...#....*.........
|
||||
...............536.....+........................&.......*.......708.....170......360..220......*../.......16................796....283..564.
|
||||
.652....&...........$.....*....................89...944.894................*....-............975.27...409*...978......./445.................
|
||||
...*.538...........822.274.491.....-..........................572.........329.........*468......................&.......................213.
|
||||
.47......568.......................706.....232....74..490............&965..........685................125...........................824*....
|
||||
........*....725...........#548............=......*.........................................77*391...*........253...............767.........
|
||||
.......769...&......................183........687..921......913$...@.....162...724.................220.........*...880.834.........404.....
|
||||
...+.................466...%411...../...............*...............894....*.......*.............*............196.....*................*....
|
||||
.90.......423..../......*.......1.........-.......360.....233...............2........=......@....422.....334.........878......322......573..
|
||||
....649..........475....450....%......-....249.................905.............760..879......829..........-....................*............
|
||||
......*......290.....................680............/............/.244&.........................................9.596.326.........359.......
|
||||
...............*........200.....723................826....773...........576..790..=..............642.................*....617....*.....649..
|
||||
.....#979....776..562.......@.....*........................#........363.....&....915..704....401....*...32*681.%261.......&.......351.......
|
||||
896*.................*896...493.88....*.........622.............925*.................*....17*.......265..............893....................
|
||||
....778.%222.......................873.872..........635@...852..........559........456..%......*128........179.......*............336*......
|
||||
.................985.......................77...482.........-.......502*......404......206...41......631..*.........761...............782...
|
||||
.....194...........&...............514*130..=..*.............................*...........................168.............../..46+...........
|
||||
.............207...............................195.....619..$..........544..191.......419*842....64......................206..........816...
|
||||
..............................670..84*78.................*.966......%.....*.....................*........=............................*.....
|
||||
.564.........185-........250....*........464...762.....412.......302.....27..764.653/.738*596..........563....741.........13.148.......928..
|
||||
....*.............%..565....%.60....+158..*...$............*503...................................824.................284.......*.931-......
|
||||
....710...763...272..*...................851..../....................456.......121....603................3..445...............382.......837.
|
||||
....................956..970........-..........377......381.......56.......926........../......135......*....../.........315........172.&...
|
||||
........201.............+...........261.517..................606........../.....347......................655.......717........468..*........
|
||||
...746...&...332...............681...............703...........*.....=............*...........................................*....824......
|
||||
......-.........*478.............*.....595.245....*...918.....875...875.........965...........676...745*...858...479.......132..............
|
||||
..354........*.................776.-...*...*...820...../..%..............291........../........*........59.*........*....................485
|
||||
...&......668.956......738.........921.312.803.....770...613....78*522..&...........665......692............253......863............537.....
|
||||
...................498......258...................................................................426.........................#.............
|
||||
.....108..........&.....357.-......794..594#............#............255........$.........664.525./....885...$..............353..823........
|
||||
........+...590..........*........*...........111.......420..374..............144...........*...*...80*.....164...928...427......&..........
|
||||
...+...........*..289.185......$...659.772/....*.............*........361..........504.........436...................*......@...............
|
||||
...14....*...296...*........453..............753.368.733...167..675....*....................................22........502..443..............
|
||||
.......413........................200=..616.......+...*...........#.....143..768..........404.............@...=...102..............378......
|
||||
...538..........579.......................=...........311.267...................*.....445...*.....%138..158............#...........*....323.
|
||||
......$............/.................586%.........825.....*....................318......@...260.................618..229...865....353...*...
|
||||
.............834.......68*981................606..*.....432............870...............................749....*...........#.........215...
|
||||
......886....*...325...................18.25*.....977...................../.......385.........$.......95*......182./........................
|
||||
............773........................*................165...+....+816........*...*...........391..................439............@........
|
||||
........................682..........249...................*.435............400...261..119.....................................921..630.....
|
||||
.......-.......92*744....*....................115.......780........205....................*...347.152......950..592&.152....@.....*.........
|
||||
.....789.949..............530.248.149.............................*....*957............957.....&..*.......#..........#.....572..............
|
||||
...........*.601.766..112.......*......113..@.....529...736.+...656.837........320...............518.120.........125..............900.......
|
||||
83......434.....*.....*.../...224.....*....235..............498............175...*.930.......................+......*................*880...
|
||||
...*296...............118.328........405.............../801................*...804....*.......................590...770.........276.........
|
||||
297..............726.............................966............*997......165..........25.983............669.............&..992*.......966..
|
||||
...........92....*........%.........108.............@.475....769..................897......@.........792................123.......122.......
|
||||
...........*...&..579......811..708...&.................*........................*....767.......386...........558............708.....=......
|
||||
........230..161...............*................#....177.........940...903./...706.......*785..=.....93..381..%.................*...........
|
||||
...621.............652..886...157........$....204............418....*....%..62......422.............*.....=........555.537.............+479.
|
||||
.....%............*.....*.............499............96.........*....656..............*...751..293...759.....174..........%..265..32%.......
|
||||
...............610....105.416.........................%........242.........*.......746.......*....=...........*..259@.......................
|
||||
............#................*591........./.-.......................653.103..62=.............585.............567...........586...+....216...
|
||||
..840/....612.......588.................676..202...................&................@..............................301............632..*....
|
||||
......................*.........=392.............667..%..........-...............457...134*....................165*.....................728.
|
||||
.339&.*74.........402.581............518&.......*....823.....874..102..678.74..............219....114..................836..915..245.-......
|
||||
..........38.....*........612...628..............90......513...*............*...........59......+..../....799..268....*.....*.........370...
|
||||
..........$........./........*.....*.......$...................8.......684=.577..209.........550.............#.....#...529.240..............
|
||||
.236..............153......163........*.414...........................................549........432............919............81.....-337..
|
||||
....*.................604......-...631..........879........240.......97...............*.....315....&...720..........610...530...............
|
||||
.....856...214..236....*.....159.%......738.....-......826....&.272.*.......36.....465.........../.....*...587.......*....*......548..699...
|
||||
.............*........36..........743.=.../...............*......*..424.................580.#...897.448....*.......833...633.....*...*......
|
||||
.............963......................542........734.....901...914..........843.............523..........818..................691.....833...
|
|
@ -0,0 +1,94 @@
|
|||
#!/usr/bin/env python
|
||||
import re
|
||||
|
||||
|
||||
def do_last(index, lines):
|
||||
pattern = r"\d+"
|
||||
symbol_pattern = r"[^\d.\n]"
|
||||
matches = [
|
||||
(match.start(), match.end()) for match in re.finditer(pattern, lines[index])
|
||||
]
|
||||
numbers = re.findall(pattern, lines[index])
|
||||
|
||||
symbol_indexes = set()
|
||||
for row in range(index-1, index+1):
|
||||
for match in re.finditer(symbol_pattern, lines[row]):
|
||||
symbol_indexes.add(match.start())
|
||||
|
||||
result = []
|
||||
for index, match in enumerate(matches):
|
||||
is_symbol_adjacent = False
|
||||
for number_index in range(match[0]-1, match[1]+1):
|
||||
if number_index in symbol_indexes:
|
||||
is_symbol_adjacent = True
|
||||
if is_symbol_adjacent:
|
||||
result.append(int(numbers[index]))
|
||||
return sum(result)
|
||||
|
||||
|
||||
def do_first(index, lines):
|
||||
pattern = r"\d+"
|
||||
symbol_pattern = r"[^\d.\n]"
|
||||
matches = [
|
||||
(match.start(), match.end()) for match in re.finditer(pattern, lines[index])
|
||||
]
|
||||
numbers = re.findall(pattern, lines[index])
|
||||
|
||||
symbol_indexes = set()
|
||||
for row in range(index, index+2):
|
||||
for match in re.finditer(symbol_pattern, lines[row]):
|
||||
symbol_indexes.add(match.start())
|
||||
|
||||
result = []
|
||||
for index, match in enumerate(matches):
|
||||
is_symbol_adjacent = False
|
||||
for number_index in range(match[0]-1, match[1]+1):
|
||||
if number_index in symbol_indexes:
|
||||
is_symbol_adjacent = True
|
||||
if is_symbol_adjacent:
|
||||
result.append(int(numbers[index]))
|
||||
return sum(result)
|
||||
|
||||
|
||||
def find_adjacent(index, lines):
|
||||
pattern = r"\d+"
|
||||
symbol_pattern = r"[^\d.\n]"
|
||||
matches = [
|
||||
(match.start(), match.end()) for match in re.finditer(pattern, lines[index])
|
||||
]
|
||||
numbers = re.findall(pattern, lines[index])
|
||||
|
||||
symbol_indexes = set()
|
||||
for row in range(index-1, index+2):
|
||||
for match in re.finditer(symbol_pattern, lines[row]):
|
||||
symbol_indexes.add(match.start())
|
||||
|
||||
result = []
|
||||
for index, match in enumerate(matches):
|
||||
is_symbol_adjacent = False
|
||||
for number_index in range(match[0]-1, match[1]+1):
|
||||
if number_index in symbol_indexes:
|
||||
is_symbol_adjacent = True
|
||||
if is_symbol_adjacent:
|
||||
result.append(int(numbers[index]))
|
||||
|
||||
return sum(result)
|
||||
|
||||
|
||||
def main():
|
||||
with open("day3", "r") as file:
|
||||
lines = file.readlines()
|
||||
|
||||
total_adjacent = 0
|
||||
for index in range(len(lines)):
|
||||
if index == 0:
|
||||
total_adjacent += do_first(index, lines)
|
||||
elif index == len(lines) - 1:
|
||||
total_adjacent += do_last(index, lines)
|
||||
else:
|
||||
total_adjacent += find_adjacent(index, lines)
|
||||
print(total_adjacent)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,10 @@
|
|||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
def main():
|
||||
with open("day3", "r") as file:
|
||||
lines = file.readlines()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue