[py] Port 2015_02
This commit is contained in:
parent
01e2920a67
commit
cfc022dad8
3 changed files with 47 additions and 45 deletions
|
|
@ -1,44 +0,0 @@
|
||||||
import re
|
|
||||||
import sys
|
|
||||||
|
|
||||||
PACKET_RE = r"(\d+)x(\d+)x(\d+)\n"
|
|
||||||
|
|
||||||
def load_packets(filename):
|
|
||||||
packets = []
|
|
||||||
with open(filename, "r") as f:
|
|
||||||
for line in f:
|
|
||||||
match = re.fullmatch(PACKET_RE, line)
|
|
||||||
a, b, c = match.groups()
|
|
||||||
a, b, c = int(a), int(b), int(c)
|
|
||||||
packets.append((a, b, c))
|
|
||||||
return packets
|
|
||||||
|
|
||||||
# PART 1
|
|
||||||
|
|
||||||
def necessary_area(packet):
|
|
||||||
a, b, c = sorted(packet)
|
|
||||||
return 3*a*b + 2*a*c + 2*b*c
|
|
||||||
|
|
||||||
def total_wrapping_paper(packets):
|
|
||||||
return sum(map(necessary_area, packets))
|
|
||||||
|
|
||||||
# PART 2
|
|
||||||
|
|
||||||
def ribbon_length(packet):
|
|
||||||
a, b, c = sorted(packet)
|
|
||||||
return 2*a + 2*b + a*b*c
|
|
||||||
|
|
||||||
def total_ribbon_length(packets):
|
|
||||||
return sum(map(ribbon_length, packets))
|
|
||||||
|
|
||||||
def main(filename):
|
|
||||||
packets = load_packets(filename)
|
|
||||||
print(f"Solutions for {filename}")
|
|
||||||
total = total_wrapping_paper(packets)
|
|
||||||
print(f"Part 1: {total}")
|
|
||||||
total_2 = total_ribbon_length(packets)
|
|
||||||
print(f"Part 2: {total_2}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
for filename in sys.argv[1:]:
|
|
||||||
main(filename)
|
|
||||||
|
|
@ -2,7 +2,7 @@ import sys
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .y2015 import d01
|
from .y2015 import d01, d02
|
||||||
from .y2016 import d01, d02, d03, d04, d05
|
from .y2016 import d01, d02, d03, d04, d05
|
||||||
from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09
|
from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09
|
||||||
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
||||||
|
|
@ -12,6 +12,7 @@ from .y2022 import d01, d02, d03, d04, d05, d06
|
||||||
|
|
||||||
DAYS = {
|
DAYS = {
|
||||||
"2015_01": y2015.d01.solve,
|
"2015_01": y2015.d01.solve,
|
||||||
|
"2015_02": y2015.d02.solve,
|
||||||
"2016_01": y2016.d01.solve,
|
"2016_01": y2016.d01.solve,
|
||||||
"2016_02": y2016.d02.solve,
|
"2016_02": y2016.d02.solve,
|
||||||
"2016_03": y2016.d03.solve,
|
"2016_03": y2016.d03.solve,
|
||||||
|
|
|
||||||
45
py/aoc/y2015/d02.py
Normal file
45
py/aoc/y2015/d02.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
PACKET_RE = r"(\d+)x(\d+)x(\d+)"
|
||||||
|
|
||||||
|
|
||||||
|
def load_packets(inputstr):
|
||||||
|
packets = []
|
||||||
|
for line in inputstr.splitlines():
|
||||||
|
match = re.fullmatch(PACKET_RE, line)
|
||||||
|
a, b, c = match.groups()
|
||||||
|
a, b, c = int(a), int(b), int(c)
|
||||||
|
packets.append((a, b, c))
|
||||||
|
return packets
|
||||||
|
|
||||||
|
|
||||||
|
# PART 1
|
||||||
|
|
||||||
|
|
||||||
|
def necessary_area(packet):
|
||||||
|
a, b, c = sorted(packet)
|
||||||
|
return 3 * a * b + 2 * a * c + 2 * b * c
|
||||||
|
|
||||||
|
|
||||||
|
def total_wrapping_paper(packets):
|
||||||
|
return sum(map(necessary_area, packets))
|
||||||
|
|
||||||
|
|
||||||
|
# PART 2
|
||||||
|
|
||||||
|
|
||||||
|
def ribbon_length(packet):
|
||||||
|
a, b, c = sorted(packet)
|
||||||
|
return 2 * a + 2 * b + a * b * c
|
||||||
|
|
||||||
|
|
||||||
|
def total_ribbon_length(packets):
|
||||||
|
return sum(map(ribbon_length, packets))
|
||||||
|
|
||||||
|
|
||||||
|
def solve(inputstr):
|
||||||
|
packets = load_packets(inputstr)
|
||||||
|
total = total_wrapping_paper(packets)
|
||||||
|
print(f"Part 1: {total}")
|
||||||
|
total_2 = total_ribbon_length(packets)
|
||||||
|
print(f"Part 2: {total_2}")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue