From 0716f6e4e9a8249c768e69d30d782f305be81457 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Mon, 5 Dec 2022 11:52:38 -0500 Subject: [PATCH] day03 solution --- day03/solve.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ day03/tests.py | 12 +++++++++++ solve.py.template | 17 +++++++++++++-- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 day03/solve.py create mode 100644 day03/tests.py diff --git a/day03/solve.py b/day03/solve.py new file mode 100644 index 0000000..0b57f81 --- /dev/null +++ b/day03/solve.py @@ -0,0 +1,53 @@ +# Day 03: Rucksack Reorganization + +import os + +def get_path(): + return os.path.join(os.path.dirname(__file__), 'data.txt') + +def get_priority(item): + iitem = ord(item) + if iitem >= 65 and iitem <= 90: # between A-Z + return iitem - 38 # range 27 and 52 + elif iitem >= 97 and iitem <= 122: # between a-z + return iitem - 96 # range between 1 and 26 + else: + return 0 + +def solve_part1(): + total = 0 + + with open(get_path()) as fp: + for line in fp: + [first,second] = [line[:len(line)/2], line[len(line)/2:]] + for c in first: + if c in second: + total += get_priority(c) + break + + print('GOAL: Find the item type that appears in both compartments of each rucksack.') + print('ANSWER: What is the sum of the priorities of those item types?') + print('Total: %d' % (total)) + +def solve_part2(): + group = 0 + total = 0 + + with open(get_path()) as fp: + while True: + [first, second, third] = [fp.readline(), fp.readline(), fp.readline()] + if first == '': + break + group += 1 + for c in first: + if c in second and c in third: + total += get_priority(c) + break + + print('GOAL: Find the item type that corresponds to the badges of each three-Elf group.') + print('ANSWER: What is the sum of the priorities of those item types?') + print('Total: %d' % (total)) + +if __name__ == '__main__': + solve_part1() + solve_part2() \ No newline at end of file diff --git a/day03/tests.py b/day03/tests.py new file mode 100644 index 0000000..b41f907 --- /dev/null +++ b/day03/tests.py @@ -0,0 +1,12 @@ +import unittest + +class Day03TestMethods(unittest.TestCase): + def setUp(self): + pass + + # Does a thing + def test_true(self): + self.assertEqual(True, False) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/solve.py.template b/solve.py.template index 6e86d4a..42d47b2 100644 --- a/solve.py.template +++ b/solve.py.template @@ -1,9 +1,22 @@ # Day #DAY#: #TITLE# +import os + +def get_path(): + return os.path.join(os.path.dirname(__file__), 'data.txt') + def solve_part1(): - print('hello') + total = 0 + + print('GOAL: #GOAL1#') + print('ANSWER: #ANSWER1#) + print('Total: %d' % (total)) def solve_part2(): - print('world') + total = 0 + + print('GOAL: #GOAL2#') + print('ANSWER: #ANSWER2#) + print('Total: %d' % (total)) if __name__ == '__main__': solve_part1()