Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
This commit is contained in:
TuDatTr
2023-10-09 01:45:03 +02:00
parent 7510bc2f59
commit 4e36ed6462
44 changed files with 1137 additions and 3 deletions

1
2022/day-3-alt/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/input.txt

9
2022/day-3-alt/README.md Normal file
View 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
View 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()}')