Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
#!/usr/bin/env python3.8 """ 30373 25512 65332 33549 35390 Each tree is represented as a single digit whose value is its height, where 0 is the shortest and 9 is the tallest. A tree is visible if all of the other trees between it and an edge of the grid are shorter than it. Only consider trees in the same row or column; that is, only look up, down, left, or right from any given tree. All of the trees around the edge of the grid are visible - since they are already on the edge, there are no trees to block the view. In this example, that only leaves the interior nine trees to consider: The top-left 5 is visible from the left and top. (It isn't visible from the right or bottom since other trees of height 5 are in the way.) The top-middle 5 is visible from the top and right. The top-right 1 is not visible from any direction; for it to be visible, there would need to only be trees of height 0 between it and an edge. The left-middle 5 is visible, but only from the right. The center 3 is not visible from any direction; for it to be visible, there would need to be only trees of at most height 2 between it and an edge. The right-middle 3 is visible from the right. In the bottom row, the middle 5 is visible, but the 3 and 4 are not. With 16 trees visible on the edge and another 5 visible in the interior, a total of 21 trees are visible in this arrangement. """ import sys import time from functools import wraps from pathlib import Path import re import pytest INPUTS_DIR = 'inputs' INPUT_FILE = 'day08.txt' PROJECT_DIR = Path('.') FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE LOOP_NO = 1 def f_time(f): @wraps(f) def time_check(*args, **kwargs): print(f'{LOOP_NO=}') start_time = time.monotonic() for i in range(LOOP_NO): sizes = f(*args, *kwargs) end_time = time.monotonic() print(f"It took {end_time-start_time:.10f} seconds to compute") return sizes return time_check def read_data(file_path): """Read file""" with open(file_path) as f: return f.read().strip().splitlines() def visible_trees_number(file_path): trees = read_data(file_path) rows, columns = prepare_rows_columns(trees) visible_trees, score = tree_visibility(trees, rows, columns) return visible_trees, score def tree_visibility(trees, rows, columns): v = len(trees) h = len(trees[0]) edge_trees = 2*(h-1) + 2*(v-1) visible_trees = 0 score = 0 for i in range(1, v - 1): for j in range(1, h - 1): tree = int(trees[i][j]) if is_tree_visibile(tree, i, j, rows, columns): visible_trees += 1 current_score = tree_score(tree, i, j, rows, columns) if current_score > score: score = current_score return visible_trees + edge_trees, score def prepare_rows_columns(trees): h = len(trees[0]) rows = [] for row in trees: rows.append(list(map(int, re.findall(r'\d', row)))) columns = [] for i in range(h): column = [] for row in rows: column.append(row[i]) columns.append(column) return (rows, columns) def is_tree_visibile(tree, pos_v, pos_h, rows, columns): good_trees = set(list(range(tree, 10))) l_trees = set(rows[pos_v][:pos_h]) r_trees = set(rows[pos_v][pos_h+1:]) u_trees = set(columns[pos_h][:pos_v]) d_trees = set(columns[pos_h][pos_v+1:]) return not ( good_trees.intersection(l_trees) and good_trees.intersection(r_trees) and good_trees.intersection(u_trees) and good_trees.intersection(d_trees) ) def tree_score(tree, pos_v, pos_h, rows, columns): score = 1 l_trees = rows[pos_v][:pos_h][::-1] r_trees = rows[pos_v][pos_h+1:] u_trees = columns[pos_h][:pos_v][::-1] d_trees = columns[pos_h][pos_v+1:] for trees in [l_trees, r_trees, u_trees, d_trees]: for i, _tree in enumerate(trees, 1): if _tree >= tree: score *= i break else: score *= len(trees) return score ex_output_1 = """ 30373 25512 65332 33549 35390 """ ex_result_1 = 21 tree_house_max_score = 8 ex_output_2 = """ 16465939517022874380 98702061596432835417 91507485930236271864 83291124607345950867 56433069148802719275 48402771015326935896 82450956312719603847 26357084751340681992 98705146257291438603 81293278096057431645 84279159316208704653 72974835102466059381 46154257981968327030 68852502743970119463 40653017817322459698 95231881257446607390 82908169013764457352 50108329926748345167 95302579846610241873 80607296935112485743 """ ex_result_2 = 174 @pytest.mark.parametrize('trees_matrix, visible_trees, tree_house_max_score', [ (ex_output_1, ex_result_1, tree_house_max_score), (ex_output_2, ex_result_2, 0) ] ) def test_dir_structure(trees_matrix, visible_trees, tree_house_max_score): trees = trees_matrix.strip().splitlines() rows, columns = prepare_rows_columns(trees) current_visible_trees, score = tree_visibility(trees, rows, columns) assert (visible_trees == current_visible_trees), ( f'Incorrect struct\nExpected: {visible_trees}\nActual: {current_visible_trees}' ) if tree_house_max_score: assert score == tree_house_max_score if __name__ == '__main__': file_path = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH print(visible_trees_number(file_path)) pytest.main(sys.argv) import os if (loop_no := os.getenv('LOOP_NO')) is None: exit() LOOP_NO = int(loop_no)
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
December smells like money
CSS | 3 min ago | 0.28 KB
✅ Changelly.com Exploit
16 min ago | 0.28 KB
Trading method
CSS | 29 min ago | 0.28 KB
Google Docs - Exploit
42 min ago | 0.28 KB
Earn money on Changelly
CSS | 56 min ago | 0.28 KB
Untitled
C | 9 hours ago | 0.88 KB
acg
1 day ago | 0.05 KB
AI METHODS
2 days ago | 0.29 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!