Add 2021-06 in Python
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
6b985a902f
commit
17bad02499
7 changed files with 117 additions and 7 deletions
33
challenges/2021/06-lanternfish/README.md
Normal file
33
challenges/2021/06-lanternfish/README.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
# [Day 6: Lanternfish](https://adventofcode.com/2021/day/6)
|
||||
|
||||
First, parse the input data into a usable form. Count the number of fish with each possible timer value (a given fish timer `t` must satisfy `0 <= t <= 8`). Then, iterating can begin.
|
||||
|
||||
A single iteration does the following (in this case, the example input `3,4,3,1,2` is being used):
|
||||
|
||||
| | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|
||||
| -------------------------------------------------------------------------------------------------------------------------- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
| Initial starting timer frequencies | | 0 | 1 | 1 | 2 | 1 | 0 | 0 | 0 | 0 |
|
||||
| Shift input left | 0 | 1 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | |
|
||||
| Add the value of -1 to the value of 6, and set the value of 8 to be equal to the value of -1, then remove the value for -1 | | 1 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | 0 |
|
||||
| | | | | | | | | | | |
|
||||
| Repeat | | 1 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | 0 |
|
||||
| Shift input left | 1 | 1 | 2 | 1 | 0 | 0 | 0 | 0 | 0 | |
|
||||
| Add values | | 1 | 2 | 1 | 0 | 0 | 0 | 1 | 0 | 1 |
|
||||
| | | | | | | | | | | |
|
||||
| Repeat | | 1 | 2 | 1 | 0 | 0 | 0 | 1 | 0 | 1 |
|
||||
| Shift input left | 1 | 2 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | |
|
||||
| Add values | | 2 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
|
||||
| | | | | | | | | | | |
|
||||
| Repeat | | 2 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
|
||||
| Shift input left | 2 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | |
|
||||
| Add values | | 1 | 0 | 0 | 0 | 0 | 0 | 3 | 1 | 2 |
|
||||
| | | | | | | | | | | |
|
||||
| etc, etc. | | | | | | | | | | |
|
||||
|
||||
Eventually, after the desired number of iterations, you can sum all the frequency values to get the challenge answer.
|
||||
|
||||
---
|
||||
|
||||
I commandeered a whiteboard for this.
|
||||
|
||||

|
15
challenges/2021/06-lanternfish/benchmark.json
Normal file
15
challenges/2021/06-lanternfish/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 6,
|
||||
"dir": "challenges/2021/06-lanternfish",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.00020124888420104982,
|
||||
"part.1.max": 0.0004673004150390625,
|
||||
"part.1.min": 0.0001266002655029297,
|
||||
"part.2.avg": 0.0004714465141296387,
|
||||
"part.2.max": 0.0010838508605957031,
|
||||
"part.2.min": 0.00030684471130371094
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
17
challenges/2021/06-lanternfish/info.json
Normal file
17
challenges/2021/06-lanternfish/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "3,4,3,1,2",
|
||||
"expected": "5934"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "3,4,3,1,2",
|
||||
"expected": "26984457539"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
44
challenges/2021/06-lanternfish/py/__init__.py
Normal file
44
challenges/2021/06-lanternfish/py/__init__.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from typing import List, Dict
|
||||
from aocpy import BaseChallenge
|
||||
|
||||
|
||||
def parse(instr: str) -> List[int]:
|
||||
return list(map(int, instr.strip().split(",")))
|
||||
|
||||
|
||||
def count_fish_by_timer(all_fish: List[int]) -> Dict[int, int]:
|
||||
m = {}
|
||||
for fish in all_fish:
|
||||
m[fish] = m.get(fish, 0) + 1
|
||||
return m
|
||||
|
||||
|
||||
def iterate_fish_once(sum_dict: Dict[int, int]):
|
||||
nm = {}
|
||||
# shift back
|
||||
for i in range(9):
|
||||
nm[i - 1] = sum_dict.get(i, 0)
|
||||
# apply -1 value
|
||||
nm[6] = nm.get(6, 0) + nm.get(-1, 0)
|
||||
nm[8] = nm.get(-1, 0)
|
||||
del nm[-1]
|
||||
return nm
|
||||
|
||||
|
||||
def sum_of_fish_after_n(fish: List[int], n: int) -> int:
|
||||
by_timer = count_fish_by_timer(fish)
|
||||
for _ in range(n):
|
||||
by_timer = iterate_fish_once(by_timer)
|
||||
return sum([by_timer[k] for k in by_timer])
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
fish = parse(instr)
|
||||
return sum_of_fish_after_n(fish, 80)
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
fish = parse(instr)
|
||||
return sum_of_fish_after_n(fish, 256)
|
BIN
challenges/2021/06-lanternfish/whiteboard.jpg
Normal file
BIN
challenges/2021/06-lanternfish/whiteboard.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 402 KiB |
|
@ -8,13 +8,14 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
|
||||
<!-- PARSE START -->
|
||||
|
||||
| Day | Status | Solutions | Notes |
|
||||
| ----------------------------------- | ------------------ | ---------------------------------------------------------------------------- | ---------------------------------------------------------------- |
|
||||
| 01 - Sonar Sweep | Complete | [Python](01-sonarSweep/py), [Go](01-sonarSweep/go), [Nim](01-sonarSweep/nim) | Numbers and sliding windows. |
|
||||
| 02 - Dive! | Complete | [Python](02-dive/py), [Go](02-dive/go) | Have this set of instructions and do something sensible with it. |
|
||||
| 03 - Binary Diagnostic | Complete | [Python](03-binaryDiagnostic/py), [Nim](03-binaryDiagnostic/nim) | Bit twiddling aplenty! |
|
||||
| 04 - Giant Squid | Complete | [Python](04-giantSquid/py) | B-I-N-G-O, B-I-N-G-O, B-I-N-G-O and Bingo was his name-o! |
|
||||
| 05 - Hydrothermal Venture | Complete | [Python](05-hydrothermalVenture/py), [Go](05-hydrothermalVenture/go) | Pointy. |
|
||||
| Day | Status | Solutions | Notes |
|
||||
| ----------------------------------- | ------------------ | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
|
||||
| 01 - Sonar Sweep | Complete | [Python](01-sonarSweep/py), [Go](01-sonarSweep/go), [Nim](01-sonarSweep/nim) | Numbers and sliding windows. |
|
||||
| 02 - Dive! | Complete | [Python](02-dive/py), [Go](02-dive/go) | Have this set of instructions and do something sensible with it. |
|
||||
| 03 - Binary Diagnostic | Complete | [Python](03-binaryDiagnostic/py), [Nim](03-binaryDiagnostic/nim) | Bit twiddling aplenty! |
|
||||
| 04 - Giant Squid | Complete | [Python](04-giantSquid/py) | B-I-N-G-O, B-I-N-G-O, B-I-N-G-O and Bingo was his name-o! |
|
||||
| 05 - Hydrothermal Venture | Complete | [Python](05-hydrothermalVenture/py), [Go](05-hydrothermalVenture/go) | Pointy. |
|
||||
| 06 - Lanternfish | Complete | [Python](06-lanternfish) | At this rate, the mass of the fish would surpass that of the Earth pretty quickly. |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 50 KiB |
Loading…
Add table
Add a link
Reference in a new issue