Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-03 11:09:28 +00:00
parent 01f5910c02
commit 7c899351d6
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 78 additions and 0 deletions

View file

@ -0,0 +1 @@
# [Day 3: Rucksack Reorganization](https://adventofcode.com/2022/day/3)

View file

@ -0,0 +1,15 @@
{
"day": 3,
"dir": "challenges/2022/03-rucksackReorganization",
"implementations": {
"Python": {
"part.1.avg": 0.0015598664283752442,
"part.1.max": 0.0057833194732666016,
"part.1.min": 0.00038242340087890625,
"part.2.avg": 0.0011923382282257081,
"part.2.max": 0.003597736358642578,
"part.2.min": 0.00029277801513671875
}
},
"numRuns": 1000
}

View file

@ -0,0 +1,17 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw",
"expected": "157"
}
],
"two": [
{
"input": "vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw",
"expected": "70"
}
]
}
}

View file

@ -0,0 +1,44 @@
from typing import *
from aocpy import BaseChallenge
def parse(instr: str) -> List[str]:
return instr.strip().splitlines()
def get_priority(char: str) -> int:
char = char[0]
co = ord(char)
if ord("a") <= co and co <= ord("z"):
return (co - ord("a")) + 1
return (co - ord("A")) + 27
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
inp = parse(instr)
sigma = 0
for x in inp:
assert len(x) % 2 == 0
l = len(x)//2
y = set(x[:l]).intersection(x[l:])
sigma += get_priority(y.pop())
return sigma
@staticmethod
def two(instr: str) -> int:
inp = parse(instr)
assert len(inp) % 3 == 0
sigma = 0
for i in range(0, len(inp), 3):
y = set(inp[i])
y.intersection_update(inp[i+1])
y.intersection_update(inp[i+2])
sigma += get_priority(y.pop())
return sigma

View file

@ -12,6 +12,7 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| ----------------------------------- | ------------------ | ---------- | ------ |
| 01 - Calorie Counting | ★ ★ | [Python](01-calorieCounting/py), [Nim](01-calorieCounting/nim), [Java](01-calorieCounting/java/src) | Summing numbers |
| 02 - Rock Paper Scissors | ★ ★ | [Python](02-rockPaperScissors/py) | Programmatically playing Rock Paper Scissors |
| 03 - Rucksack Reorganization | ★ ★ | [Python](03-rucksackReorganization/py) | Sets and intersections |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Before After
Before After