From 312c9107e33ecccaacddfe410db6291acfabb93a Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Mon, 5 Dec 2022 12:38:19 -0500 Subject: [PATCH] day04 solution --- .gitignore | 3 ++- day02/solve.pyc | Bin 2596 -> 0 bytes day04/solve.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ day04/tests.py | 38 ++++++++++++++++++++++++++++++++++++++ solve.py.template | 8 ++++++-- 5 files changed, 92 insertions(+), 3 deletions(-) delete mode 100644 day02/solve.pyc create mode 100644 day04/solve.py create mode 100644 day04/tests.py 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 1cef8e934b7b6cdf005ac98ef02cfcef2c6534f4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2596 zcmd5;>uwuW5dL;Ic4EiQ{Yrp_OTd6qx0Xny2vsd^)I@+C&ZbIP30Y>(NwdNB8t+M! zO3APCi$A;o55Obv96SjU{D+y@buOhKBqD+B{dQ)~oY~pA9ecl)i$5=Y7lz0_9^LQJ zmHbAa>F1yW1Vp&_mIw#hL*PP>An>4b2u7gu2y)P)2=dTl2u7g`2*#j`2nx{S2#Sb2 zm@$|fHcSB~Pq!k>C^mv|=n{gGsFk712qvH>?&C^0Izqo?blqoj#2# zL8)_4ZV*`D$ib02Bs|y=Jg2xgc<#clxTMoa#t3!Wwr|=KTgmY@md6R{0{yl<6yhk3 zdUljmqd~9THI}!vS_e_bv|1ISaRJF4VylO7*Hj1LVY}Opk58&$tWCA8pPHUsj}O+F zU9a7$)vCJv>{hLs#GR++X8)PkePV2jpUmDQ>EW#S)&Mh&ZL6gC`OmGC%}{J=wk_rzP)Y@)6ivCLip<=0TAmQ_G9~6`SXK zf4FLMxHq{hC!0eb20wzraBkBUE@-NAzrwHCs1yk;gKE}4Y3($2AMM%kA=`YkH{^Hk z?{2AVy+rFNpD9d`z_0K4epC6G&g5lLrdA-?-p*FbuQ$~e`9o<5g;zZx-3D83H&<@+}ST?tf7LR6N6kzL@Bj z76!#=SQ!7j02F6b($49jY}K2eZ7I&GQf!i zOZGdFRZB!wSWR*)M$%%JA$`!7+1#OqlHiAjaTJ>5nu5={m$6K= 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__':