start on 2015
This commit is contained in:
parent
bbf66c23d1
commit
8adc7b0de8
5 changed files with 1094 additions and 0 deletions
44
2015/1/d1.py
Normal file
44
2015/1/d1.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
"""
|
||||
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
|
||||
|
||||
An opening parenthesis, `(`, means he should go up one floor, and a closing parenthesis, `)`, means he should go down one floor.
|
||||
|
||||
The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.
|
||||
|
||||
For example:
|
||||
|
||||
`(())` and `()()` both result in floor 0.
|
||||
`(((` and `(()(()(` both result in floor 3.
|
||||
`))(((((` also results in floor 3.
|
||||
`())`and `))(` both result in floor -1 (the first basement level).
|
||||
`)))` and `)())())` both result in floor -3.
|
||||
|
||||
To what floor do the instructions take Santa?
|
||||
"""
|
||||
|
||||
with open('directions.txt') as filein:
|
||||
text = filein.read()
|
||||
|
||||
translation = {
|
||||
'(':1,
|
||||
')':-1
|
||||
}
|
||||
|
||||
print(sum([translation.get(c,0) for c in text]))
|
||||
|
||||
"""
|
||||
Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
|
||||
|
||||
For example:
|
||||
|
||||
`)` causes him to enter the basement at character position 1.
|
||||
`()())` causes him to enter the basement at character position 5.
|
||||
|
||||
What is the position of the character that causes Santa to first enter the basement?
|
||||
"""
|
||||
s = 0
|
||||
for i,c in enumerate(text, start=1):
|
||||
s += translation[c]
|
||||
if s<0:
|
||||
break
|
||||
print(i)
|
||||
1
2015/1/directions.txt
Normal file
1
2015/1/directions.txt
Normal file
File diff suppressed because one or more lines are too long
46
2015/2/day_2.py
Normal file
46
2015/2/day_2.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
"""
|
||||
# Day 2: I Was Told There Would Be No Math
|
||||
|
||||
The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.
|
||||
|
||||
Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side.
|
||||
|
||||
For example:
|
||||
|
||||
- A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet.
|
||||
- A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet.
|
||||
|
||||
All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?
|
||||
"""
|
||||
|
||||
def surface(dimensions:str) -> int:
|
||||
l,w,h = [int(i) for i in dimensions.strip().split('x')]
|
||||
s1, s2, s3 = l*w,w*h,h*l
|
||||
return 2*(s1+s2+s3)+min([s1,s2,s3])
|
||||
|
||||
with open('dimensions.txt') as filein:
|
||||
lines = filein.readlines()
|
||||
# print([surface(line) for line in ["2x3x4","1x1x10"]])
|
||||
|
||||
print(sum([surface(line) for line in lines]))
|
||||
|
||||
"""
|
||||
The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact.
|
||||
|
||||
The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don't ask how they tie the bow, though; they'll never tell.
|
||||
|
||||
For example:
|
||||
|
||||
- A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of ribbon to wrap the present plus 2*3*4 = 24 feet of ribbon for the bow, for a total of 34 feet.
|
||||
- A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of ribbon to wrap the present plus 1*1*10 = 10 feet of ribbon for the bow, for a total of 14 feet.
|
||||
|
||||
How many total feet of ribbon should they order?
|
||||
"""
|
||||
import numpy as np
|
||||
def ribbon(dimensions:str):
|
||||
l,w,h = [int(i) for i in dimensions.strip().split('x')]
|
||||
perimiter = min(np.array([l+w,w+h,h+l])*2)
|
||||
|
||||
return perimiter + l*w*h
|
||||
|
||||
print(sum([ribbon(line) for line in lines]))
|
||||
1000
2015/2/dimensions.txt
Normal file
1000
2015/2/dimensions.txt
Normal file
File diff suppressed because it is too large
Load diff
3
2015/intro.md
Normal file
3
2015/intro.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect **fifty stars** by December 25th.
|
||||
|
||||
Collect stars by helping Santa solve 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!
|
||||
Loading…
Add table
Reference in a new issue