From 5ed8eb6433c59c78d5c8c82b8547c196e9c79fb0 Mon Sep 17 00:00:00 2001 From: shoofle Date: Thu, 14 Dec 2023 22:02:17 -0500 Subject: [PATCH] days 8 and 9 --- day08/day08.py | 86 +++++++++++++++++++ day09/day09.py | 53 ++++++++++++ day09/day09b.py | 53 ++++++++++++ day10/.gitignore | 29 +++++++ day10/.idea/.gitignore | 8 ++ day10/.idea/codeStyles/Project.xml | 10 +++ day10/.idea/codeStyles/codeStyleConfig.xml | 5 ++ .../inspectionProfiles/Project_Default.xml | 6 ++ day10/.idea/kotlinc.xml | 10 +++ day10/.idea/libraries/KotlinJavaRuntime.xml | 26 ++++++ day10/.idea/misc.xml | 6 ++ day10/.idea/modules.xml | 8 ++ day10/.idea/vcs.xml | 6 ++ day10/day10.iml | 15 ++++ day10/src/Main.kt | 3 + 15 files changed, 324 insertions(+) create mode 100644 day08/day08.py create mode 100644 day09/day09.py create mode 100644 day09/day09b.py create mode 100644 day10/.gitignore create mode 100644 day10/.idea/.gitignore create mode 100644 day10/.idea/codeStyles/Project.xml create mode 100644 day10/.idea/codeStyles/codeStyleConfig.xml create mode 100644 day10/.idea/inspectionProfiles/Project_Default.xml create mode 100644 day10/.idea/kotlinc.xml create mode 100644 day10/.idea/libraries/KotlinJavaRuntime.xml create mode 100644 day10/.idea/misc.xml create mode 100644 day10/.idea/modules.xml create mode 100644 day10/.idea/vcs.xml create mode 100644 day10/day10.iml create mode 100644 day10/src/Main.kt diff --git a/day08/day08.py b/day08/day08.py new file mode 100644 index 0000000..9780739 --- /dev/null +++ b/day08/day08.py @@ -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)) + diff --git a/day09/day09.py b/day09/day09.py new file mode 100644 index 0000000..92df3d1 --- /dev/null +++ b/day09/day09.py @@ -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))) \ No newline at end of file diff --git a/day09/day09b.py b/day09/day09b.py new file mode 100644 index 0000000..498ffb1 --- /dev/null +++ b/day09/day09b.py @@ -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))) \ No newline at end of file diff --git a/day10/.gitignore b/day10/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/day10/.gitignore @@ -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 \ No newline at end of file diff --git a/day10/.idea/.gitignore b/day10/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/day10/.idea/.gitignore @@ -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 diff --git a/day10/.idea/codeStyles/Project.xml b/day10/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/day10/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/day10/.idea/codeStyles/codeStyleConfig.xml b/day10/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/day10/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/day10/.idea/inspectionProfiles/Project_Default.xml b/day10/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/day10/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/day10/.idea/kotlinc.xml b/day10/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/day10/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/day10/.idea/libraries/KotlinJavaRuntime.xml b/day10/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/day10/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day10/.idea/misc.xml b/day10/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/day10/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day10/.idea/modules.xml b/day10/.idea/modules.xml new file mode 100644 index 0000000..ff3e9b5 --- /dev/null +++ b/day10/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/day10/.idea/vcs.xml b/day10/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/day10/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day10/day10.iml b/day10/day10.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/day10/day10.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day10/src/Main.kt b/day10/src/Main.kt new file mode 100644 index 0000000..a7a5e00 --- /dev/null +++ b/day10/src/Main.kt @@ -0,0 +1,3 @@ +fun main() { + println("Hello World!") +} \ No newline at end of file