Skip to content

Commit

Permalink
Solving day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardy committed Dec 7, 2022
1 parent 4b1972c commit 0579a55
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ dmypy.json

# Pyre type checker
.pyre/
**/problem.txt
problem.txt
47 changes: 47 additions & 0 deletions code/day_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from helper import *

# readString or readNumbers
data_example = readStrings("./days/day_4/example.txt")
data = readStrings("./days/day_4/problem.txt")

def part1(data):
sum = 0
for i, item in enumerate(data):
item = item.split(",")
l1 = int(item[0].split("-")[0])
l2 = int(item[0].split("-")[1])
r1 = int(item[1].split("-")[0])
r2 = int(item[1].split("-")[1])

if l1 <= r1 and l2 >= r2:
sum += 1
elif r1 <= l1 and r2 >= l2:
sum += 1
return sum


def part2(data):
sum = 0
for i, item in enumerate(data):
item = item.split(",")
l1 = int(item[0].split("-")[0])
l2 = int(item[0].split("-")[1])
r1 = int(item[1].split("-")[0])
r2 = int(item[1].split("-")[1])

if l1 <= r1 and l2 >= r2:
sum += 1
elif r1 <= l1 and r2 >= l2:
sum += 1
elif l1 <= r1 and l2 >= r1:
sum += 1
elif l1 <= r2 and l1 >= r1:
sum += 1
elif r1 <= l1 and r2 >= l1:
sum += 1
elif r1 <= l2 and r1 >= l1:
sum += 1
return sum

print(part1(data_example), part2(data_example))
print(part1(data), part2(data))
6 changes: 6 additions & 0 deletions days/day_4/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

0 comments on commit 0579a55

Please sign in to comment.