feat: day 2

main
Cassie Cheung 6 months ago
parent fa83d5b526
commit 62d75050a6
Signed by: cassie
GPG Key ID: 21918D0F2EEBCCD5
13 changed files with 150 additions and 6 deletions

@ -1,3 +1,4 @@
{
"vscord.status.buttons.button1.git.active.url": "https://patchy.soopy.moe/mizuki/aoc"
"vscord.status.buttons.button1.git.active.url": "https://patchy.soopy.moe/mizuki/aoc",
"python.analysis.typeCheckingMode": "off"
}

@ -17,4 +17,4 @@ test_data = """
10000
"""
main(test_data)
main(test_data.strip())

@ -1,5 +1,47 @@
def main(data):
... # write your logic here !
from aoc_utils.log import debug
scoring = {
"A": { # opp: rock
"X": 3, # me: rock
"Y": 6, # me: paper
"Z": 0, # me: scissors
},
"B": { # opp: paper
"X": 0, # me: rock
"Y": 3, # me: paper
"Z": 6, # me: scissors
},
"C": { # opp: scissors
"X": 6, # me: rock
"Y": 0, # me: paper
"Z": 3, # me: scissors
},
}
# s >> 1
shape = {
"X": 1,
"Y": 2,
"Z": 3,
}
def main(data: str):
score = 0
debug(data.splitlines())
strats = [(round.split()[0], round.split()[1]) for round in data.splitlines()]
for count, (opp, me) in enumerate(strats):
debug(f"opp: {opp} me: {me}")
cur = scoring[opp][me] + shape[me]
debug(f"total score for round #{count}: ", cur)
score += cur
print(
f"#{count}: {opp} <> {me} ({scoring[opp][me]}) + base ({shape[me]}) = {cur}, total={score}"
)
print("========== RESULT ==========")
print(f" total: {score}")
if __name__ == "__main__":

@ -0,0 +1,56 @@
from aoc_utils.log import debug
# tuple format: (to win, to draw, to lose)
rule = {
"A": ("E", "D", "F"), # opp: rock
"B": ("F", "E", "D"), # opp: paper
"C": ("D", "F", "E"), # opp: scissors
}
# s >> 1
scoring = {"X": 0, "Y": 3, "Z": 6}
shapes = {
"D": 1, # rock
"E": 2, # paper
"F": 3, # scissors
}
def main(data: str):
score = 0
debug(data.splitlines())
strats = [(round.split()[0], round.split()[1]) for round in data.splitlines()]
for count, (opp, winlose) in enumerate(strats):
debug("###############################################")
debug(f"Opponent used {opp}, need to { {'X':'lose','Y':'tie','Z':'win'}.get(winlose) }")
cur = 0
cur += scoring[winlose]
debug(f"{winlose}: +{scoring[winlose]} pt")
# figure out which shape to use
match winlose:
case "X": # Lose
debug(f"LOSE: base score of shape {rule[opp][2]} is {shapes[rule[opp][2]]}")
wl = 2
case "Y": # Draw
debug(f"DRAW: base score of {rule[opp][1]} is {shapes[rule[opp][1]]}")
wl = 1
case "Z": # Win
debug(f" WIN: base score of {rule[opp][0]} is {shapes[rule[opp][2]]}")
wl = 0
case _:
raise ValueError(f'what the fuck is {winlose}')
cur += shapes[rule[opp][wl]]
score += cur
print(f"Round #{count+1}: {shapes[rule[opp][wl]]} + {scoring[winlose]} = {cur}; Σ={score}")
print("========== RESULT ==========")
print(f" total: {score}")
if __name__ == "__main__":
# shouldn't change anything here.
with open("input.txt") as f:
main(f.read().strip())

@ -6,4 +6,4 @@ B X
C Z
"""
main(test_data)
main(test_data.strip())

@ -0,0 +1,9 @@
from main_2 import main
test_data = """
A Y
B X
C Z
"""
main(test_data.strip())

@ -4,4 +4,4 @@ test_data = """
paste in test data here
"""
main(test_data)
main(test_data.strip())

@ -0,0 +1,11 @@
# copy and rename to main_2 for part 2.
def main(data: str):
... # write your logic here !
if __name__ == "__main__":
# shouldn't change anything here.
with open("input.txt") as f:
main(f.read().strip())

@ -0,0 +1,7 @@
from main import main
test_data = """
paste in test data here
"""
main(test_data.strip())

@ -0,0 +1,7 @@
from main_2 import main
test_data = """
paste in test data here
"""
main(test_data.strip())

@ -1,6 +1,8 @@
.PHONY: help Makefile format
MAKEFLAGS := --jobs=1
export PYTHONUNBUFFERED = true
export DEBUG
all:
@python .util.py --help

@ -0,0 +1,9 @@
import os
__all__ = ("debug",)
def debug(msg: str, *args, **kwargs) -> None:
if not os.getenv("DEBUG", False):
return
print("[DBG]", msg, *args, **kwargs)
Loading…