[py] Port 2015_03
This commit is contained in:
parent
cfc022dad8
commit
3b7bf7340f
3 changed files with 48 additions and 48 deletions
|
|
@ -1,47 +0,0 @@
|
|||
import sys
|
||||
|
||||
def load_steps(filename):
|
||||
with open(filename, "r") as f:
|
||||
return f.read()[:-1]
|
||||
|
||||
# PART 1
|
||||
|
||||
def find_doubles(steps):
|
||||
x, y = 0, 0
|
||||
houses = {(0, 0)}
|
||||
for step in steps:
|
||||
if step == "^": y += 1
|
||||
elif step == "v": y -= 1
|
||||
elif step == "<": x -= 1
|
||||
elif step == ">": x += 1
|
||||
houses.add((x, y))
|
||||
return houses
|
||||
|
||||
# PART 2
|
||||
|
||||
def split_string(s):
|
||||
s1 = ""
|
||||
s2 = ""
|
||||
while True:
|
||||
if not s: break
|
||||
s1 += s[0]
|
||||
s = s[1:]
|
||||
if not s: break
|
||||
s2 += s[0]
|
||||
s = s[1:]
|
||||
return s1, s2
|
||||
|
||||
def main(filename):
|
||||
steps = load_steps(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
doubles = len(find_doubles(steps))
|
||||
print(f"Part 1: {doubles}")
|
||||
santa, robot = split_string(steps)
|
||||
santa_doubles = find_doubles(santa)
|
||||
robot_doubles = find_doubles(robot)
|
||||
doubles_2 = len(santa_doubles | robot_doubles)
|
||||
print(f"Part 2: {doubles_2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
|
|
@ -2,7 +2,7 @@ import sys
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from .y2015 import d01, d02
|
||||
from .y2015 import d01, d02, d03
|
||||
from .y2016 import d01, d02, d03, d04, d05
|
||||
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
|
||||
|
|
@ -13,6 +13,7 @@ from .y2022 import d01, d02, d03, d04, d05, d06
|
|||
DAYS = {
|
||||
"2015_01": y2015.d01.solve,
|
||||
"2015_02": y2015.d02.solve,
|
||||
"2015_03": y2015.d03.solve,
|
||||
"2016_01": y2016.d01.solve,
|
||||
"2016_02": y2016.d02.solve,
|
||||
"2016_03": y2016.d03.solve,
|
||||
|
|
|
|||
46
py/aoc/y2015/d03.py
Normal file
46
py/aoc/y2015/d03.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# PART 1
|
||||
|
||||
|
||||
def find_doubles(steps):
|
||||
x, y = 0, 0
|
||||
houses = {(0, 0)}
|
||||
for step in steps:
|
||||
if step == "^":
|
||||
y += 1
|
||||
elif step == "v":
|
||||
y -= 1
|
||||
elif step == "<":
|
||||
x -= 1
|
||||
elif step == ">":
|
||||
x += 1
|
||||
houses.add((x, y))
|
||||
return houses
|
||||
|
||||
|
||||
# PART 2
|
||||
|
||||
|
||||
def split_string(s):
|
||||
s1 = ""
|
||||
s2 = ""
|
||||
while True:
|
||||
if not s:
|
||||
break
|
||||
s1 += s[0]
|
||||
s = s[1:]
|
||||
if not s:
|
||||
break
|
||||
s2 += s[0]
|
||||
s = s[1:]
|
||||
return s1, s2
|
||||
|
||||
|
||||
def solve(inputstr):
|
||||
steps = inputstr.strip()
|
||||
doubles = len(find_doubles(steps))
|
||||
print(f"Part 1: {doubles}")
|
||||
santa, robot = split_string(steps)
|
||||
santa_doubles = find_doubles(santa)
|
||||
robot_doubles = find_doubles(robot)
|
||||
doubles_2 = len(santa_doubles | robot_doubles)
|
||||
print(f"Part 2: {doubles_2}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue