[py] Simplify 2015_03
This commit is contained in:
parent
f6198637a8
commit
18b58fd936
5 changed files with 15 additions and 39 deletions
|
|
@ -1,46 +1,16 @@
|
||||||
# PART 1
|
def santa(directions):
|
||||||
|
|
||||||
|
|
||||||
def find_doubles(steps):
|
|
||||||
x, y = 0, 0
|
x, y = 0, 0
|
||||||
houses = {(0, 0)}
|
houses = {(0, 0)}
|
||||||
for step in steps:
|
for d in directions:
|
||||||
if step == "^":
|
dx, dy = {"^": (0, 1), "v": (0, -1), "<": (-1, 0), ">": (1, 0)}[d]
|
||||||
y += 1
|
x, y = x + dx, y + dy
|
||||||
elif step == "v":
|
|
||||||
y -= 1
|
|
||||||
elif step == "<":
|
|
||||||
x -= 1
|
|
||||||
elif step == ">":
|
|
||||||
x += 1
|
|
||||||
houses.add((x, y))
|
houses.add((x, y))
|
||||||
return houses
|
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):
|
def solve(inputstr):
|
||||||
steps = inputstr.strip()
|
directions = inputstr.strip()
|
||||||
doubles = len(find_doubles(steps))
|
part1 = len(santa(directions))
|
||||||
print(f"Part 1: {doubles}")
|
print(f"Part 1: {part1}")
|
||||||
santa, robot = split_string(steps)
|
part2 = len(santa(directions[::2]) | santa(directions[1::2]))
|
||||||
santa_doubles = find_doubles(santa)
|
print(f"Part 2: {part2}")
|
||||||
robot_doubles = find_doubles(robot)
|
|
||||||
doubles_2 = len(santa_doubles | robot_doubles)
|
|
||||||
print(f"Part 2: {doubles_2}")
|
|
||||||
|
|
|
||||||
1
sample_inputs/2015/2015_03.01.input
Normal file
1
sample_inputs/2015/2015_03.01.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
^>v<
|
||||||
2
sample_inputs/2015/2015_03.01.solution
Normal file
2
sample_inputs/2015/2015_03.01.solution
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Part 1: 4
|
||||||
|
Part 2: 3
|
||||||
1
sample_inputs/2015/2015_03.02.input
Normal file
1
sample_inputs/2015/2015_03.02.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
^v^v^v^v^v
|
||||||
2
sample_inputs/2015/2015_03.02.solution
Normal file
2
sample_inputs/2015/2015_03.02.solution
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Part 1: 2
|
||||||
|
Part 2: 11
|
||||||
Loading…
Add table
Add a link
Reference in a new issue