Merge pull request #325 from microsoft/advent-of-code-2022

Advent of code garden ep 2 problems 6-8
This commit is contained in:
Renee Noble 2022-12-09 15:15:04 +11:00 коммит произвёл GitHub
Родитель b6e5ec4db1 7d8ae71d40
Коммит 62ace08652
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 248 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1 @@
bvwbjplbgvbhsrlpgdmjqwftvncz

Просмотреть файл

@ -0,0 +1,16 @@
data = open("day6-data.txt").read().strip()
# bvwbjplbgvbhsrlpgdmjqwftvncz
for i in range(len(data)-4):
chunk = data[i:i+4]
if len(set(chunk)) == 4:
print(chunk)
print("First start of packet marker found", i+4)
break
for i in range(len(data)-14):
chunk = data[i:i+14]
if len(set(chunk)) == 14:
print(chunk)
print("First start of message marker found", i+14)
break

Просмотреть файл

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

Просмотреть файл

@ -0,0 +1,89 @@
data = [x.strip() for x in open("day7-example.txt").readlines()]
class Folder:
def __init__(self, name, parent=None):
self.name = name
self.subfolders = {}
self.files = {}
self.parent = parent
def addSubFolder(self, subName):
newSub = Folder(subName, parent=self)
self.subfolders[subName] = newSub
return newSub
def addFile(self, fName, fSize):
self.files[fName] = fSize
def folderSize(self):
fileSizes = sum(self.files.values())
folderSizes = sum([f.folderSize() for f in self.subfolders.values()])
# total_folders_size = 0
# for f in self.subfolders.values():
# total_folder_size += f.folderSize()
return fileSizes + folderSizes
def isSmall(self):
return self.folderSize() <= 100000
# class Directory():
# def __init__(self, name, parent):
# self.name = name
# self.contents = []
# self.parent = parent
# class Leaf_file():
# def __init__(self, name, size):
# self.name = name
# self.size = int(size)
root = Folder("/")
workingDir = root
allFolders = [root]
for line in data[1:]:
if line.startswith("$"):
# is a command
if line.startswith("$ cd"):
destination = line.split(" ")[2]
if destination == "..":
workingDir = workingDir.parent
else:
workingDir = workingDir.subfolders[destination]
elif line.startswith("$ ls"):
pass # do nothing here
else:
# is a file or directory (directories are files shhh)
if line.startswith("dir"):
newFolder = workingDir.addSubFolder(line.split(" ")[1])
allFolders.append(newFolder)
else:
fSize, fName = line.split(" ")
workingDir.addFile(fName, int(fSize))
print("All small folders:", sum([x.folderSize() for x in allFolders if x.isSmall()]))
# total = 0
# for x in allFolders:
# if x.isSmall():
# total += x.folderSize()
# print("All small folders:", total)
TOTAL_SIZE = 70000000
NEEDED_FREE = 30000000
freeSpace = TOTAL_SIZE - root.folderSize()
neededClearance = NEEDED_FREE - freeSpace
foldersBySize = sorted(allFolders, key=lambda x : x.folderSize())
for f in foldersBySize:
if f.folderSize() >= neededClearance:
print("Folder to delete and size:", f.name, f.folderSize())
break
# def example_lambda(x):
# return x.folderSize()

Просмотреть файл

@ -0,0 +1,119 @@
#!/usr/bin/env python3
data = [x.strip() for x in open("day8-data.txt").readlines()]
maxY = len(data)
maxX = len(data[0])
trees = {}
for y in range(maxY):
for x in range(maxX):
trees[(x,y)] = int(data[y][x])
def isVisible(treeX, treeY):
treeHeight = trees[(treeX, treeY)]
if treeX == 0 or treeY == 0 or treeX == maxX-1 or treeY == maxY-1:
# is boundary tree
return True
vis = True
for x in range(0, treeX):
# can be seen from the left
if trees[(x, treeY)] >= treeHeight:
vis = False
break
if vis:
return True
vis = True
for x in range(treeX+1, maxX):
# can be seen from the right
if trees[(x, treeY)] >= treeHeight:
vis = False
break
if vis:
return True
vis = True
for y in range(0, treeY):
# can be seen from the top
if trees[(treeX, y)] >= treeHeight:
vis = False
break
if vis:
return True
vis = True
for y in range(treeY+1, maxY):
# can be seen from the bottom
if trees[(treeX, y)] >= treeHeight:
vis = False
break
if vis:
return True
return False
visibleTrees = 0
for x, y in trees.keys():
visibleTrees += isVisible(x, y)
print(visibleTrees)
def upVis(x, treeY, h):
score = 0
for y in range(treeY-1, -1, -1):
if trees[(x, y)] < h:
score += 1
else:
score += 1
return score
return score
def downVis(x, treeY, h):
score = 0
for y in range(treeY+1, maxY):
if trees[(x, y)] < h:
score += 1
else:
score += 1
return score
return score
def leftVis(treeX, y, h):
score = 0
for x in range(treeX-1, -1, -1):
if trees[(x, y)] < h:
score += 1
else:
score += 1
return score
return score
def rightVis(treeX, y, h):
score = 0
for x in range(treeX+1, maxX):
if trees[(x, y)] < h:
score += 1
else:
score += 1
return score
return score
def visScore(x, y):
h = trees[(x, y)]
return upVis(x, y, h) * downVis(x, y, h) * leftVis(x, y, h) * rightVis(x, y, h)
bestScore = 0
bestPos = None
for x, y in trees.keys():
if x == 2 and y == 3:
pass
score = visScore(x, y)
if score > bestScore:
bestPos = (x, y)
bestScore = score
print(bestPos, bestScore)