parent
f707aa0220
commit
5ed8eb6433
@ -0,0 +1,86 @@ |
|||||||
|
import re |
||||||
|
import argparse |
||||||
|
|
||||||
|
parser = argparse.ArgumentParser() |
||||||
|
parser.add_argument("filename", help="the file from which to take data") |
||||||
|
args = parser.parse_args() |
||||||
|
|
||||||
|
with open(args.filename, "r") as f: contents = f.read() |
||||||
|
|
||||||
|
big_matrix = [] |
||||||
|
|
||||||
|
for line in contents.strip().split("\n"): |
||||||
|
big_matrix.append(list(map(int, line))) |
||||||
|
|
||||||
|
def print_matrix(mat): |
||||||
|
for row in mat: |
||||||
|
print("".join(map(str, row))) |
||||||
|
|
||||||
|
visibility = [] |
||||||
|
for row in big_matrix: |
||||||
|
new_row = [] |
||||||
|
for col in row: |
||||||
|
new_row.append(False) |
||||||
|
visibility.append(new_row) |
||||||
|
|
||||||
|
def update_visibility(trees, visibles): |
||||||
|
top = -1 |
||||||
|
for i, t in enumerate(trees): |
||||||
|
if t>top: |
||||||
|
visibles[i] = True |
||||||
|
top = t |
||||||
|
|
||||||
|
def watch_from_west(mat, vis): |
||||||
|
for trees, vision in zip(mat, vis): |
||||||
|
update_visibility(trees, vision) |
||||||
|
|
||||||
|
watch_from_west(big_matrix, visibility) # look from the west |
||||||
|
big_matrix = list(map(list, map(reversed, big_matrix))) |
||||||
|
visibility = list(map(list, map(reversed, visibility))) |
||||||
|
watch_from_west(big_matrix, visibility) # look from the east |
||||||
|
big_matrix = list(map(list,zip(*big_matrix))) |
||||||
|
visibility = list(map(list,zip(*visibility))) |
||||||
|
watch_from_west(big_matrix, visibility) # look from the... north? |
||||||
|
big_matrix = list(map(list, map(reversed, big_matrix))) |
||||||
|
visibility = list(map(list, map(reversed, visibility))) |
||||||
|
watch_from_west(big_matrix, visibility) # look from the south |
||||||
|
|
||||||
|
f_l = lambda i: filter(lambda x: x, i) |
||||||
|
|
||||||
|
answer = sum(map(len, map(list, map(f_l, visibility)))) |
||||||
|
|
||||||
|
print_matrix(visibility) |
||||||
|
print("visible trees are {} in number".format(answer)) |
||||||
|
|
||||||
|
|
||||||
|
def look(forest, x, y): |
||||||
|
n = look_at(forest, x, y, 0, -1) |
||||||
|
s = look_at(forest, x, y, 0, 1) |
||||||
|
e = look_at(forest, x, y, 1, 0) |
||||||
|
w = look_at(forest, x, y, -1, 0) |
||||||
|
|
||||||
|
return n*s*e*w |
||||||
|
|
||||||
|
def look_at(forest, x, y, dx, dy): |
||||||
|
count = 0 |
||||||
|
treehouse = forest[y][x] |
||||||
|
tx = x + dx |
||||||
|
ty = y + dy |
||||||
|
while tx >= 0 and tx < len(forest[0]) and ty >= 0 and ty < len(forest): |
||||||
|
if forest[ty][tx] >= treehouse: |
||||||
|
count += 1 |
||||||
|
break |
||||||
|
count += 1 |
||||||
|
tx = tx + dx |
||||||
|
ty = ty + dy |
||||||
|
return count |
||||||
|
|
||||||
|
best_score = 0 |
||||||
|
for y, row in enumerate(big_matrix): |
||||||
|
for x, tree in enumerate(row): |
||||||
|
score = look(big_matrix, x, y) |
||||||
|
if score > best_score: |
||||||
|
best_score = score |
||||||
|
|
||||||
|
print("the highest score we found was {}".format(best_score)) |
||||||
|
|
@ -0,0 +1,53 @@ |
|||||||
|
import re |
||||||
|
import argparse |
||||||
|
|
||||||
|
parser = argparse.ArgumentParser() |
||||||
|
parser.add_argument("filename", help="the file from which to take data") |
||||||
|
args = parser.parse_args() |
||||||
|
|
||||||
|
with open(args.filename, "r") as f: commands = f.readlines() |
||||||
|
|
||||||
|
visited = {(0,0)} |
||||||
|
head = (0, 0) |
||||||
|
tails = (0, 0) |
||||||
|
|
||||||
|
def walk(command, head, tail, visited): |
||||||
|
direction, repeats = command.split(" ") |
||||||
|
repeats = int(repeats) |
||||||
|
first = True |
||||||
|
while repeats > 0: |
||||||
|
head = step(direction, head, tail) |
||||||
|
tail = follow(head, tail) |
||||||
|
first = False |
||||||
|
visited = visited | {tail} |
||||||
|
|
||||||
|
repeats -= 1 |
||||||
|
|
||||||
|
print("walking {} while head is {} and tail is {}".format(command, head, tail)) |
||||||
|
return head, tail, visited |
||||||
|
|
||||||
|
def follow(head, tail): |
||||||
|
if head[0] < tail[0]-1 or head[0] > tail[0]+1 or \ |
||||||
|
head[1] < tail[1]-1 or head[1] > tail[1]+1: |
||||||
|
if head[0] < tail[0]: tail = (tail[0]-1, tail[1]) |
||||||
|
if head[0] > tail[0]: tail = (tail[0]+1, tail[1]) |
||||||
|
if head[1] < tail[1]: tail = (tail[0], tail[1]-1) |
||||||
|
if head[1] > tail[1]: tail = (tail[0], tail[1]+1) |
||||||
|
return tail |
||||||
|
|
||||||
|
def step(direction, head, tail): |
||||||
|
match direction: |
||||||
|
case 'R': return (head[0]+1, head[1]) |
||||||
|
case 'L': return (head[0]-1, head[1]) |
||||||
|
case 'U': return (head[0], head[1]+1) |
||||||
|
case 'D': return (head[0], head[1]-1) |
||||||
|
case _: return head |
||||||
|
|
||||||
|
print("{}".format(visited)) |
||||||
|
|
||||||
|
for command in commands: |
||||||
|
head, tail, visited = walk(command, head, tail, visited) |
||||||
|
|
||||||
|
print("{}".format(visited)) |
||||||
|
|
||||||
|
print("visited {} locations".format(len(visited))) |
@ -0,0 +1,53 @@ |
|||||||
|
import re |
||||||
|
import argparse |
||||||
|
|
||||||
|
parser = argparse.ArgumentParser() |
||||||
|
parser.add_argument("filename", help="the file from which to take data") |
||||||
|
args = parser.parse_args() |
||||||
|
|
||||||
|
with open(args.filename, "r") as f: commands = f.readlines() |
||||||
|
|
||||||
|
visited = {(0,0)} |
||||||
|
head = (0, 0) |
||||||
|
tails = [(0, 0)]*9 |
||||||
|
|
||||||
|
def walk(command, head, tails, visited): |
||||||
|
direction, repeats = command.split(" ") |
||||||
|
repeats = int(repeats) |
||||||
|
first = True |
||||||
|
while repeats > 0: |
||||||
|
head = step(direction, head) |
||||||
|
for i in range(len(tails)): |
||||||
|
if i==0: |
||||||
|
h = head |
||||||
|
else: |
||||||
|
h = tails[i-1] |
||||||
|
tails[i] = follow(h, tails[i]) |
||||||
|
first = False |
||||||
|
visited = visited | {tails[-1]} |
||||||
|
|
||||||
|
repeats -= 1 |
||||||
|
|
||||||
|
return head, tails, visited |
||||||
|
|
||||||
|
def follow(head, tail): |
||||||
|
if head[0] < tail[0]-1 or head[0] > tail[0]+1 or \ |
||||||
|
head[1] < tail[1]-1 or head[1] > tail[1]+1: |
||||||
|
if head[0] < tail[0]: tail = (tail[0]-1, tail[1]) |
||||||
|
if head[0] > tail[0]: tail = (tail[0]+1, tail[1]) |
||||||
|
if head[1] < tail[1]: tail = (tail[0], tail[1]-1) |
||||||
|
if head[1] > tail[1]: tail = (tail[0], tail[1]+1) |
||||||
|
return tail |
||||||
|
|
||||||
|
def step(direction, head): |
||||||
|
match direction: |
||||||
|
case 'R': return (head[0]+1, head[1]) |
||||||
|
case 'L': return (head[0]-1, head[1]) |
||||||
|
case 'U': return (head[0], head[1]+1) |
||||||
|
case 'D': return (head[0], head[1]-1) |
||||||
|
case _: return head |
||||||
|
|
||||||
|
for command in commands: |
||||||
|
head, tails, visited = walk(command, head, tails, visited) |
||||||
|
|
||||||
|
print("visited {} locations".format(len(visited))) |
@ -0,0 +1,29 @@ |
|||||||
|
### IntelliJ IDEA ### |
||||||
|
out/ |
||||||
|
!**/src/main/**/out/ |
||||||
|
!**/src/test/**/out/ |
||||||
|
|
||||||
|
### Eclipse ### |
||||||
|
.apt_generated |
||||||
|
.classpath |
||||||
|
.factorypath |
||||||
|
.project |
||||||
|
.settings |
||||||
|
.springBeans |
||||||
|
.sts4-cache |
||||||
|
bin/ |
||||||
|
!**/src/main/**/bin/ |
||||||
|
!**/src/test/**/bin/ |
||||||
|
|
||||||
|
### NetBeans ### |
||||||
|
/nbproject/private/ |
||||||
|
/nbbuild/ |
||||||
|
/dist/ |
||||||
|
/nbdist/ |
||||||
|
/.nb-gradle/ |
||||||
|
|
||||||
|
### VS Code ### |
||||||
|
.vscode/ |
||||||
|
|
||||||
|
### Mac OS ### |
||||||
|
.DS_Store |
@ -0,0 +1,8 @@ |
|||||||
|
# Default ignored files |
||||||
|
/shelf/ |
||||||
|
/workspace.xml |
||||||
|
# Editor-based HTTP Client requests |
||||||
|
/httpRequests/ |
||||||
|
# Datasource local storage ignored files |
||||||
|
/dataSources/ |
||||||
|
/dataSources.local.xml |
@ -0,0 +1,10 @@ |
|||||||
|
<component name="ProjectCodeStyleConfiguration"> |
||||||
|
<code_scheme name="Project" version="173"> |
||||||
|
<JetCodeStyleSettings> |
||||||
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> |
||||||
|
</JetCodeStyleSettings> |
||||||
|
<codeStyleSettings language="kotlin"> |
||||||
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> |
||||||
|
</codeStyleSettings> |
||||||
|
</code_scheme> |
||||||
|
</component> |
@ -0,0 +1,5 @@ |
|||||||
|
<component name="ProjectCodeStyleConfiguration"> |
||||||
|
<state> |
||||||
|
<option name="USE_PER_PROJECT_SETTINGS" value="true" /> |
||||||
|
</state> |
||||||
|
</component> |
@ -0,0 +1,6 @@ |
|||||||
|
<component name="InspectionProjectProfileManager"> |
||||||
|
<profile version="1.0"> |
||||||
|
<option name="myName" value="Project Default" /> |
||||||
|
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" /> |
||||||
|
</profile> |
||||||
|
</component> |
@ -0,0 +1,10 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project version="4"> |
||||||
|
<component name="Kotlin2JvmCompilerArguments"> |
||||||
|
<option name="jvmTarget" value="1.8" /> |
||||||
|
</component> |
||||||
|
<component name="KotlinCommonCompilerArguments"> |
||||||
|
<option name="apiVersion" value="1.9" /> |
||||||
|
<option name="languageVersion" value="1.9" /> |
||||||
|
</component> |
||||||
|
</project> |
@ -0,0 +1,26 @@ |
|||||||
|
<component name="libraryTable"> |
||||||
|
<library name="KotlinJavaRuntime" type="repository"> |
||||||
|
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.0" /> |
||||||
|
<CLASSES> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0.jar!/" /> |
||||||
|
</CLASSES> |
||||||
|
<JAVADOC> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-javadoc.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-javadoc.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-javadoc.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-javadoc.jar!/" /> |
||||||
|
</JAVADOC> |
||||||
|
<SOURCES> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-sources.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-sources.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-sources.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" /> |
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-sources.jar!/" /> |
||||||
|
</SOURCES> |
||||||
|
</library> |
||||||
|
</component> |
@ -0,0 +1,6 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project version="4"> |
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK"> |
||||||
|
<output url="file://$PROJECT_DIR$/out" /> |
||||||
|
</component> |
||||||
|
</project> |
@ -0,0 +1,8 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project version="4"> |
||||||
|
<component name="ProjectModuleManager"> |
||||||
|
<modules> |
||||||
|
<module fileurl="file://$PROJECT_DIR$/day10.iml" filepath="$PROJECT_DIR$/day10.iml" /> |
||||||
|
</modules> |
||||||
|
</component> |
||||||
|
</project> |
@ -0,0 +1,6 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project version="4"> |
||||||
|
<component name="VcsDirectoryMappings"> |
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> |
||||||
|
</component> |
||||||
|
</project> |
@ -0,0 +1,15 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<module type="JAVA_MODULE" version="4"> |
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true"> |
||||||
|
<exclude-output /> |
||||||
|
<content url="file://$MODULE_DIR$"> |
||||||
|
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" /> |
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> |
||||||
|
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> |
||||||
|
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" /> |
||||||
|
</content> |
||||||
|
<orderEntry type="inheritedJdk" /> |
||||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||||
|
<orderEntry type="library" name="KotlinJavaRuntime" level="project" /> |
||||||
|
</component> |
||||||
|
</module> |
@ -0,0 +1,3 @@ |
|||||||
|
fun main() { |
||||||
|
println("Hello World!") |
||||||
|
} |
Loading…
Reference in new issue