diff --git a/.gitignore b/.gitignore index 4669fe9..0332342 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -data.txt \ No newline at end of file +data.txt +*.pyc \ No newline at end of file diff --git a/day02/solve.pyc b/day02/solve.pyc deleted file mode 100644 index 1cef8e9..0000000 Binary files a/day02/solve.pyc and /dev/null differ diff --git a/day04/solve.py b/day04/solve.py new file mode 100644 index 0000000..e356e24 --- /dev/null +++ b/day04/solve.py @@ -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() \ No newline at end of file diff --git a/day04/tests.py b/day04/tests.py new file mode 100644 index 0000000..f52ce08 --- /dev/null +++ b/day04/tests.py @@ -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() \ No newline at end of file diff --git a/solve.py.template b/solve.py.template index 42d47b2..0c56228 100644 --- a/solve.py.template +++ b/solve.py.template @@ -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__':