Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day 04 #85

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions aoc_wim/aoc2023/q04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
--- Day 4: Scratchcards ---
https://adventofcode.com/2023/day/4
"""
from aocd import data

lines = data.splitlines()
n = len(lines)
L = [1] * n
a = b = 0
for i, line in enumerate(lines):
_, lr = line.split(": ")
l, r = lr.split(" | ")
winners = {int(x) for x in l.split()}
w = sum(1 for i in map(int, r.split()) if i in winners)
if w:
a += 2 ** (w - 1)
b += L[i]
for j in range(i + 1, i + 1 + w):
try:
L[j] += L[i]
except IndexError:
break

print("answer_a:", a)
print("answer_b:", b)
49 changes: 27 additions & 22 deletions aoc_wim/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,35 @@ def start():
if day == 1 and not src.parent.is_dir():
src.parent.mkdir()
src.parent.joinpath("__init__.py").touch()
src.write_text(dedent('''\
from aocd import data
from aoc_wim.autoparse import parsed
from collections import Counter, defaultdict, deque
d = parsed(data)
if d != data:
print(d)
print(f"{parsed.n_bytes} bytes/{parsed.n_lines} lines, parsed by {parsed.parser}")
src.write_text(
dedent(
"""\
from aocd import data
from aoc_wim.autoparse import parsed
from collections import Counter, defaultdict, deque
d = parsed(data)
if d != data:
print(d)
print(f"{parsed.n_bytes} bytes/{parsed.n_lines} lines, parsed by {parsed.parser}")

# import numpy as np
# import networkx as nx
# from aoc_wim.zgrid import ZGrid
# from aoc_wim import stuff
# import numpy as np
# import networkx as nx
# from aoc_wim.zgrid import ZGrid
# from aoc_wim import stuff

# import logging; logging.basicConfig(level=logging.DEBUG)
# import logging; logging.basicConfig(level=logging.DEBUG)




print("answer_a:", )
print("answer_b:", )

# from aocd import submit; submit(a)
'''))
print("answer_a:", )
print("answer_b:", )

# from aocd import submit; submit(a)
"""
)
)
test = here.parent / "tests" / str(year) / str(day).zfill(2) / "0.txt"
if not test.exists():
test.parent.mkdir(parents=True, exist_ok=True)
Expand All @@ -134,9 +139,9 @@ def start():
puzzle = Puzzle(year, day)
[example] = puzzle.examples
if test.read_text() == "\n-\n-\n":
test.write_text(dedent(f"""\
{example.input_data}
{example.answer_a or "-"}
{example.answer_b or "-"}
"""))
test.write_text(
f"{example.input_data}\n"
f"{example.answer_a or '-'}\n"
f"{example.answer_b or '-'}\n"
)
webbrowser.open(puzzle.url)
8 changes: 8 additions & 0 deletions tests/2023/04/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
13
30