Skip to content

Commit

Permalink
Replaced eval with json
Browse files Browse the repository at this point in the history
  • Loading branch information
npanuhin committed Dec 13, 2023
1 parent 28435a2 commit ea94e92
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
3 changes: 2 additions & 1 deletion 2021/Day 18/part1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import reduce
import json

SnailfishNumber = list[int]

Expand Down Expand Up @@ -97,6 +98,6 @@ def magnitude(number: SnailfishNumber) -> int:


with open("input.txt") as file:
numbers = list(map(eval, filter(None, map(str.strip, file))))
numbers = list(map(json.loads, filter(None, map(str.strip, file))))

print(magnitude(reduce(add, numbers)))
3 changes: 2 additions & 1 deletion 2021/Day 18/part2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
import json

SnailfishNumber = list[int]

Expand Down Expand Up @@ -103,7 +104,7 @@ def magnitude(number: SnailfishNumber) -> int:


with open("input.txt") as file:
numbers = list(map(eval, filter(None, map(str.strip, file))))
numbers = list(map(json.loads, filter(None, map(str.strip, file))))

print(max(
magnitude(save_add(a, b))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ I try to optimize each solution as much as possible, so although they are writte

#### I also follow some rules for writing my solutions:

- The entire repository is [PEP8](https://pep8.org/) compliant, as [verified](../../actions/workflows/lint.yaml) by the `flake8` linter (with the [exception of some rules](tox.ini#L21-L52))
- The entire repository is [PEP8](https://pep8.org/) compliant, as [verified](../../actions/workflows/lint.yaml) by the `flake8` linter (with the [exception of some rules](tox.ini#L21-L48))

- **Input files may contain any number of empty lines**, especially at the end. In some cases, this also applies to whitespace characters.<br>
Currently I like to handle this rule using the following general snippet to read files:
Expand Down
6 changes: 1 addition & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require-plugins=
; flake8-string-format
pep8-naming

inline-quotes='"'
; inline-quotes='"'
max-line-length=120
ignore=

Expand All @@ -43,10 +43,6 @@ ignore=
; I mostly follow these rules, but since I want to keep solutions code clean,
; I want to add neither extra lines of code nor `# noqa` comments to comply with the linter

; Use of "eval" is insecure
; (For some puzzles, `eval` comes in handy for quickly parsing input data)
DUO104

; Variable in global scope should not be mixedCase
; (Sometimes, I want to use variables in the same case as they were in the puzzle statement)
N816
Expand Down

0 comments on commit ea94e92

Please sign in to comment.