-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
105 lines (93 loc) · 2.62 KB
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
from collections import defaultdict
from itertools import permutations, combinations
import functools
import itertools
import math
import re
import heapq
import time
from aoctools import dijkstra
from aoctools import get_input, submit_result
from math import *
day = 3
year = 2023
inp = get_input(day, year)
inp1 = """
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
"""
inp = inp.strip()
inp = inp.split('\n')
inp = [l.strip() for l in inp]
s = 0
numbers = []
symbols = {}
for r, l in enumerate(inp):
n = ""
ns = 0
for i, c in enumerate(l):
if c == '.':
if n:
numbers.append((int(n), (r, (ns, i-1))))
n = ""
elif c.isdigit():
if n == "":
ns = i
n = n + c
else:
if n:
numbers.append((int(n), (r, (ns, i-1))))
n = ""
symbols[(r, i)] = c
if n:
numbers.append((int(n), (r, (ns, i-1))))
n = ""
def part_1():
s = 0
for n, (r, pos) in numbers:
if (r-1, pos[0]-1) in symbols or (r-1, pos[0]) in symbols or (r-1, pos[0]+1) in symbols:
s += n
elif (r+1, pos[0]-1) in symbols or (r+1, pos[0]) in symbols or (r+1, pos[0]+1) in symbols:
s += n
elif (r-1, pos[1]-1) in symbols or (r-1, pos[1]) in symbols or (r-1, pos[1]+1) in symbols:
s += n
elif (r+1, pos[1]-1) in symbols or (r+1, pos[1]) in symbols or (r+1, pos[1]+1) in symbols:
s += n
elif (r,pos[0]-1) in symbols or (r, pos[1]+1) in symbols:
s += n
return s
def part_2():
s = 0
gears = defaultdict(list)
for n, (r, pos) in numbers:
glist = []
for nb in [ (r-1, pos[0]-1), (r-1, pos[0]), (r-1, pos[0]+1),
(r+1, pos[0]-1), (r+1, pos[0]), (r+1, pos[0]+1),
(r-1, pos[1]-1), (r-1, pos[1]), (r-1, pos[1]+1),
(r+1, pos[1]-1), (r+1, pos[1]), (r+1, pos[1]+1),
(r, pos[0]-1), (r, pos[1]+1) ]:
sym = symbols.get(nb)
if sym == '*':
if nb not in glist:
glist.append(nb)
for g in glist:
gears[g].append(n)
for g, ns in gears.items():
if len(ns) == 2:
s += ns[0]*ns[1]
return s
result_1 = part_1()
result_2 = part_2()
print("Part 1:", result_1)
print("Part 2:", result_2)
#submit_result(day, year, 1, result_1)
#submit_result(day, year, 2, result_2)