diff --git a/challenges/2020/03-tobogganTrajectory/README.md b/challenges/2020/03-tobogganTrajectory/README.md new file mode 100644 index 0000000..47cdd89 --- /dev/null +++ b/challenges/2020/03-tobogganTrajectory/README.md @@ -0,0 +1,2 @@ +# [Day 3: Toboggan Trajactory](https://adventofcode.com/2021/day/3) + diff --git a/challenges/2020/03-tobogganTrajectory/benchmark.txt b/challenges/2020/03-tobogganTrajectory/benchmark.txt new file mode 100644 index 0000000..101bc68 --- /dev/null +++ b/challenges/2020/03-tobogganTrajectory/benchmark.txt @@ -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 +-------------------------------------------------------------------------------- diff --git a/challenges/2020/03-tobogganTrajectory/go/challenge.go b/challenges/2020/03-tobogganTrajectory/go/challenge.go new file mode 100644 index 0000000..04b861f --- /dev/null +++ b/challenges/2020/03-tobogganTrajectory/go/challenge.go @@ -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 +} diff --git a/challenges/2020/03-tobogganTrajectory/info.json b/challenges/2020/03-tobogganTrajectory/info.json new file mode 100644 index 0000000..ec2a287 --- /dev/null +++ b/challenges/2020/03-tobogganTrajectory/info.json @@ -0,0 +1,4 @@ +{ + "inputFile": "input.txt", + "testCases": {} +} \ No newline at end of file diff --git a/challenges/2020/03-tobogganTrajectory/py/__init__.py b/challenges/2020/03-tobogganTrajectory/py/__init__.py new file mode 100644 index 0000000..c8f37ca --- /dev/null +++ b/challenges/2020/03-tobogganTrajectory/py/__init__.py @@ -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