Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-08 11:41:47 +00:00
parent 5c61b3acfb
commit 6becab8669
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
7 changed files with 137 additions and 13 deletions

View file

@ -3,20 +3,20 @@
"dir": "challenges/2022/06-tuningTrouble",
"implementations": {
"Nim": {
"part.1.avg": 0.0007432498970000007,
"part.1.max": 0.002544188,
"part.1.min": 0.000169445,
"part.2.avg": 0.003928926078999996,
"part.2.max": 0.008833809,
"part.2.min": 0.000939222
"part.1.avg": 0.0008974078990000008,
"part.1.max": 0.001647554,
"part.1.min": 0.000185945,
"part.2.avg": 0.004843347521000009,
"part.2.max": 0.007982272,
"part.2.min": 0.000947054
},
"Python": {
"part.1.avg": 0.0014611027240753174,
"part.1.max": 0.0042591094970703125,
"part.1.min": 0.0003712177276611328,
"part.2.avg": 0.006137934684753418,
"part.2.max": 0.014965057373046875,
"part.2.min": 0.0015811920166015625
"part.1.avg": 0.0018942492008209228,
"part.1.max": 0.0035827159881591797,
"part.1.min": 0.0003771781921386719,
"part.2.avg": 0.008033792734146119,
"part.2.max": 0.012754201889038086,
"part.2.min": 0.0016443729400634766
}
},
"numRuns": 1000

View file

@ -0,0 +1 @@
# [Day 8: Treetop Tree House](https://adventofcode.com/2022/day/8)

View file

@ -0,0 +1,15 @@
{
"day": 8,
"dir": "challenges/2022/08-treetopTreeHouse",
"implementations": {
"Python": {
"part.1.avg": 0.033502077102661135,
"part.1.max": 0.06080937385559082,
"part.1.min": 0.023911237716674805,
"part.2.avg": 0.04170952320098877,
"part.2.max": 0.0762169361114502,
"part.2.min": 0.02996683120727539
}
},
"numRuns": 500
}

View file

@ -0,0 +1,17 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "30373\n25512\n65332\n33549\n35390",
"expected": "21"
}
],
"two": [
{
"input": "30373\n25512\n65332\n33549\n35390",
"expected": "8"
}
]
}
}

View file

@ -0,0 +1,89 @@
from typing import *
from aocpy import BaseChallenge
Coordinate = Tuple[int, int]
Forest = Dict[Coordinate, int]
def parse(instr: str) -> Tuple[Forest, Coordinate]:
res: Forest = {}
for x, line in enumerate(instr.strip().splitlines()):
for y, char in enumerate(line):
res[(x, y)] = int(char)
return res, (x, y)
def are_adjacents_lower(forest: Forest, initial_pos: Coordinate, next_pos: Callable[[Coordinate], Coordinate]) -> bool:
height = forest[initial_pos]
pos = next_pos(initial_pos)
while pos in forest:
if forest[pos] >= height:
return False
pos = next_pos(pos)
return True
def get_viewing_distance(forest: Forest, initial_pos: Coordinate, next_pos: Callable[[Coordinate], Coordinate]) -> int:
viewing_dist = 0
height = forest[initial_pos]
pos = next_pos(initial_pos)
while pos in forest:
viewing_dist += 1
if forest[pos] >= height:
break
pos = next_pos(pos)
return viewing_dist
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
forest, (max_x, max_y) = parse(instr)
count = 0
for tree_pos in forest:
x, y = tree_pos
if x == 0 or y == 0 or x == max_x or y == max_y:
count += 1
continue
# left -x
if are_adjacents_lower(forest, tree_pos, lambda x: (x[0]-1, x[1])):
count += 1
continue
# right +x
if are_adjacents_lower(forest, tree_pos, lambda x: (x[0]+1, x[1])):
count += 1
continue
# up -y
if are_adjacents_lower(forest, tree_pos, lambda x: (x[0], x[1]-1)):
count += 1
continue
# down +y
if are_adjacents_lower(forest, tree_pos, lambda x: (x[0], x[1]+1)):
count += 1
continue
return count
@staticmethod
def two(instr: str) -> int:
forest, _ = parse(instr)
max_senic = 0
for tree_pos in forest:
view_left = get_viewing_distance(forest, tree_pos, lambda x: (x[0]-1, x[1]))
view_right = get_viewing_distance(forest, tree_pos, lambda x: (x[0]+1, x[1]))
view_up = get_viewing_distance(forest, tree_pos, lambda x: (x[0], x[1]-1))
view_down = get_viewing_distance(forest, tree_pos, lambda x: (x[0], x[1]+1))
senic = view_left * view_right * view_up * view_down
if senic > max_senic:
max_senic = senic
return max_senic

View file

@ -15,7 +15,9 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| 03 - Rucksack Reorganization | ★ ★ | [Python](03-rucksackReorganization/py), [Nim](03-rucksackReorganization/nim) | Sets and intersections |
| 04 - Camp Cleanup | ★ ★ | [Python](04-campCleanup/py), [Nim](04-campCleanup/nim) | More sets and more intersections! |
| 05 - Supply Stacks | ★ ★ | [Python](05-supplyStacks/py), [Nim](05-supplyStacks/nim) | Believe it or not, this one involved stacks. |
| 05 - Tuning Trouble | ★ ★ | [Python](06-tuningTrouble/py), [Nim](06-tuningTrouble/nim) | This is the first year I've not repeatedly forgotten about the existence of sets, and it's coming in quite handy. |
| 06 - Tuning Trouble | ★ ★ | [Python](06-tuningTrouble/py), [Nim](06-tuningTrouble/nim) | This is the first year I've not repeatedly forgotten about the existence of sets, and it's coming in quite handy. |
| 07 - No Space Left On Device | ☆ ☆ | | Fake file systems are much more difficult to implement than one might expect. |
| 08 - Treetop Tree House | ★ ★ | [Python](08-treetopTreeHouse/py) | Magical coordinate dictionary tuple things do be magical. |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 41 KiB

Before After
Before After