Skip to content

Commit

Permalink
day 4 rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
kafonek committed Jan 4, 2024
1 parent 69c04cd commit b8c7570
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
working-directory: ./rust-wasm
run: |
cargo install wasm-pack
wasm-pack build --target web
wasm-pack build --target web --release
- name: Setup Pages
uses: actions/configure-pages@v4
Expand Down
141 changes: 141 additions & 0 deletions notebooks/python/04-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "be4c97b5-7228-4a32-9cf7-84bda5fdc070",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53',\n",
" 'Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19',\n",
" 'Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1',\n",
" 'Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83',\n",
" 'Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36',\n",
" 'Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11']"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text = \"\"\"\n",
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53\n",
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19\n",
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1\n",
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83\n",
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36\n",
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11\n",
"\"\"\"\n",
"lines = text.strip().split('\\n')\n",
"lines"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e0d7aeda-3529-4deb-9197-1fd198becaaf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Card(id=1, winning_numbers=['41', '48', '83', '86', '17'], guess_numbers=['83', '86', '6', '31', '17', '9', '48', '53'])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from aoc_2023.day04 import Card\n",
"\n",
"Card.from_string(lines[0])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db052bc7-b05c-4c88-9cae-cf1701305635",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 8\n",
"2 2\n",
"3 2\n",
"4 1\n",
"5 0\n",
"6 0\n"
]
}
],
"source": [
"cards = [Card.from_string(line) for line in lines]\n",
"for card in cards:\n",
" print(card.id, card.score())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5eff0808-9f13-4382-9e47-dbbfefecf3c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer = 0\n",
"for card in cards:\n",
" answer += card.score()\n",
"\n",
"answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2569e0c-8859-4547-b10b-d132a6fd419c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
147 changes: 147 additions & 0 deletions notebooks/python/04-2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ee80730c-7438-4dcc-b184-f6e7aa494114",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53',\n",
" 'Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19',\n",
" 'Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1',\n",
" 'Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83',\n",
" 'Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36',\n",
" 'Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11']"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text = \"\"\"\n",
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53\n",
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19\n",
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1\n",
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83\n",
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36\n",
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11\n",
"\"\"\"\n",
"lines = text.strip().split('\\n')\n",
"lines"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8fb9be20-0758-4368-b3c2-da7dda1167f6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 4, 5]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from aoc_2023.day04 import Card\n",
" \n",
"c = Card.from_string(lines[0])\n",
"c.copies()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "47b2abef-b33f-48e1-bb62-214437c068a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defaultdict(int, {1: 1, 2: 2, 3: 4, 4: 8, 5: 14, 6: 1})"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import collections\n",
"\n",
"cards = [Card.from_string(line) for line in lines]\n",
"counts = collections.defaultdict(int)\n",
"\n",
"for card in cards:\n",
" counts[card.id] += 1\n",
"\n",
"for card in cards:\n",
" mult = counts[card.id]\n",
" for copy in card.copies():\n",
" counts[copy] += mult\n",
"counts "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b67c95ec-7173-4d5b-9523-2d642e19850b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer = sum(counts.values())\n",
"answer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eda69c05-340a-43af-9a3b-e9b2f30c2064",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
34 changes: 34 additions & 0 deletions python/aoc_2023/day04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from dataclasses import dataclass
from typing import List


@dataclass
class Card:
id: int
winning_numbers: List[int]
guess_numbers: List[int]

@classmethod
def from_string(cls, s: str) -> "Card":
card, numbers = s.split(":", 1)
id = int(card.split()[1])
(left, right) = numbers.split("|", 1)
winning_numbers = left.split()
guess_numbers = right.split()
return cls(id=id, winning_numbers=winning_numbers, guess_numbers=guess_numbers)

def score(self) -> int:
score = 0
for n in self.guess_numbers:
if n in self.winning_numbers:
if not score:
score = 1
else:
score *= 2
return score

def copies(self) -> List[int]:
copy_range = len([n for n in self.guess_numbers if n in self.winning_numbers])
start = self.id + 1
stop = start + copy_range
return list(range(start, stop))
21 changes: 21 additions & 0 deletions python/bin/04-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path

from aoc_2023.day04 import Card
from aoc_2023.utils import run_and_time


def solve(fp: Path) -> str:
lines = fp.read_text().splitlines()

cards = [Card.from_string(line) for line in lines]

answer = 0
for card in cards:
answer += card.score()

return str(answer)


if __name__ == "__main__":
fp = Path("../data/day04.txt")
run_and_time(solve, fp)
29 changes: 29 additions & 0 deletions python/bin/04-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import collections
from pathlib import Path

from aoc_2023.day04 import Card
from aoc_2023.utils import run_and_time


def solve(fp: Path) -> str:
lines = fp.read_text().splitlines()

cards = [Card.from_string(line) for line in lines]
counts = collections.defaultdict(int)

for card in cards:
counts[card.id] += 1

for card in cards:
mult = counts[card.id]
for copy in card.copies():
counts[copy] += mult

answer = sum(counts.values())

return str(answer)


if __name__ == "__main__":
fp = Path("../data/day04.txt")
run_and_time(solve, fp)
6 changes: 6 additions & 0 deletions rust-pyo3/aoc_2023_pyo3/day04.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import List

class Card:
id: int
def score(self) -> int: ...
def copies(self) -> List[int]: ...
Loading

0 comments on commit b8c7570

Please sign in to comment.