Code formatting

black
go fmt
This commit is contained in:
akp 2020-12-06 19:25:39 +00:00
parent e2393f03f1
commit 229174dffe
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
39 changed files with 142 additions and 89 deletions

4
.github/tablegen.py vendored
View file

@ -23,7 +23,7 @@ for line in readme_text:
for i, l in enumerate(table_lines):
rs = re.match(r"\|\s*(\d{1,})\s*\|\s*\|.+\|.+\|", l)
if rs is not None and int(rs.group(1)) == today_day:
table_lines[i] = f"| {today_day} | ![Not yet attempted][pending] | | |"
counter = 0
@ -38,4 +38,4 @@ for i, line in enumerate(readme_text):
elif line == start_flag:
in_table = True
open(".github/README.md", "w").write("\n".join(readme_text))
open(".github/README.md", "w").write("\n".join(readme_text))

View file

@ -10,6 +10,7 @@ year = "2020"
day = "1"
title = "Report Repair"
def run_tests():
try:
test_cases = open("testCases.json").read()
@ -36,7 +37,8 @@ def run_tests():
print()
if __name__ == "__main__":
if __name__ == "__main__":
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}\n")
try:

View file

@ -1,4 +1,5 @@
from typing import List
def parse(instr:str) -> List:
def parse(instr: str) -> List:
return [int(x) for x in instr.strip().split("\n")]

View file

@ -1,6 +1,7 @@
from common import *
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
values = parse(instr)
for i in values:

View file

@ -1,6 +1,7 @@
from common import *
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
values = parse(instr)
for i in values:

View file

@ -11,6 +11,7 @@ year = "2020"
day = "2"
title = "Password Philosophy"
def run_tests():
try:
test_cases = open("testCases.json").read()
@ -37,7 +38,8 @@ def run_tests():
print()
if __name__ == "__main__":
if __name__ == "__main__":
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
print(f"Python {platform.python_version()}\n")

View file

@ -1,4 +1,5 @@
from typing import List
def parse(instr:str) -> List:
def parse(instr: str) -> List:
return instr.strip().split("\n")

View file

@ -2,7 +2,8 @@ import re
from common import *
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
input_string = parse(instr)
class Password:
@ -11,7 +12,9 @@ def partOne(instr:str) -> int:
min_repeats: int
max_repeats: int
def __init__(self, plaintext:str, target_letter:str, min_repeats:int, max_repeats:int) -> None:
def __init__(
self, plaintext: str, target_letter: str, min_repeats: int, max_repeats: int
) -> None:
self.plaintext = plaintext
self.target_letter = target_letter
self.min_repeats = min_repeats
@ -23,7 +26,9 @@ def partOne(instr:str) -> int:
for line in input_string:
m = re.match(parser_regex, line)
passwords.append(Password(m.group(4), m.group(3), int(m.group(1)), int(m.group(2))))
passwords.append(
Password(m.group(4), m.group(3), int(m.group(1)), int(m.group(2)))
)
num_valid_passwords = 0
@ -35,5 +40,5 @@ def partOne(instr:str) -> int:
if password.min_repeats <= target_letter_count <= password.max_repeats:
num_valid_passwords += 1
return num_valid_passwords

View file

@ -2,7 +2,8 @@ import re
from common import *
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
input_string = parse(instr)
class Password:
@ -11,7 +12,13 @@ def partTwo(instr:str) -> int:
position_one: int
position_two: int
def __init__(self, plaintext:str, target_letter:str, position_one:int, position_two:int) -> None:
def __init__(
self,
plaintext: str,
target_letter: str,
position_one: int,
position_two: int,
) -> None:
self.plaintext = plaintext
self.target_letter = target_letter
self.position_one = position_one - 1 # No concept of index zero... eurgh
@ -23,15 +30,21 @@ def partTwo(instr:str) -> int:
for line in input_string:
m = re.match(parser_regex, line)
passwords.append(Password(m.group(4), m.group(3), int(m.group(1)), int(m.group(2))))
passwords.append(
Password(m.group(4), m.group(3), int(m.group(1)), int(m.group(2)))
)
num_valid_passwords = 0
for password in passwords:
position_one_matches = password.plaintext[password.position_one] == password.target_letter
position_two_matches = password.plaintext[password.position_two] == password.target_letter
position_one_matches = (
password.plaintext[password.position_one] == password.target_letter
)
position_two_matches = (
password.plaintext[password.position_two] == password.target_letter
)
if position_one_matches ^ position_two_matches:
num_valid_passwords += 1
num_valid_passwords += 1
return num_valid_passwords

View file

@ -1,5 +1,5 @@
package challenge
func PartOne(instr string) int {
func PartOne(instr string) int {
return findCollisions(parse(instr), 3, 1)
}
}

View file

@ -11,6 +11,7 @@ year = "2020"
day = "3"
title = "Toboggan Trajectory"
def run_tests():
try:
test_cases = open("testCases.json").read()
@ -37,7 +38,8 @@ def run_tests():
print()
if __name__ == "__main__":
if __name__ == "__main__":
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
print(f"Python {platform.python_version()}\n")

View file

@ -2,10 +2,12 @@ from typing import List
tree_char = "#"
def parse(instr:str) -> List:
def parse(instr: str) -> List:
return [[char for char in line] for line in instr.strip().split("\n")]
def find_collisions(forest:list, x_offset:int, y_offset:int) -> int:
def find_collisions(forest: list, x_offset: int, y_offset: int) -> int:
encountered_trees = 0
x_pointer = 0

View file

@ -1,4 +1,5 @@
from common import *
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
return find_collisions(parse(instr), 3, 1)

View file

@ -1,20 +1,15 @@
from common import *
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
forest = parse(instr)
tree_product = 1
offset_pairs = [
(3, 1),
(1, 1),
(5, 1),
(7, 1),
(1, 2)
]
offset_pairs = [(3, 1), (1, 1), (5, 1), (7, 1), (1, 2)]
for i, pair in enumerate(offset_pairs):
encountered_trees = find_collisions(forest, *pair)
tree_product *= encountered_trees
return tree_product

View file

@ -11,6 +11,7 @@ year = "2020"
day = "4"
title = "Passport Processing"
def run_tests():
try:
test_cases = open("testCases.json").read()
@ -37,7 +38,8 @@ def run_tests():
print()
if __name__ == "__main__":
if __name__ == "__main__":
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
print(f"Python {platform.python_version()}\n")

View file

@ -1,4 +1,5 @@
from typing import List
def parse(instr:str) -> List:
def parse(instr: str) -> List:
return [x.replace("\n", " ") for x in instr.strip().split("\n\n")]

View file

@ -2,7 +2,8 @@ import re
from common import *
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
input_list = parse(instr)
test_cases = [
@ -14,7 +15,7 @@ def partOne(instr:str) -> int:
[r"hcl:([^ ]+)", False],
[r"ecl:([^ ]+)", False],
[r"pid:([^ ]+)", False],
[r"cid:([^ ]+)", True]
[r"cid:([^ ]+)", True],
]
valid_passports = 0

View file

@ -2,7 +2,8 @@ import re
from common import *
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
input_list = parse(instr)
passports = [Passport(x) for x in input_list]
@ -14,7 +15,8 @@ def partTwo(instr:str) -> int:
return valid_passports
class Passport:
class Passport:
byr: str # Birth year
iyr: str # Issue year
eyr: str # Expiration year
@ -24,7 +26,7 @@ class Passport:
pid: str # Passport ID
cid: str # Country ID
def __init__(self, instr:str) -> None:
def __init__(self, instr: str) -> None:
self.byr = self._extract_field("byr", instr)
self.iyr = self._extract_field("iyr", instr)
self.eyr = self._extract_field("eyr", instr)
@ -34,11 +36,11 @@ class Passport:
self.pid = self._extract_field("pid", instr)
self.cid = self._extract_field("cid", instr)
def _extract_field(self, field:str, instr:str) -> str:
def _extract_field(self, field: str, instr: str) -> str:
matches = re.search(field + r":([^ ]+)", instr)
if matches is None:
return ""
return matches.group(1)
def validate(self) -> bool:
@ -53,7 +55,7 @@ class Passport:
return False
if not (2010 <= int(self.iyr) <= 2020):
return False
# eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
if self.eyr == "":
return False
@ -98,4 +100,4 @@ class Passport:
else:
return False
return True
return True

View file

@ -1,6 +1,6 @@
package challenge
func PartOne(instr string) int {
func PartOne(instr string) int {
inputSlice := parse(instr)
var highestSeatId int
@ -13,4 +13,4 @@ func PartOne(instr string) int {
}
return highestSeatId
}
}

View file

@ -1,6 +1,6 @@
package challenge
func PartTwo(instr string) int {
func PartTwo(instr string) int {
inputSlice := parse(instr)
var seatMatrix [numRows][numCols]bool
@ -17,7 +17,7 @@ func PartTwo(instr string) int {
for row := 0; row < len(seatMatrix); row += 1 {
for col := 0; col < len(seatMatrix[row]); col += 1 {
this := seatMatrix[row][col]
if (lastTwo && !lastOne && this) {
if lastTwo && !lastOne && this {
// We need to get the previous item because at this point, we've already moved on one
prevRow := row
prevCol := col - 1
@ -26,11 +26,11 @@ func PartTwo(instr string) int {
prevCol += numCols
}
return getSeatId(prevRow, prevCol)
}
}
lastTwo = lastOne
lastOne = this
}
}
return 0
}
}

View file

@ -66,9 +66,9 @@ type tc struct {
}
type info struct {
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
TestCases struct {
One []tc `json:"one"`
Two []tc `json:"two"`

View file

@ -7,6 +7,7 @@ from rich import print
from partOne import partOne
from partTwo import partTwo
def run_tests(test_cases):
do_tests = True
if len(test_cases) == 0:
@ -35,7 +36,8 @@ def run_tests(test_cases):
print()
if __name__ == "__main__":
if __name__ == "__main__":
try:
info = open("info.json").read()
except FileNotFoundError:
@ -43,7 +45,7 @@ if __name__ == "__main__":
sys.exit(-1)
info = json.loads(info)
year = info["year"]
day = info["day"]
title = info["title"]

View file

@ -8,15 +8,17 @@ right = "R"
num_rows = 128
num_cols = 8
def parse(instr:str) -> List:
def parse(instr: str) -> List:
return instr.strip().split("\n")
def decode_position(row_string:str, dec_char:str, inc_char:str, max_val:int) -> int:
def decode_position(row_string: str, dec_char: str, inc_char: str, max_val: int) -> int:
min_val = 0
max_val -= 1
current_range = (max_val + 1) - min_val
for char in row_string.upper():
range_modifier = current_range / 2
@ -33,11 +35,13 @@ def decode_position(row_string:str, dec_char:str, inc_char:str, max_val:int) ->
else:
return max_val
def parse_seat(seat_string:str) -> Tuple[int, int]:
def parse_seat(seat_string: str) -> Tuple[int, int]:
row = decode_position(seat_string[:7], front, back, num_rows)
col = decode_position(seat_string[7:], left, right, num_cols)
return int(row), int(col)
def get_seat_id(row:int, col:int) -> int:
def get_seat_id(row: int, col: int) -> int:
return (row * 8) + col

View file

@ -1,6 +1,7 @@
from common import *
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
input_list = parse(instr)
highest_seat_id = 0

View file

@ -1,6 +1,7 @@
from common import *
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
input_list = parse(instr)
# Build a matrix to represent the entire plane
@ -22,7 +23,7 @@ def partTwo(instr:str) -> int:
for row in range(len(seat_matrix)):
for col in range(len(seat_matrix[row])):
this = seat_matrix[row][col]
if [lastTwo, lastOne, this] == [True, False, True]:

View file

@ -8,7 +8,7 @@ func parse(instr string) []string {
type Group struct {
Questions []rune
NumPax int
NumPax int
}
func IsRuneInSlice(r rune, s []rune) bool {

View file

@ -60,9 +60,9 @@ type tc struct {
}
type info struct {
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
TestCases struct {
One []tc `json:"one"`
Two []tc `json:"two"`

View file

@ -7,6 +7,7 @@ from rich import print
from partOne import partOne
from partTwo import partTwo
def run_tests(test_cases):
do_tests = True
if len(test_cases) == 0:
@ -35,7 +36,8 @@ def run_tests(test_cases):
print()
if __name__ == "__main__":
if __name__ == "__main__":
try:
info = open("info.json").read()
except FileNotFoundError:
@ -43,7 +45,7 @@ if __name__ == "__main__":
sys.exit(-1)
info = json.loads(info)
year = info["year"]
day = info["day"]
title = info["title"]

View file

@ -1,4 +1,5 @@
from typing import List
def parse(instr:str) -> List[str]:
def parse(instr: str) -> List[str]:
return instr.strip().split("\n\n")

View file

@ -1,10 +1,11 @@
from common import *
class Group:
questions: List[str]
num_pax: int
def __init__(self, instr:str) -> None:
def __init__(self, instr: str) -> None:
individual_pax = instr.split("\n")
self.num_pax = len(individual_pax)
@ -15,7 +16,8 @@ class Group:
if char not in self.questions:
self.questions.append(char)
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
groups = [Group(x) for x in parse(instr)]
question_total = 0

View file

@ -1,7 +1,8 @@
from common import *
from typing import Dict
def check_char(aq:Dict[int, List[str]], char:str) -> bool:
def check_char(aq: Dict[int, List[str]], char: str) -> bool:
is_in_all = True
for key in aq:
val = aq[key]
@ -10,11 +11,12 @@ def check_char(aq:Dict[int, List[str]], char:str) -> bool:
break
return is_in_all
class Group:
questions: List[str]
num_pax: int
def __init__(self, instr:str) -> None:
def __init__(self, instr: str) -> None:
individual_pax = instr.split("\n")
self.num_pax = len(individual_pax)
self.questions = []
@ -30,7 +32,8 @@ class Group:
if char not in self.questions:
self.questions.append(char)
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
groups = [Group(x) for x in parse(instr)]
question_total = 0

View file

@ -2,4 +2,4 @@ package challenge
func parse(instr string) []string {
return []string{}
}
}

View file

@ -1,6 +1,6 @@
package challenge
func PartOne(instr string) int {
func PartOne(instr string) int {
// inputSlice := parse(instr)
return 0
}
}

View file

@ -1,6 +1,6 @@
package challenge
func PartTwo(instr string) int {
func PartTwo(instr string) int {
// inputSlice := parse(instr)
return 0
}
}

View file

@ -60,9 +60,9 @@ type tc struct {
}
type info struct {
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
TestCases struct {
One []tc `json:"one"`
Two []tc `json:"two"`

View file

@ -7,6 +7,7 @@ from rich import print
from partOne import partOne
from partTwo import partTwo
def run_tests(test_cases):
do_tests = True
if len(test_cases) == 0:
@ -35,7 +36,8 @@ def run_tests(test_cases):
print()
if __name__ == "__main__":
if __name__ == "__main__":
try:
info = open("info.json").read()
except FileNotFoundError:
@ -43,7 +45,7 @@ if __name__ == "__main__":
sys.exit(-1)
info = json.loads(info)
year = info["year"]
day = info["day"]
title = info["title"]

View file

@ -1,4 +1,5 @@
from typing import List
def parse(instr:str) -> List:
def parse(instr: str) -> List:
return []

View file

@ -1,5 +1,6 @@
from common import *
def partOne(instr:str) -> int:
def partOne(instr: str) -> int:
input_list = parse(instr)
return 0

View file

@ -1,5 +1,6 @@
from common import *
def partTwo(instr:str) -> int:
def partTwo(instr: str) -> int:
input_list = parse(instr)
return 0