Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12fe606

Browse files
author
mhamid
committed
day 4
1 parent ca3e11d commit 12fe606

File tree

3 files changed

+1032
-0
lines changed

3 files changed

+1032
-0
lines changed

‎day4/day4.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# read input
2+
input = open('input.txt', 'r')
3+
lines = input.readlines()
4+
sectionOverlapIds = 0
5+
someOverlapIds = 0
6+
7+
def firstToLast(str):
8+
list = str.split('-')
9+
return [x for x in range(int(list[0]), int(list[1]) + 1)]
10+
11+
for line in lines:
12+
line = line.split('\n')[0]
13+
line = line.split(',')
14+
firstStr = line[0]
15+
secondStr = line[1]
16+
firstList = firstToLast(firstStr)
17+
secondList = firstToLast(secondStr)
18+
if (all(item in firstList for item in secondList) or all(item in secondList for item in firstList)):
19+
sectionOverlapIds = sectionOverlapIds + 1
20+
if (any(item in firstList for item in secondList) or any(item in secondList for item in firstList)):
21+
someOverlapIds = someOverlapIds + 1
22+
23+
# part one
24+
print('Total overlapping section IDs: ', sectionOverlapIds)
25+
# part two
26+
print('Some overlapping section IDs: ', someOverlapIds)

‎day4/dummy.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2-4,6-8
2+
2-3,4-5
3+
5-7,7-9
4+
2-8,3-7
5+
6-6,4-6
6+
2-6,4-8

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /