generated from arol-polito/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
8,644 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# -- DAY 9 --\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'size': 94798, 'free': 44967, 'used': 49831}\n", | ||
"The solution of Puzzle 1 is: 6288599492129\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# --- Part One ---\n", | ||
"\n", | ||
"#from itertools import combinations\n", | ||
"from icecream import ic\n", | ||
"\n", | ||
"test = \"2333133121414131402\"\n", | ||
"\n", | ||
"retvalue = 0\n", | ||
"\n", | ||
"class Disk:\n", | ||
" def __init__(self, diskmap = None):\n", | ||
" self.clusters = {}\n", | ||
" if diskmap: self.import_diskmap(diskmap)\n", | ||
" \n", | ||
" def size(self):\n", | ||
" return len(self.clusters)\n", | ||
" \n", | ||
" def info(self):\n", | ||
" info = {}\n", | ||
" info[\"size\"] = self.size()\n", | ||
" info[\"free\"] = len([k for k,v in self.clusters.items() if v == None])\n", | ||
" info[\"used\"] = self.size() - info[\"free\"]\n", | ||
" return info\n", | ||
"\n", | ||
" def import_diskmap(self, diskmap):\n", | ||
" \"\"\"\n", | ||
" Import a disk map\n", | ||
" The disk map uses a dense format to represent the layout of files and free space on the disk.\n", | ||
" The digits alternate between indicating the length of a file and the length of free space.\n", | ||
" \"\"\"\n", | ||
" dm = map(int,list(diskmap))\n", | ||
" cl = 0\n", | ||
" id = 0\n", | ||
" is_file = True\n", | ||
" for c in dm:\n", | ||
" for i in range(c):\n", | ||
" if is_file: self.clusters[cl] = id\n", | ||
" else: self.clusters[cl] = None\n", | ||
" cl += 1\n", | ||
" is_file = not is_file\n", | ||
" id = id + 1 if is_file else id\n", | ||
" \n", | ||
" def defrag(self):\n", | ||
" free = [k for k,v in self.clusters.items() if v == None]\n", | ||
" used = list(reversed([(k, v) for k,v in self.clusters.items() if v != None])) \n", | ||
" while used[0][0] > free[0]: # if used to move is in a cluster following the first free cluster\n", | ||
" from_cluster = used.pop(0)\n", | ||
" to_cluster = free.pop(0)\n", | ||
" self.clusters[to_cluster] = from_cluster[1]\n", | ||
" self.clusters[from_cluster[0]] = None\n", | ||
" return\n", | ||
"\n", | ||
" def checksum(self):\n", | ||
" return sum([k * v for k,v in self.clusters.items() if v != None],0)\n", | ||
"\n", | ||
"\n", | ||
"with open(\"input.txt\",\"r\") as file:\n", | ||
" #file = test.splitlines() # decomment to use test \n", | ||
" diskmap = [l for l in file][0].strip()\n", | ||
"\n", | ||
" disk = Disk(diskmap)\n", | ||
"\n", | ||
" print(disk.info())\n", | ||
" disk.defrag()\n", | ||
"\n", | ||
" #m = \"\".join([str(c) if c!=None else '.' for c in disk.clusters.values()])\n", | ||
" # print (m)\n", | ||
"\n", | ||
" retvalue = disk.checksum()\n", | ||
"\n", | ||
"print (f\"The solution of Puzzle 1 is: {retvalue}\")\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'size': 94798, 'free': 44967, 'used': 49831}\n", | ||
"The solution of Puzzle 2 is: 6321896265143\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# --- Part Two ---\n", | ||
"\n", | ||
"# This solution uses objects defined in part 1\n", | ||
"\n", | ||
"test = \"2333133121414131402\"\n", | ||
"\n", | ||
"retvalue = 0\n", | ||
"\n", | ||
"# --- class patch ---\n", | ||
"# Modify the original Disk class\n", | ||
"# `defrag` method now accepts the \"method\" parameter which can be either \"compact\" or \"files\"\n", | ||
"# If method is \"compact\", the method will compact the disk using original `defrag` method\n", | ||
"# If method is \"files\", the method will compact the disk mantaining the file unfragmented\n", | ||
"_defrag_compact = Disk.defrag\n", | ||
"\n", | ||
"def _defrag_files(self):\n", | ||
" # map files and free clusters in clusters\n", | ||
" files = {} # {id: [start, len]}\n", | ||
" frees = {} # {id: [start, len]}\n", | ||
" last = -1\n", | ||
" id_free = -1\n", | ||
" for k,v in self.clusters.items():\n", | ||
" if v == None:\n", | ||
" if last != None:\n", | ||
" id_free = k\n", | ||
" frees.setdefault(id_free, [id_free, 0])[1] += 1\n", | ||
" else:\n", | ||
" files.setdefault(v, [k, 0])[1] += 1\n", | ||
" last = v\n", | ||
"\n", | ||
" # DEBUG print(\"\".join([str(c) if c!=None else '.' for c in self.clusters.values()]))\n", | ||
"\n", | ||
" for file_id, (file_cluster, file_size) in reversed(list(files.items())):\n", | ||
" for free_id, (free_cluster, free_size) in frees.items():\n", | ||
" if free_size >= file_size and file_cluster > free_cluster:\n", | ||
" self._move_file(file_cluster, free_cluster, file_size)\n", | ||
"\n", | ||
" frees[free_id][0] += file_size\n", | ||
" frees[free_id][1] -= file_size\n", | ||
" # DEBUG print(\"\".join([str(c) if c!=None else '.' for c in self.clusters.values()]))\n", | ||
" break\n", | ||
" \n", | ||
"\n", | ||
"def _move_file(self, from_cluster, to_cluster, size):\n", | ||
" # check if to_cluster is free until size is reached\n", | ||
" for i in range(size):\n", | ||
" if self.clusters[to_cluster + i] != None:\n", | ||
" raise ValueError(f\"Cluster {to_cluster + i} is not free\")\n", | ||
" # move file\n", | ||
" for i in range(size):\n", | ||
" self.clusters[to_cluster + i] = self.clusters[from_cluster + i]\n", | ||
" self.clusters[from_cluster + i] = None\n", | ||
"\n", | ||
"def new_defrag(self, method = \"compact\"):\n", | ||
" if method == \"compact\":\n", | ||
" _defrag_compact(self)\n", | ||
" elif method == \"files\":\n", | ||
" _defrag_files(self)\n", | ||
" else:\n", | ||
" raise ValueError(f\"Invalid method: {method}\")\n", | ||
"\n", | ||
"Disk.defrag = new_defrag\n", | ||
"Disk._move_file = _move_file\n", | ||
"\n", | ||
"# --- end of class patch ---\n", | ||
"\n", | ||
"with open(\"input.txt\",\"r\") as file:\n", | ||
" # file = test.splitlines() # decomment to use test \n", | ||
" diskmap = [l for l in file][0].strip()\n", | ||
"\n", | ||
" disk = Disk(diskmap)\n", | ||
"\n", | ||
" print(disk.info())\n", | ||
" disk.defrag(\"files\")\n", | ||
"\n", | ||
" retvalue = disk.checksum()\n", | ||
"print (f\"The solution of Puzzle 2 is: {retvalue}\")\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# -- DAY 10 --\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The solution of Puzzle 1 is: 574\n", | ||
"The solution of Puzzle 2 is: 1238\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# --- Part One & Two ---\n", | ||
"\n", | ||
"from icecream import ic\n", | ||
"\n", | ||
"test = \"\"\"89010123\n", | ||
"78121874\n", | ||
"87430965\n", | ||
"96549874\n", | ||
"45678903\n", | ||
"32019012\n", | ||
"01329801\n", | ||
"10456732\"\"\"\n", | ||
"\n", | ||
"ret1 = 0\n", | ||
"ret2 = 0\n", | ||
"\n", | ||
"def get_trail(grid, x, y, sv) -> list:\n", | ||
" ret = list()\n", | ||
" w = len(grid[0])\n", | ||
" h = len(grid)\n", | ||
" if x<0 or x>=w: return ret\n", | ||
" if y<0 or y>=h: return ret\n", | ||
" v = grid[y][x]\n", | ||
" if v != sv: return ret\n", | ||
" if v == 9:\n", | ||
" ret.append((x,y))\n", | ||
" return ret\n", | ||
" ret.extend(get_trail(grid, x+1, y, v+1))\n", | ||
" ret.extend(get_trail(grid, x-1, y, v+1))\n", | ||
" ret.extend(get_trail(grid, x, y+1, v+1))\n", | ||
" ret.extend(get_trail(grid, x, y-1, v+1))\n", | ||
" return ret\n", | ||
" \n", | ||
"\n", | ||
"with open(\"input.txt\",\"r\") as file:\n", | ||
" # file = test.splitlines() # decomment to use test\n", | ||
" grid = [list(map(int,list(c))) for c in [l.strip() for l in file]]\n", | ||
" \n", | ||
" for y, row in enumerate(grid):\n", | ||
" for x, v in enumerate(row):\n", | ||
" if v == 0:\n", | ||
" trail = get_trail(grid,x,y,v)\n", | ||
" # ic(trail)\n", | ||
" ret1 += len(set(trail))\n", | ||
" ret2 += len(trail)\n", | ||
"\n", | ||
"print (f\"The solution of Puzzle 1 is: {ret1}\")\n", | ||
"print (f\"The solution of Puzzle 2 is: {ret2}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
676781023010121078756541010565410126589652103 | ||
787692014523134569687238921076321087676543012 | ||
896543210674013278798107831089980896567122107 | ||
654100134985329143237356540123678925498033498 | ||
783210325676438050123445443294541012321044589 | ||
694309018984567267034576356789032008769655678 | ||
345678567823432178125689219878102109678724369 | ||
456969430214563089068701008765210234569013212 | ||
327854321005478873879010123674377654354321001 | ||
218901232876569912968123294589988912210132432 | ||
107650343985567803451054387487676903432101561 | ||
210545674783498712589965236596567876543650170 | ||
323432185692105605678876145645430967858743289 | ||
214981092185434104987986001034321458969801001 | ||
105670123076393213098887632125010321578932102 | ||
789889874101284332106798540136521230432840013 | ||
876776965692379876087034567287650145521051224 | ||
965460150789561045696129898398010676670569345 | ||
234321041276432038765408765498723487989678496 | ||
165432132345987129932317454389654395432310987 | ||
074540122187656087801326761230101276211001236 | ||
783458043090345196540410890121210989303456545 | ||
892169834901210105432589789032367893456327896 | ||
701078985810012234501678776543456302587410187 | ||
667654856798943107657578905494543211693217896 | ||
578983012367874038748765412387687600784506787 | ||
457832343455465129889854307898990521099615690 | ||
300761567854321012934781212387121434988794321 | ||
211650434969482103245690101236012345670188760 | ||
672349123478091014132386789845621012873279454 | ||
589678012562182365001675632736790123964560303 | ||
432547001601276478976543541345887654456781212 | ||
321232118762345569885012310212994569323998800 | ||
210321129098710378894322343200123478010878901 | ||
300410030123601256765011056123430765430765432 | ||
321567542034510349810782987001521894321045645 | ||
434788601945654878725693965432676541013236012 | ||
095699717898783965434344876501987034901107823 | ||
187659826500192854303239884567698127872787934 | ||
234549834410201601214138793298012016543396543 | ||
067812765325360519871025682105623456671230123 | ||
154908901876451456789014871234702965580145674 | ||
233217010945962359874323960189811874495432985 | ||
945606321034876543265221054076320432356781876 | ||
876545432321089012100100123165410321065690165 |
Oops, something went wrong.