Add 2020-03

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-11-27 20:05:58 +00:00
parent c8fd7ad9ba
commit 1ad039bb13
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
5 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# [Day 3: Toboggan Trajactory](https://adventofcode.com/2021/day/3)

View file

@ -0,0 +1,23 @@
Day 3 (Toboggan Trajectory) benchmark
Dir: challenges/2020/03-tobogganTrajectory
Runs per part: 50
--------------------------------------------------------------------------------
Golang
benchmark.part.1.avg: 0.000101 seconds
benchmark.part.1.min: 0.000039 seconds
benchmark.part.1.max: 0.000569 seconds
benchmark.part.2.avg: 0.000089 seconds
benchmark.part.2.min: 0.000047 seconds
benchmark.part.2.max: 0.000229 seconds
--------------------------------------------------------------------------------
Python
benchmark.part.1.avg: 0.000542 seconds
benchmark.part.1.min: 0.000341 seconds
benchmark.part.1.max: 0.001299 seconds
benchmark.part.2.avg: 0.000756 seconds
benchmark.part.2.min: 0.000504 seconds
benchmark.part.2.max: 0.001462 seconds
--------------------------------------------------------------------------------

View file

@ -0,0 +1,66 @@
package challenge
import (
"strings"
"github.com/codemicro/adventOfCode/lib/aocgo"
)
func parse(instr string) [][]rune {
inputSlice := strings.Split(strings.TrimSpace(instr), "\n")
var forest [][]rune
for _, line := range inputSlice {
forest = append(forest, []rune(line))
}
return forest
}
var tree_char = []rune("#")[0] // No idea why I can't just do rune("#")
func findCollisions(forest [][]rune, xOffset, yOffset int) int {
var encounteredTrees int
var xPointer int
var yPointer int
for yPointer < len(forest) {
row := forest[yPointer]
targetIndex := xPointer % len(row)
if row[targetIndex] == tree_char {
encounteredTrees += 1
}
xPointer += xOffset
yPointer += yOffset
}
return encounteredTrees
}
type Challenge struct {
aocgo.BaseChallenge
}
func (c Challenge) One(instr string) (interface{}, error) {
return findCollisions(parse(instr), 3, 1), nil
}
func (c Challenge) Two(instr string) (interface{}, error) {
forest := parse(instr)
offsetPairs := [][]int{
{3, 1},
{1, 1},
{5, 1},
{7, 1},
{1, 2},
}
treeProduct := 1
for _, pair := range offsetPairs {
encounteredTrees := findCollisions(forest, pair[0], pair[1])
treeProduct *= encounteredTrees
}
return treeProduct, nil
}

View file

@ -0,0 +1,4 @@
{
"inputFile": "input.txt",
"testCases": {}
}

View file

@ -0,0 +1,41 @@
from typing import List
from aocpy import BaseChallenge
tree_char = "#"
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:
encountered_trees = 0
x_pointer = 0
for row in forest[::y_offset]:
target_index = x_pointer % len(row)
if row[target_index] == tree_char:
encountered_trees += 1
x_pointer += x_offset
return encountered_trees
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
return find_collisions(parse(instr), 3, 1)
@staticmethod
def two(instr: str) -> int:
forest = parse(instr)
tree_product = 1
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