[py] Port 2017_03
This commit is contained in:
parent
7ac215b493
commit
d3d8a493f1
3 changed files with 69 additions and 65 deletions
|
|
@ -2,7 +2,7 @@ import sys
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from .y2017 import d01, d02
|
||||
from .y2017 import d01, d02, d03
|
||||
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
||||
from .y2020 import d10
|
||||
from .y2021 import d14
|
||||
|
|
@ -11,6 +11,7 @@ from .y2022 import d01, d02, d03, d04, d05, d06
|
|||
DAYS = {
|
||||
"2017_01": y2017.d01.solve,
|
||||
"2017_02": y2017.d02.solve,
|
||||
"2017_03": y2017.d03.solve,
|
||||
"2018_01": y2018.d01.solve,
|
||||
"2018_02": y2018.d02.solve,
|
||||
"2018_03": y2018.d03.solve,
|
||||
|
|
|
|||
67
py/aoc/y2017/d03.py
Normal file
67
py/aoc/y2017/d03.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
# PART 1
|
||||
|
||||
|
||||
def spiral(nth):
|
||||
length = 1
|
||||
nth -= 1
|
||||
x = 0
|
||||
y = 0
|
||||
while True:
|
||||
x += min(length, nth)
|
||||
nth -= min(length, nth)
|
||||
if nth == 0:
|
||||
return x, y
|
||||
y += min(length, nth)
|
||||
nth -= min(length, nth)
|
||||
if nth == 0:
|
||||
return x, y
|
||||
|
||||
length += 1
|
||||
|
||||
x -= min(length, nth)
|
||||
nth -= min(length, nth)
|
||||
if nth == 0:
|
||||
return x, y
|
||||
y -= min(length, nth)
|
||||
nth -= min(length, nth)
|
||||
if nth == 0:
|
||||
return x, y
|
||||
|
||||
length += 1
|
||||
|
||||
|
||||
def manhattan(x, y):
|
||||
return abs(x) + abs(y)
|
||||
|
||||
|
||||
# PART 2
|
||||
|
||||
|
||||
def store(cap):
|
||||
stored = {(0, 0): 1}
|
||||
nth = 2
|
||||
while True:
|
||||
x, y = spiral(nth)
|
||||
adjacent = 0
|
||||
adjacent += stored.get((x - 1, y - 1), 0)
|
||||
adjacent += stored.get((x, y - 1), 0)
|
||||
adjacent += stored.get((x + 1, y - 1), 0)
|
||||
adjacent += stored.get((x - 1, y), 0)
|
||||
adjacent += stored.get((x + 1, y), 0)
|
||||
adjacent += stored.get((x - 1, y + 1), 0)
|
||||
adjacent += stored.get((x, y + 1), 0)
|
||||
adjacent += stored.get((x + 1, y + 1), 0)
|
||||
stored[(x, y)] = adjacent
|
||||
nth += 1
|
||||
|
||||
if adjacent > cap:
|
||||
return adjacent
|
||||
|
||||
|
||||
def solve(inputstr):
|
||||
nth = int(inputstr.strip())
|
||||
x, y = spiral(nth)
|
||||
dist = manhattan(x, y)
|
||||
print(f"Part 1: {dist}")
|
||||
adjacent = store(nth)
|
||||
print(f"Part 2: {adjacent}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue