Add 2021-21 in Python (part one only)

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-12-21 13:20:38 +00:00
parent fd1956dc10
commit 1bd7ffe158
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 111 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# [Day 21: Dirac Dice](https://adventofcode.com/2021/day/21)

View file

@ -0,0 +1,15 @@
{
"day": 21,
"dir": "challenges/2021/21-diracDice",
"implementations": {
"Python": {
"part.1.avg": 0.0007445704936981201,
"part.1.max": 0.00394892692565918,
"part.1.min": 0.0004050731658935547,
"part.2.avg": 0.000006680965423583984,
"part.2.max": 0.000054836273193359375,
"part.2.min": 0.0000011920928955078125
}
},
"numRuns": 1000
}

View file

@ -0,0 +1,17 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "Player 1 starting position: 4\nPlayer 2 starting position: 8\n",
"expected": "739785"
}
],
"two": [
{
"input": "Player 1 starting position: 4\nPlayer 2 starting position: 8\n",
"expected": "444356092776315"
}
]
}
}

View file

@ -0,0 +1,76 @@
from typing import Any, List, Tuple
from aocpy import BaseChallenge
class DeterministicDie:
num_rolls: int
def __init__(self, sides: int = 100):
self._sides = sides
self._n = 0
self.num_rolls = 0
def next_number(self) -> int:
self.num_rolls += 1
self._n += 1
if self._n > self._sides:
self._n = 1
return self._n
def next_n_numbers(self, n: int) -> List[int]:
o = []
for _ in range(n):
o.append(self.next_number())
return o
Player = List[int] # position, score
Players = Tuple[Player, Player]
def parse(instr: str) -> Players:
o = []
for line in instr.strip().splitlines():
start_pos = int(line.split(": ")[-1])
o.append([start_pos, 0])
return tuple(o)
def get_next_position(current_position: int, steps: int) -> int:
n = current_position + (steps % 10)
return n if n <= 10 else n - 10
def move_player_forwards_by(p: Player, steps: int):
p[0] = get_next_position(p[0], steps)
p[1] = p[1] + p[0]
def get_player_score(p: Player) -> int:
return p[1]
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> Any:
players = parse(instr)
die = DeterministicDie()
while True:
game_finished = False
for player in players:
forward_steps = sum(die.next_n_numbers(3))
move_player_forwards_by(player, forward_steps)
if get_player_score(player) >= 1000:
game_finished = True
break
if game_finished:
break
scores = list(sorted([get_player_score(p) for p in players]))
return scores[0] * die.num_rolls
@staticmethod
def two(instr: str) -> int:
return NotImplementedError

View file

@ -32,6 +32,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
| 18 - Snailfish | Could not solve | | |
| 19 - Beacon Scanner | Unattempted | | |
| 20 - Trench Map \* | ★ ★ | [Python](20-trenchMap/py), [Nim](20-trenchMap/nim) | Took a moment to realise that the infinite grid alternates between lit and unlit, and even then I had to look at someone else's solution to realise it. |
| 21 - Dirac Dice | ★ ☆ | [Python](21-diracDice/py) | Couldn't be bothered with part two - I've got too much going on at the moment to spend a lot of time thinking about it. |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 37 KiB

Before After
Before After