2022-02
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
0dc14d600a
commit
b0234096f2
7 changed files with 107 additions and 2 deletions
1
challenges/2022/02-rockPaperScissors/README.md
Normal file
1
challenges/2022/02-rockPaperScissors/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 2: Rock Paper Scissors](https://adventofcode.com/2022/day/2)
|
15
challenges/2022/02-rockPaperScissors/benchmark.json
Normal file
15
challenges/2022/02-rockPaperScissors/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 2,
|
||||
"dir": "challenges/2022/02-rockPaperScissors",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.005764134883880615,
|
||||
"part.1.max": 0.013495206832885742,
|
||||
"part.1.min": 0.0013463497161865234,
|
||||
"part.2.avg": 0.0034727907180786134,
|
||||
"part.2.max": 0.008818864822387695,
|
||||
"part.2.min": 0.0007967948913574219
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
17
challenges/2022/02-rockPaperScissors/info.json
Normal file
17
challenges/2022/02-rockPaperScissors/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "A Y\nB X\nC Z\n",
|
||||
"expected": "15"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "A Y\nB X\nC Z\n",
|
||||
"expected": "12"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
71
challenges/2022/02-rockPaperScissors/py/__init__.py
Normal file
71
challenges/2022/02-rockPaperScissors/py/__init__.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
from typing import *
|
||||
from aocpy import BaseChallenge
|
||||
|
||||
|
||||
Match = List[str]
|
||||
|
||||
ROCK = "A"
|
||||
PAPER = "B"
|
||||
SCISSORS = "C"
|
||||
|
||||
LOSE = "X"
|
||||
DRAW = "Y"
|
||||
WIN = "Z"
|
||||
|
||||
SCORES = {
|
||||
ROCK: 1,
|
||||
PAPER: 2,
|
||||
SCISSORS: 3,
|
||||
WIN: 6,
|
||||
LOSE: 0,
|
||||
DRAW: 3,
|
||||
}
|
||||
|
||||
|
||||
def parse(instr: str) -> List[Match]:
|
||||
return [x.split(" ") for x in instr.strip().splitlines()]
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
inp = parse(instr)
|
||||
|
||||
for i, m in enumerate(inp):
|
||||
m[1] = m[1].replace("X", ROCK).replace("Y", PAPER).replace("Z", SCISSORS)
|
||||
inp[i] = m
|
||||
|
||||
score = 0
|
||||
for m in inp:
|
||||
score += SCORES[m[1]]
|
||||
|
||||
if m[0] == m[1]:
|
||||
score += SCORES[DRAW]
|
||||
elif (
|
||||
(m[1] == ROCK and m[0] == SCISSORS)
|
||||
or (m[1] == PAPER and m[0] == ROCK)
|
||||
or (m[1] == SCISSORS and m[0] == PAPER)
|
||||
):
|
||||
score += SCORES[WIN]
|
||||
|
||||
return score
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
inp = parse(instr)
|
||||
|
||||
magic_beans = [SCISSORS, ROCK, PAPER, SCISSORS, ROCK]
|
||||
|
||||
outcome_offsets = {
|
||||
WIN: 1,
|
||||
LOSE: -1,
|
||||
DRAW: 0,
|
||||
}
|
||||
|
||||
score = 0
|
||||
for m in inp:
|
||||
score += SCORES[m[1]]
|
||||
our_move = magic_beans[magic_beans.index(m[0], 1) + outcome_offsets[m[1]]]
|
||||
score += SCORES[our_move]
|
||||
|
||||
return score
|
|
@ -11,9 +11,10 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
|
|||
| Day | Status | Solutions | Notes |
|
||||
| ----------------------------------- | ------------------ | ---------- | ------ |
|
||||
| 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 |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
---
|
||||
|
||||
<!--  -->
|
||||

|
||||
|
|
BIN
challenges/2022/running-times.png
Normal file
BIN
challenges/2022/running-times.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
|
@ -54,7 +54,7 @@ for lang in benchmark_data:
|
|||
day = int(key.split(".", 1)[0])
|
||||
all_days.add(day)
|
||||
|
||||
figure = plt.figure(figsize=(len(all_days)/2, 5))
|
||||
figure = plt.figure(figsize=(25/2, 5))
|
||||
axp1 = figure.add_subplot(1, 2, 1)
|
||||
axp2 = figure.add_subplot(1, 2, 2, sharey=axp1)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue