Added solution for AoC 2022/3, in python and in rust

Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
This commit is contained in:
TuDatTr
2022-12-03 13:59:13 +01:00
parent a5420b8c51
commit 04f18534bc
17 changed files with 816 additions and 6 deletions

View File

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

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`

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()}')