1
2022/day-3-alt/.gitignore
vendored
Normal file
1
2022/day-3-alt/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/input.txt
|
||||
9
2022/day-3-alt/README.md
Normal file
9
2022/day-3-alt/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Rucksack Reorganization
|
||||
Created this solution first, since the implementation seemed fairly easy.
|
||||
Turns out it was.
|
||||
|
||||
<sub> Could use some more list comprehensions though </sub>
|
||||
|
||||
## Usage
|
||||
Expecting the `input.txt` in the same folder as the `main.py`
|
||||
`python main.py`
|
||||
26
2022/day-3-alt/main.py
Normal file
26
2022/day-3-alt/main.py
Normal file
@@ -0,0 +1,26 @@
|
||||
def task3a():
|
||||
sum = 0
|
||||
for rucksack in open('input.txt', 'r').read().splitlines():
|
||||
left = rucksack[:int(len(rucksack)/2)]
|
||||
right= rucksack[int(len(rucksack)/2):]
|
||||
|
||||
common = [c for c in left if c in right][0]
|
||||
priority = ord(common) - ord('a') + 1 if common.islower() else ord(common) - ord('A') + 27
|
||||
# print(f"{common}: {priority}")
|
||||
sum += priority
|
||||
return sum
|
||||
print(f'Task 3a: {task3a()}')
|
||||
|
||||
def task3b():
|
||||
sum = 0
|
||||
lines = open('input.txt', 'r').read().splitlines()
|
||||
for rucksacks in [lines[n:n+3] for n in range(0, len(lines), 3)]:
|
||||
common = [d for d in [c for c in rucksacks[0] if c in rucksacks[1]] if d in rucksacks[2]][0]
|
||||
priority = ord(common) - ord('a') + 1 if common.islower() else ord(common) - ord('A') + 27
|
||||
# print(f"{common}: {priority}")
|
||||
sum += priority
|
||||
return sum
|
||||
|
||||
print(f'Task 3b: {task3b()}')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user