parent
489bbbd1ca
commit
a44f0957f7
@ -0,0 +1,78 @@ |
||||
# Day 9: Smoke Basin |
||||
|
||||
These caves seem to be [lava tubes](https://en.wikipedia.org/wiki/Lava_tube). Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain. |
||||
|
||||
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input). |
||||
|
||||
Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap: |
||||
|
||||
``` |
||||
2199943210 |
||||
3987894921 |
||||
9856789892 |
||||
8767896789 |
||||
9899965678 |
||||
``` |
||||
|
||||
Each number corresponds to the height of a particular location, where `9` is the highest and `0` is the lowest a location can be. |
||||
|
||||
Your first goal is to find the **low points** - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.) |
||||
|
||||
In the above example, there are **four** low points, all highlighted: two are in the first row (a `1` and a `0`), one is in the third row (a `5`), and one is in the bottom row (also a `5`). All other locations on the heightmap have some lower adjacent location, and so are not low points. |
||||
|
||||
The **risk level** of a low point is **1 plus its height**. In the above example, the risk levels of the low points are `2`, `1`, `6`, and `6`. The sum of the risk levels of all low points in the heightmap is therefore `15`. |
||||
|
||||
Find all of the low points on your heightmap. **What is the sum of the risk levels of all low points on your heightmap?** |
||||
|
||||
# Part Two |
||||
|
||||
Next, you need to find the largest basins so you know what areas are most important to avoid. |
||||
|
||||
A **basin** is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height `9` do not count as being in any basin, and all other locations will always be part of exactly one basin. |
||||
|
||||
The **size** of a basin is the number of locations within the basin, including the low point. The example above has four basins. |
||||
|
||||
The top-left basin, size `3`: |
||||
|
||||
``` |
||||
2199943210 |
||||
3987894921 |
||||
9856789892 |
||||
8767896789 |
||||
9899965678 |
||||
``` |
||||
|
||||
The top-right basin, size `9`: |
||||
|
||||
``` |
||||
2199943210 |
||||
3987894921 |
||||
9856789892 |
||||
8767896789 |
||||
9899965678 |
||||
``` |
||||
|
||||
The middle basin, size `14`: |
||||
|
||||
``` |
||||
2199943210 |
||||
3987894921 |
||||
9856789892 |
||||
8767896789 |
||||
9899965678 |
||||
``` |
||||
|
||||
The bottom-right basin, size `9`: |
||||
|
||||
``` |
||||
2199943210 |
||||
3987894921 |
||||
9856789892 |
||||
8767896789 |
||||
9899965678 |
||||
``` |
||||
|
||||
Find the three largest basins and multiply their sizes together. In the above example, this is `9 * 14 * 9 = 1134`. |
||||
|
||||
**What do you get if you multiply together the sizes of the three largest basins?** |
||||
|
@ -0,0 +1,100 @@ |
||||
9976557856799875679875642456989998767978931098989876587878999876565667896543210123567899876794310234 |
||||
8765432145789983989864321345978999954569532987679965426567899865423457899654329439698987665679442545 |
||||
9898553234567899898765432499767899895689549876567894312458998543212679998785698998999996543458953456 |
||||
8987668765679998769986753987656798799899698765456789101345697654323567899896797987899989542557894597 |
||||
7698778978895129954397864797543445678978939988367895212467899875534568999999986576789877410136789989 |
||||
7539989989943239865298985698432134799654329876238954324588921976646899889998765435698765321245695678 |
||||
4321398991294345976129876986553346999743219765359995435789890989887898767899754324569876434557894567 |
||||
9932357890989496987235999898764457898752102976998789756899789999999976556689865546894987678967942456 |
||||
7893459939878989998499989789898768987643244599896578997947567898899875434599986657975998989878921347 |
||||
6899978919965578999987875678999978998754395699789468998933458987798654323678998768976899293989842568 |
||||
5767899998764455899976534779899989329895989987685357999212347976539875435678929899987920191099754989 |
||||
4356899987643234789987413566789998912989879987531238994301656987645989596789434921098939989198769899 |
||||
3245689998432124568998302345678987893979769898750126789212797899787898989899865699999998878999898789 |
||||
4346897998543345689863212558789796999868757789942334599433458998999987778999976987899987657899987645 |
||||
6567976697654576797654323567896545987654645679643545678945579987899896569998989876999874545678998434 |
||||
7988965498865687899875434578987856998763238998759856789757899876798765378987898765689963234567894323 |
||||
8999654319976798967989565689498967899542127789898769898968989965669876567896439876799854355989995434 |
||||
9498765423498899656798779894329878998743235678999889967899877654554987678989321997898769866798789645 |
||||
4349876434599999348939889986445989999987345789998994457898766542443498799678934598969899877895678956 |
||||
3256987595989888999423994399578997879996568998987653216789654321012349896579949789345999989924569969 |
||||
4345698989865676789212349298999875457899689767499864434578975993153569965456898993234998999212479898 |
||||
9559899876543134694325498987899764345698797654398765675678999789234698543234567894399877787993998787 |
||||
7998942965421012349434997876798621234899999865239977788789987698946987652126989975989566546889876546 |
||||
6897891977632163768949876565689540135679998752145698899898776587799998763234991099877452335679985434 |
||||
5956789876543245689769954434578932348799989543234999954987654465688999954756789198765321025899974323 |
||||
3347979997659456789898743125567896556999878956399876743498943334567899975678998989876432134679765434 |
||||
1234567998767768999987659012389987669898767898989954342349832123479999876989546976987563245678976755 |
||||
3569678969889879898998798923568998798789659999969895201298753334567891987895439765987675656789988767 |
||||
4678989657996998787899987894567999979678945899756789212459896567978953998998598654399899777893299878 |
||||
6989298743235987656799986789789398768599434698645698924599987798989769869897698765478967989965459989 |
||||
7892019651019876545899875678990239843489321986534567895989998939599898756789999896569546599996598796 |
||||
8989198772124985434599989889789349652578999898423498999878989012399999646796899997678933498989697654 |
||||
9678949989234997523678992997678998543456789789212349898956578993989898731234679998989321987679798793 |
||||
3568931098949886212789751034567987654567898698901298797543499989876789820124567899999910199568999989 |
||||
8689543987898775102457942123457998865689965457992999697621989875465698931435789967989891398459999876 |
||||
8797699876987654323568943234567989976799954354989876543210178954324987892387991349877689987378989765 |
||||
9898987654599875464899754345789876988898890123976998679633267893219876789998910598765578996569876543 |
||||
3979998943456987596789965956899965499997689359895679798545456789439765676789321679654356987679987654 |
||||
4568989894569998687892979877919894345986478998654567987656567996598754355698945998543245699789999765 |
||||
5699876795678969788921989989109789499894351989743456798767678987987643234567899876542123479899987976 |
||||
6989965689789459899542498999998678987643210578932345679878789898998792123676899965432014567929876987 |
||||
9976864578994344989656987999876534987654592469321236789989899789329989012345789987843123458912965698 |
||||
8985923467890123478968976899987621298766784578910123499993998543219879193456891298954434568929894569 |
||||
7654312356921354567899865678998542379879897699321234568932987654598768989569910129865677679998643459 |
||||
7842101239892465679999654567999653457989989789432545699653998767697657978978923457978788989998764578 |
||||
6543214356789568799998732356899764568997679896543756789979899879898744567899944767989899899899865789 |
||||
7654525487897689899876420124678975789996431997679898898998799989999932978967899988999987678789977897 |
||||
8775686598979799998765431285789996799987549989989989957899678994598763799545988999129876567698998956 |
||||
9989887679865988999896542376798987899999698777899976546789569965699854678934567898999865466567899349 |
||||
5698998989534567998987664567987699988931987656789988723499439876989965799123698987689654315456791298 |
||||
4567899998676979887699775679999532167899894345678999834568921989879876893244569754598765402367893987 |
||||
5989989769989897654545986799898954358998765456789321946789910198765987894997689643459897514589999996 |
||||
6795678999998796543235697998767895469999876567898943497899891987654598999889789652667959776689898987 |
||||
7894569878997689632123998989656976979981987698967964598998789999743459987679997543489542987896797699 |
||||
8932398767586579743434899978945897898870198799459895799878689998432369876568898764678931099945689431 |
||||
9543987656323489854575698656934789987653239912349789987964567896543498967456789898799992998756797210 |
||||
8665796546212349965689987545895678999795356799498678986543456989654987652345891949898789899898965423 |
||||
9976987632101467896798554436789789439896987898987567965432345678969887431337990134987678789969876675 |
||||
5698999543232345697987432124592995423997999967896459876521256899998776210126789999876598689756989776 |
||||
3569898674346556789996549036693987549979323456789345987432768999987654321234598789989434568934595987 |
||||
8698798765456687899989698945789999798765444578993234896545679988798765438756789578998623457899694598 |
||||
9987659878767798999878987957897889999879556989654346798959899976549887659867895489876534669998989999 |
||||
5699845989979899298767896898986578998989669998765457899767999895326998767998965345997645978987767899 |
||||
4987631296989932109656355789975467987898798889976769979978998789435679878999879469998756789896646789 |
||||
3986542345698654998743234678954389876569897679899878967989877698945797989999998998999867898765434890 |
||||
4987653457789799775810124589643298765456986565678989556299956567896896599878987887899998929854324691 |
||||
5698767878892987654321238899955129892349876464899995432198843477897999323459765676998799013992013989 |
||||
9979878989991098775543646789876399989498765343487896546297652346969788912399834345899695324989329978 |
||||
8767989796789989987655757899987988678997654212346797757986543457956567893987321256789589439878998967 |
||||
7656896545678976598866869998999876512987654301237898868997654579544456789875452387893478998767987656 |
||||
9738998437899865329877978956798765433496543212578999978998785989432345678986563598954567969659876544 |
||||
7629689545678974310989989432349986595987854323456893989129896792101567899997654569767679854546985433 |
||||
5434578959899765451398996545689597689898965454578912494298987893243459998999867689978898763434599212 |
||||
6545789767953986532567987856789498798769976579789894999987998954956598787895979799989987662125988301 |
||||
7856892979892397645698998969892359989656797679896799898976569879897987676796989999699876543019876312 |
||||
8769921989799498776789569878999459876545798789945698787895456999769876585689999998567987653236995423 |
||||
9878932998688999987893499989698998765434989890199987656965345987656987434567899876439798765345986564 |
||||
8989549876567896898992989796587899987325679931987654545893239876545987323489910987649649976456798775 |
||||
7497657988456954559789876572456799875456789899996543236789129766434596544595439998798432989987899889 |
||||
6569878999567893245699798341248678999578998658789654127679297654323987965689598999897521299999998996 |
||||
7698989998688932134987653210234569878989876545699943234568979865434899878799987899995439988913987895 |
||||
9987699889799541035699654321234698767999985435567899545678956976645789989899876789987598877894996654 |
||||
9876597679899432123498765452345987656899876423456998757789349798756892193998765679398697656999875443 |
||||
3987986546998543234679876743456797645799989910287899768996298659977891012989324589219987647899954321 |
||||
2199977634987657845789998754567988534678999891235989879765498843298932229876434695456898729999765210 |
||||
3398965420199789657893198766678976323459998789345678989879987654129646547986565789667999835798754321 |
||||
9987896554239898768999019897789875212345987698969799596997698865439897656798779899989989545679865432 |
||||
8766799669999929879198998998898984303459876567998999435989539976545998767899889999998978957999877545 |
||||
9954678998789012989297897549957895212569865456897678949878920987676999878942999987987569998910987696 |
||||
8765789654699123499986789321245987823498754349976577898769891398987898989656789876643468999699998989 |
||||
9986898943488939698765698932359876437987653298765456789655789459598976499867898765432127896587899878 |
||||
9998956732367898998983567893467987545698732129954398998744568969329995354978999865321056789456798769 |
||||
9899542101456997997542456789579998957789821019894289987623458978910989212989899976435145699212977556 |
||||
8789653212568976987631345997698999998997932198743168976545667899999878901296789997543234578909865445 |
||||
7698765333457895798545456789997899899876544987654347897996778921987767892345789898654357678998754323 |
||||
6549896544679934679656768999876585799987865699765756789889889899876645789556798798765468999698768210 |
||||
5431987758989124569867989799975464689999976899879867895678996799865434678967987679987689996569989432 |
||||
3210198767891012679878995689864343457998987987989879924899345679876656989979897565699998989432399553 |
||||
4523459898942123789989987897653212346897598995498989215679234578988767993298769434569876578941498764 |
||||
6735568999643445678990198987543203567965439875357894326789146678999879754579854325678998459653459895 |
@ -0,0 +1,108 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"bufio" |
||||
"fmt" |
||||
"os" |
||||
"sort" |
||||
"strconv" |
||||
) |
||||
|
||||
func partOne(heightmap [][]uint8) { |
||||
riskLevel := 0 |
||||
for y, _ := range heightmap { |
||||
for x, _ := range heightmap[y] { |
||||
if (y-1 >= 0 && heightmap[y][x] >= heightmap[y-1][x]) || |
||||
(y+1 <= len(heightmap)-1 && heightmap[y][x] >= heightmap[y+1][x]) || |
||||
(x-1 >= 0 && heightmap[y][x] >= heightmap[y][x-1]) || |
||||
(x+1 <= len(heightmap[y])-1 && heightmap[y][x] >= heightmap[y][x+1]) { |
||||
continue |
||||
} |
||||
|
||||
riskLevel += int(heightmap[y][x]) + 1 |
||||
} |
||||
} |
||||
|
||||
fmt.Printf("Part One Answer: %d\n", riskLevel) |
||||
} |
||||
|
||||
func getBasinSize(heightmap [][]uint8, basinMap [][]bool, x int, y int) int { |
||||
basinMap[y][x] = true |
||||
basinSize := 1 |
||||
|
||||
if x-1 >= 0 && heightmap[y][x-1] != 9 && !basinMap[y][x-1] { |
||||
basinSize += getBasinSize(heightmap, basinMap, x-1, y) |
||||
} |
||||
|
||||
if x+1 <= len(heightmap[y]) - 1 && heightmap[y][x+1] != 9 && !basinMap[y][x+1] { |
||||
basinSize += getBasinSize(heightmap, basinMap, x+1, y) |
||||
} |
||||
|
||||
if y-1 >= 0 && heightmap[y-1][x] != 9 && !basinMap[y-1][x] { |
||||
basinSize += getBasinSize(heightmap, basinMap, x, y-1) |
||||
} |
||||
|
||||
if y+1 <= len(heightmap) - 1 && heightmap[y+1][x] != 9 && !basinMap[y+1][x] { |
||||
basinSize += getBasinSize(heightmap, basinMap, x, y+1) |
||||
} |
||||
|
||||
return basinSize |
||||
} |
||||
|
||||
func partTwo(heightmap [][]uint8) { |
||||
basinMap := make([][]bool, len(heightmap)) |
||||
for basinIndex, _ := range basinMap { |
||||
basinMap[basinIndex] = make([]bool, len(heightmap[0])) |
||||
} |
||||
|
||||
basins := []int{} |
||||
for y, _ := range heightmap { |
||||
for x, _ := range heightmap[y] { |
||||
if heightmap[y][x] == 9 || basinMap[y][x] { |
||||
continue |
||||
} |
||||
|
||||
basinSize := getBasinSize(heightmap, basinMap, x, y) |
||||
|
||||
basins = append(basins, basinSize) |
||||
} |
||||
} |
||||
|
||||
sort.Ints(basins) |
||||
|
||||
fmt.Printf("Part Two Answer: %d\n", basins[len(basins) - 1] * basins[len(basins) - 2] * basins[len(basins) - 3]) |
||||
} |
||||
|
||||
func main() { |
||||
file, err := os.Open("./input") |
||||
if err != nil { |
||||
fmt.Printf("[Error] Unable to open file") |
||||
os.Exit(1) |
||||
} |
||||
defer file.Close() |
||||
|
||||
heightmap := [][]uint8{} |
||||
y := 0 |
||||
scanner := bufio.NewScanner(file) |
||||
for scanner.Scan() { |
||||
rowString := scanner.Text() |
||||
if len(rowString) == 0 { |
||||
continue |
||||
} |
||||
|
||||
heightmap = append(heightmap, make([]uint8, len(rowString))) |
||||
for x, heightCharacter := range rowString { |
||||
height, err := strconv.Atoi(string(heightCharacter)) |
||||
if err != nil { |
||||
fmt.Printf("[Error] Invalid height") |
||||
continue |
||||
} |
||||
heightmap[y][x] = uint8(height) |
||||
} |
||||
|
||||
y += 1 |
||||
} |
||||
|
||||
partOne(heightmap) |
||||
partTwo(heightmap) |
||||
} |
Loading…
Reference in new issue