From 18b58fd936e56df6dae9544223b83aa9bc19b94b Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 6 Dec 2022 21:19:53 +0100 Subject: [PATCH] [py] Simplify 2015_03 --- py/aoc/y2015/d03.py | 48 +++++--------------------- sample_inputs/2015/2015_03.01.input | 1 + sample_inputs/2015/2015_03.01.solution | 2 ++ sample_inputs/2015/2015_03.02.input | 1 + sample_inputs/2015/2015_03.02.solution | 2 ++ 5 files changed, 15 insertions(+), 39 deletions(-) create mode 100644 sample_inputs/2015/2015_03.01.input create mode 100644 sample_inputs/2015/2015_03.01.solution create mode 100644 sample_inputs/2015/2015_03.02.input create mode 100644 sample_inputs/2015/2015_03.02.solution diff --git a/py/aoc/y2015/d03.py b/py/aoc/y2015/d03.py index 79b52ba..09feba1 100644 --- a/py/aoc/y2015/d03.py +++ b/py/aoc/y2015/d03.py @@ -1,46 +1,16 @@ -# PART 1 - - -def find_doubles(steps): +def santa(directions): 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 + for d in directions: + dx, dy = {"^": (0, 1), "v": (0, -1), "<": (-1, 0), ">": (1, 0)}[d] + x, y = x + dx, y + dy 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}") + directions = inputstr.strip() + part1 = len(santa(directions)) + print(f"Part 1: {part1}") + part2 = len(santa(directions[::2]) | santa(directions[1::2])) + print(f"Part 2: {part2}") diff --git a/sample_inputs/2015/2015_03.01.input b/sample_inputs/2015/2015_03.01.input new file mode 100644 index 0000000..c59f4bf --- /dev/null +++ b/sample_inputs/2015/2015_03.01.input @@ -0,0 +1 @@ +^>v< diff --git a/sample_inputs/2015/2015_03.01.solution b/sample_inputs/2015/2015_03.01.solution new file mode 100644 index 0000000..7451432 --- /dev/null +++ b/sample_inputs/2015/2015_03.01.solution @@ -0,0 +1,2 @@ +Part 1: 4 +Part 2: 3 diff --git a/sample_inputs/2015/2015_03.02.input b/sample_inputs/2015/2015_03.02.input new file mode 100644 index 0000000..8a6e322 --- /dev/null +++ b/sample_inputs/2015/2015_03.02.input @@ -0,0 +1 @@ +^v^v^v^v^v diff --git a/sample_inputs/2015/2015_03.02.solution b/sample_inputs/2015/2015_03.02.solution new file mode 100644 index 0000000..8bcd962 --- /dev/null +++ b/sample_inputs/2015/2015_03.02.solution @@ -0,0 +1,2 @@ +Part 1: 2 +Part 2: 11