diff --git a/solutions/2/README.md b/solutions/2/README.md index 9821e20..f5c36c3 100644 --- a/solutions/2/README.md +++ b/solutions/2/README.md @@ -5,13 +5,6 @@ Just some simple parsing. The second one is similar, but we can be more efficient if we XOR the first and second position. -# 2 - -For the first we can simply count the occurrences and see if they are between the accepted values. -Just some simple parsing. - -The second one is similar, but we can be more efficient if we XOR the first and second position. -
Solutions
    diff --git a/solutions/3/README.md b/solutions/3/README.md new file mode 100644 index 0000000..6392e0f --- /dev/null +++ b/solutions/3/README.md @@ -0,0 +1,16 @@ +# 3 + +We can simply parse the forest as an array of strings. +The trees repeat infinitely to the right, this screams for a good mod. +This means: `char = row[x % len(row)]`. No complex logic needed + +For the second one we simply automate the process and sum up the total. +We can simply encode the coordinates as a function of the index we are current at. + +
    + Solutions +
      +
    1. 252
    2. +
    3. 57 * 252 * 64 * 66 * 43 = 2608962048
    4. +
    +
    diff --git a/solutions/3/main.py b/solutions/3/main.py new file mode 100644 index 0000000..a754dcf --- /dev/null +++ b/solutions/3/main.py @@ -0,0 +1,50 @@ +from typing import Dict, List +from os.path import join, dirname +from functools import reduce + + +class Forest(): + + def __init__(self, text: str) -> None: + self.text = text.strip().split('\n') + + @property + def height(self) -> int: + return len(self.text) + + def is_tree_at(self, y: int, x: int) -> bool: + if y > self.height: + return False + row = self.text[y] + return row[x % len(row)] == '#' + + +data = join(dirname(__file__), 'data.txt') +with open(data) as f: + forest = Forest(f.read()) + + # 1 + trees: int = 0 + for y in range(forest.height): + is_tree: bool = forest.is_tree_at(y, y*3) + if is_tree: + trees += 1 + print(f'Result Simple: {trees}') + + # 2 + all: Dict[str, int] = { + '11': 0, + '13': 0, + '15': 0, + '17': 0, + '21': 0, + } + for i in range(forest.height): + for key, value in all.items(): + dy, dx = map(int, list(key)) + y = i * dy + x = i * dx + if forest.is_tree_at(y, x): + all[key] += 1 + total = reduce((lambda x, y: x * y), all.values()) + print(f'Result Combined: {list(all.values())} = {total}')