Skip to content

Commit

Permalink
add testplan for day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
vinvinvincent committed Dec 10, 2023
1 parent 8cff5e4 commit 94ec663
Show file tree
Hide file tree
Showing 13 changed files with 953 additions and 814 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
.DS_Store
__pycache__
.pytest_cache
.coverage

# virtual env
venv

# testplan report
report.json
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Hello! 👋
This is my [Advent of Code 2023](https://adventofcode.com/2023/) project! I code with python and test with [testplan](https://github.com/morganstanley/testplan).

## How to Run
### testplan
```sh
python tests/test_plan.py

# display testplan report
tpsreport display fromjson report/report.json
```

### testplan with coverage
```sh
python tests/test_plan.py --coverage
coverage report -m
coverage html

# now coverage report is under htmlcov/index.html
```

## License
See [License](LICENSE)
Empty file added aoc2023/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions aoc2023/day8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Part1:
NODE_KEY='AAA'
MATCH_LAMBDA=lambda v: v == 'ZZZ'

@classmethod
def total_steps(cls, instructions, nodes, node_key=NODE_KEY, match_lambda=MATCH_LAMBDA):
'''
get total steps from walking instructions and nodes
using node_key as initial starting node
and end when node name matches with match_lambda
'''
total_steps=0

while True:
for i in range(len(instructions)):
total_steps += 1
instruction = instructions[i]
current_node = nodes[node_key]

if instruction == 'L':
node_key = current_node[0]
else:
node_key = current_node[1]

if match_lambda(node_key):
return total_steps

class Part2:
MATCH_LAMBDA=lambda v: v.endswith('Z')

@classmethod
def get_node_keys(cls, nodes):
return [x for x in nodes.keys() if x.endswith('A')]

@classmethod
def total_steps(cls, instructions, nodes, node_keys, match_lambda=MATCH_LAMBDA):
min_steps_per_node_key = [Part1.total_steps(instructions, nodes, node_key, match_lambda) for node_key in node_keys]

import math
return math.lcm(*min_steps_per_node_key)
Loading

0 comments on commit 94ec663

Please sign in to comment.