-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.py
More file actions
55 lines (43 loc) · 1.46 KB
/
Copy pathday5.py
File metadata and controls
55 lines (43 loc) · 1.46 KB
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
# answer 1: 635
# answer 2: submitted 369761800782619
fresh = []
fresh_ids = []
n_total_fresh_ids = 0
inventory = set()
overlaps = 1
# data preparation
with open('input/day5/input1.txt', 'r', encoding='utf-8') as file:
for txt_line in file:
line = txt_line.strip()
if line == '': continue # ignore the empty line(s)
id = line.split('-')
match len(id):
case 1: # no split -> id
inventory.add(int(id[0]))
case 2: # range of ids -> fresh stuff
fresh.append([int(id[0]),int(id[1])])
for id in inventory:
for area in fresh:
if area[0] <= id <= area[1]:
fresh_ids.append(id)
break
print(fresh_ids)
print(f"{len(fresh_ids)=}")
while overlaps > 0:
overlaps = 0
for idx, area in enumerate(fresh): # is wrong because I had chosen to ignore overlaps so far
for ix, checkarea in enumerate(fresh):
if idx == ix: continue # do not check against yourself
if checkarea[0] <= area[0] <= checkarea[1] or checkarea[0] <= area[1] <= checkarea[1]:
area[0] = min(area[0],checkarea[0])
area[1] = max(area[1],checkarea[1])
overlaps += 1
fresh[idx]=area
fresh.pop(ix)
break
else:
continue
break
for i in fresh:
n_total_fresh_ids += i[1]-i[0]+1
print(f"total fresh IDs: {n_total_fresh_ids=}")