Skip to content

Commit 2cb1c82

Browse files
2022: Add day 18.
1 parent ce2b25c commit 2cb1c82

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

2022/advent_of_code_2022.ipynb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,86 @@
14811481
"source": [
14821482
"#TODO"
14831483
]
1484+
},
1485+
{
1486+
"attachments": {},
1487+
"cell_type": "markdown",
1488+
"metadata": {},
1489+
"source": [
1490+
"## Day 18: Boiling Boulders\n",
1491+
"\n",
1492+
"What is the surface area of your scanned lava droplet?"
1493+
]
1494+
},
1495+
{
1496+
"cell_type": "code",
1497+
"execution_count": null,
1498+
"metadata": {},
1499+
"outputs": [],
1500+
"source": [
1501+
"def are_connected(cube0, cube1):\n",
1502+
" return abs(cube0[0] - cube1[0]) == 1 and cube0[1] == cube1[1] and cube0[2] == cube1[2] \\\n",
1503+
" or cube0[0] == cube1[0] and abs(cube0[1] - cube1[1]) == 1 and cube0[2] == cube1[2] \\\n",
1504+
" or cube0[0] == cube1[0] and cube0[1] == cube1[1] and abs(cube0[2] - cube1[2]) == 1\n",
1505+
"\n",
1506+
"def get_num_connected_sides(cube, cubes):\n",
1507+
" return sum(1 for c in cubes if are_connected(cube, c))\n",
1508+
"\n",
1509+
"def get_surface_area_of_cube(cube, cubes):\n",
1510+
" return 6 - get_num_connected_sides(cube, cubes)\n",
1511+
"\n",
1512+
"lines = open('18_input.txt', 'r').readlines()\n",
1513+
"lines = [line.strip() for line in lines]\n",
1514+
"\n",
1515+
"cubes = [tuple([int(coord) for coord in line.split(',')]) for line in lines]\n",
1516+
"sum(get_surface_area_of_cube(c, cubes) for c in cubes)"
1517+
]
1518+
},
1519+
{
1520+
"attachments": {},
1521+
"cell_type": "markdown",
1522+
"metadata": {},
1523+
"source": [
1524+
"What is the exterior surface area of your scanned lava droplet?"
1525+
]
1526+
},
1527+
{
1528+
"cell_type": "code",
1529+
"execution_count": null,
1530+
"metadata": {},
1531+
"outputs": [],
1532+
"source": [
1533+
"def change_tuple_at_index(t, index, modifier):\n",
1534+
" l = list(t)\n",
1535+
" l[index] += modifier\n",
1536+
" return tuple(l)\n",
1537+
"\n",
1538+
"def process_candidate(cube, index, modifier, cubes, visited_air, air_to_visit, area):\n",
1539+
" candidate = change_tuple_at_index(cube, index, modifier)\n",
1540+
" if candidate in cubes:\n",
1541+
" area += 1\n",
1542+
" elif candidate not in visited_air:\n",
1543+
" air_to_visit.add(candidate)\n",
1544+
" return air_to_visit, area\n",
1545+
"\n",
1546+
"def process_cube(cube, cubes, max_coords, visited_air, air_to_visit, area):\n",
1547+
" for i in range(len(cube)):\n",
1548+
" if cube[i] >= 0:\n",
1549+
" air_to_visit, area = process_candidate(cube, i, -1, cubes, visited_air, air_to_visit, area)\n",
1550+
" if cube[i] < max_coords[i]:\n",
1551+
" air_to_visit, area = process_candidate(cube, i, 1, cubes, visited_air, air_to_visit, area)\n",
1552+
" return air_to_visit, area\n",
1553+
"\n",
1554+
"max_coords = tuple([max(i)+1 for i in zip(*cubes)])\n",
1555+
"air_to_visit, visited_air = set([max_coords]), set()\n",
1556+
"\n",
1557+
"area = 0\n",
1558+
"while air_to_visit:\n",
1559+
" cube = air_to_visit.pop()\n",
1560+
" visited_air.add(cube)\n",
1561+
" air_to_visit, area = process_cube(cube, cubes, max_coords, visited_air, air_to_visit, area)\n",
1562+
"area"
1563+
]
14841564
}
14851565
],
14861566
"metadata": {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy