mirror of
https://github.com/RussellChamp/advent-of-code-2022.git
synced 2026-10-27 21:52:30 +00:00
day04 solution
This commit is contained in:
1 parent
0716f6e4e9
commit
312c9107e3
5 files changed
+92
-3
No files matched your search
+2
-1
@@ -1 +1,2 @@
|
||||
data.txt
|
||||
data.txt
|
||||
*.pyc
|
||||
Binary file not shown.
@@ -0,0 +1,46 @@
|
||||
# Day 04: Camp Cleanup
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
def get_path():
|
||||
return os.path.join(os.path.dirname(__file__), 'data.txt')
|
||||
|
||||
def fully_contains(a, b, c, d):
|
||||
# if the first item is contained in the second
|
||||
# or the second item is contained in the first
|
||||
return (a >= c and b <= d) or (c >= a and d <= b)
|
||||
|
||||
def any_overlap(a,b,c,d):
|
||||
# if the first item contains any overlap on the second item
|
||||
# if either the upper or lower of the first item exist within the second item
|
||||
# or the upper or lower of the second item exist within the first item
|
||||
return (a >= c and a <= d) or (b >= c and b <= d) \
|
||||
or (c >= a and c <= b) or (d >= a and d <= b)
|
||||
|
||||
def solve_part1():
|
||||
total = 0
|
||||
|
||||
with open(get_path()) as fp:
|
||||
for line in fp:
|
||||
[first_lower, first_upper, second_lower, second_upper] = map(int, re.split(r',|-', line))
|
||||
if fully_contains(first_lower, first_upper, second_lower, second_upper):
|
||||
total += 1
|
||||
|
||||
print('ANSWER: In how many assignment pairs does one range fully contain the other?')
|
||||
print('Total: %d' % (total))
|
||||
def solve_part2():
|
||||
total = 0
|
||||
|
||||
with open(get_path()) as fp:
|
||||
for line in fp:
|
||||
[first_lower, first_upper, second_lower, second_upper] = map(int, re.split(r',|-', line))
|
||||
if any_overlap(first_lower, first_upper, second_lower, second_upper):
|
||||
total += 1
|
||||
|
||||
print('ANSWER: In how many assignment pairs do the ranges overlap?')
|
||||
print('Total: %d' % (total))
|
||||
|
||||
if __name__ == '__main__':
|
||||
solve_part1()
|
||||
solve_part2()
|
||||
@@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
import solve
|
||||
|
||||
class Day04TestMethods(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_fully_contains(self):
|
||||
test_cases = [
|
||||
[True, 1,2,1,2],
|
||||
[True, 1,100,5,50],
|
||||
[True, 5,50,1,100],
|
||||
[False, 1,2,3,4],
|
||||
[False, 1,100,50,500],
|
||||
[False, 2,4,6,8],
|
||||
[False, 2,3,4,5],
|
||||
[False, 5,7,7,9],
|
||||
[True, 2,8,3,7],
|
||||
[True, 6,6,4,6],
|
||||
[False, 2,6,4,8]
|
||||
]
|
||||
for [expected, a, b, c, d] in test_cases:
|
||||
self.assertEqual(solve.fully_contains(a, b, c, d), expected)
|
||||
|
||||
def test_any_overlap(self):
|
||||
test_cases = [
|
||||
[False, 2,4,6,8],
|
||||
[False, 2,3,4,5],
|
||||
[True, 5,7,7,9],
|
||||
[True, 2,8,3,7],
|
||||
[True, 6,6,4,6],
|
||||
[True, 2,6,4,8]
|
||||
]
|
||||
for [expected, a, b, c, d] in test_cases:
|
||||
self.assertEqual(solve.any_overlap(a, b, c, d), expected, 'overlap of %d-%d and %d-%d should be %s' % (a, b, c, d, expected))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+6
-2
@@ -8,14 +8,18 @@ def get_path():
|
||||
def solve_part1():
|
||||
total = 0
|
||||
|
||||
with open(get_path()) as fp:
|
||||
for line in fp:
|
||||
print(line)
|
||||
|
||||
print('GOAL: #GOAL1#')
|
||||
print('ANSWER: #ANSWER1#)
|
||||
print('ANSWER: #ANSWER1#')
|
||||
print('Total: %d' % (total))
|
||||
def solve_part2():
|
||||
total = 0
|
||||
|
||||
print('GOAL: #GOAL2#')
|
||||
print('ANSWER: #ANSWER2#)
|
||||
print('ANSWER: #ANSWER2#')
|
||||
print('Total: %d' % (total))
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user