mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-12-22 08:06:25 +00:00
day 3
This commit is contained in:
parent
57bec63bca
commit
e5abef45a3
@ -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.
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
|
16
solutions/3/README.md
Normal file
16
solutions/3/README.md
Normal file
@ -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.
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>252</li>
|
||||
<li>57 * 252 * 64 * 66 * 43 = 2608962048</li>
|
||||
</ol>
|
||||
</details>
|
50
solutions/3/main.py
Normal file
50
solutions/3/main.py
Normal file
@ -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}')
|
Loading…
Reference in New Issue
Block a user