mirror of
https://github.com/RussellChamp/advent-of-code-2022.git
synced 2026-10-27 21:52:30 +00:00
day03 solution
This commit is contained in:
1 parent
24fae37154
commit
0716f6e4e9
3 files changed
+80
-2
No files matched your search
@@ -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()
|
||||
@@ -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()
|
||||
+15
-2
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user