move to 2020 fodler

This commit is contained in:
2021-12-01 11:43:46 +01:00
parent eb251ac1ea
commit c651a48895
33 changed files with 0 additions and 0 deletions

View 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>

View File

@@ -0,0 +1,66 @@
package main
import (
"fmt"
"io/ioutil"
"strings"
)
const high = rune('#')
type sForest struct {
data [][]bool
height int
width int
}
func (f sForest) at(y, x int) bool {
if y >= f.height {
return false
}
return f.data[y][x%f.width]
}
func (f sForest) traverse(y, x int) int {
trees := 0
for dy := 0; dy <= f.height; dy++ {
tree := f.at(dy*y, dy*x)
if tree {
trees++
}
}
return trees
}
func main() {
data, _ := ioutil.ReadFile("./solutions/3/data.txt")
rows := strings.Split(strings.TrimSpace(string(data)), "\n")
height, width := len(rows), len(rows[0])
d := make([][]bool, height)
for y, row := range rows {
d[y] = make([]bool, width)
for x, char := range []rune(row) {
d[y][x] = char == high
}
}
forest := sForest{
data: d,
height: height,
width: width,
}
fmt.Println("Simple: ", forest.traverse(1, 3))
trees11 := forest.traverse(1, 1)
trees13 := forest.traverse(1, 3)
trees15 := forest.traverse(1, 5)
trees17 := forest.traverse(1, 7)
trees21 := forest.traverse(2, 1)
fmt.Println(trees11, trees13, trees15, trees17, trees21)
fmt.Println(trees11 * trees13 * trees15 * trees17 * trees21)
}

View File

@@ -0,0 +1,50 @@
from typing import Dict
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}')