Browse Source

days 1 and 2

main
xenua 1 year ago
commit
6dcfdc6920
  1. 3
      .gitignore
  2. 24
      1.py
  3. 82
      2.py
  4. 66
      aoclib.py
  5. 2250
      inputs/1
  6. 2500
      inputs/2
  7. 54
      prompts/1
  8. 47
      prompts/2

3
.gitignore vendored

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
venv/
__pycache__/**
.idea/

24
1.py

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
import aoclib
inp = aoclib.get_input(1, parser=aoclib.group_on_empty_line(casting_func=int))
def part1():
lorge = 0
for group in inp:
c = sum(group)
if c > lorge:
lorge = c
return lorge
def part2():
sums = []
for group in inp:
sums.append(sum(group))
return sum(list(reversed(sorted(sums)))[:3])
aoclib.main(part1, part2)

82
2.py

@ -0,0 +1,82 @@ @@ -0,0 +1,82 @@
import aoclib
inp = aoclib.get_input(2, parser=aoclib.parse_lines_with_type_and_default(
typ=lambda l: l.split(" "), default=None))
def shape_value(us):
match us:
case "X":
return 1
case "Y":
return 2
case "Z":
return 3
def win_value(them, us):
win = 6
draw = 3
lose = 0
if them == "A":
match us:
case "X":
return draw
case "Y":
return win
case "Z":
return lose
elif them == "B":
match us:
case "X":
return lose
case "Y":
return draw
case "Z":
return win
else:
match us:
case "X":
return win
case "Y":
return lose
case "Z":
return draw
def get_round_value(them, us):
return shape_value(us) + win_value(them, us)
def map_part2(them, us):
mapping = {
"X": {
"A": "Z",
"B": "X",
"C": "Y"
},
"Y": {
"A": "X",
"B": "Y",
"C": "Z"
},
"Z": {
"A": "Y",
"B": "Z",
"C": "X"
}
}
us = mapping[us][them]
return get_round_value(them, us)
def part1():
return sum([get_round_value(them, us) for them, us in inp])
def part2():
return sum([map_part2(them, us) for them, us in inp])
aoclib.main(part1, part2)

66
aoclib.py

@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
from sys import argv
def _no_op(x=None):
return x
def get_input(day: int, parser: callable = _no_op):
with open(f'inputs/{day}') as inp:
return parser(inp.read())
def parse_lines(raw: str):
return raw.splitlines(keepends=False)
def parse_lines_with_type_and_default(typ: callable, default):
def inner(raw: str):
lines = raw.splitlines(keepends=False)
out = []
for line in lines:
try:
out.append(typ(line))
except:
out.append(default)
return out
return inner
def group_on_empty_line(casting_func: callable = _no_op):
def inner(raw: str):
lines = raw.splitlines(keepends=False)
out = []
current = []
for line in lines:
if not line:
out.append(current)
current = []
else:
current.append(casting_func(line))
return out
return inner
def main(part1: callable = _no_op, part2: callable = _no_op):
match argv[-1]:
case "1":
ret = part1()
if ret is not None:
print(ret)
case "2":
ret = part2()
if ret is not None:
print(ret)
case _:
ret1 = part1()
if ret1 is not None:
print("part1:", ret1)
ret2 = part2()
if ret2 is not None:
print("part2:", ret2)

2250
inputs/1

File diff suppressed because it is too large Load Diff

2500
inputs/2

File diff suppressed because it is too large Load Diff

54
prompts/1

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
--- Day 1: Calorie Counting ---
Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows.
To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th. Although the Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way, just in case.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line.
For example, suppose the Elves finish writing their items' Calories and end up with the following list:
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
This list represents the Calories of the food carried by five Elves:
The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.
The second Elf is carrying one food item with 4000 Calories.
The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.
The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.
The fifth Elf is carrying one food item with 10000 Calories.
In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
Your puzzle answer was [redacted].
--- Part Two ---
By the time you calculate the answer to the Elves' question, they've already realized that the Elf carrying the most Calories of food might eventually run out of snacks.
To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.
In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 Calories), then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000.
Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?
Your puzzle answer was [redacted].

47
prompts/2

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
--- Day 2: Rock Paper Scissors ---
The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant Rock Paper Scissors tournament is already in progress.
Rock Paper Scissors is a game between two players. Each game contains many rounds; in each round, the players each simultaneously choose one of Rock, Paper, or Scissors using a hand shape. Then, a winner for that round is selected: Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock. If both players choose the same shape, the round instead ends in a draw.
Appreciative of your help yesterday, one Elf gives you an encrypted strategy guide (your puzzle input) that they say will be sure to help you win. "The first column is what your opponent is going to play: A for Rock, B for Paper, and C for Scissors. The second column--" Suddenly, the Elf is called away to help with someone's tent.
The second column, you reason, must be what you should play in response: X for Rock, Y for Paper, and Z for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen.
The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round. The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won).
Since you can't be sure if the Elf is trying to help you or trick you, you should calculate the score you would get if you were to follow the strategy guide.
For example, suppose you were given the following strategy guide:
A Y
B X
C Z
This strategy guide predicts and recommends the following:
In the first round, your opponent will choose Rock (A), and you should choose Paper (Y). This ends in a win for you with a score of 8 (2 because you chose Paper + 6 because you won).
In the second round, your opponent will choose Paper (B), and you should choose Rock (X). This ends in a loss for you with a score of 1 (1 + 0).
The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = 6.
In this example, if you were to follow the strategy guide, you would get a total score of 15 (8 + 1 + 6).
What would your total score be if everything goes exactly according to your strategy guide?
Your puzzle answer was [redacted].
--- Part Two ---
The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win. Good luck!"
The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this:
In the first round, your opponent will choose Rock (A), and you need the round to end in a draw (Y), so you also choose Rock. This gives you a score of 1 + 3 = 4.
In the second round, your opponent will choose Paper (B), and you choose Rock so you lose (X) with a score of 1 + 0 = 1.
In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = 7.
Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of 12.
Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide?
Your puzzle answer was [redacted].
Loading…
Cancel
Save