diff options
author | Julien Dessaux | 2021-12-09 21:59:22 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-12-09 21:59:22 +0100 |
commit | f15c44dc186eedbe248127797b6d42fbfcaa9589 (patch) | |
tree | fa938a66a25edd6a7913449395257e1b685a420f | |
parent | Added solutions for 8th day : the seven segments code to guess (diff) | |
download | advent-of-code-f15c44dc186eedbe248127797b6d42fbfcaa9589.tar.gz advent-of-code-f15c44dc186eedbe248127797b6d42fbfcaa9589.tar.bz2 advent-of-code-f15c44dc186eedbe248127797b6d42fbfcaa9589.zip |
Added solutions for 9th day : minimums and basins in height map
Diffstat (limited to '')
-rw-r--r-- | 2021/09/example | 5 | ||||
-rw-r--r-- | 2021/09/first.go | 69 | ||||
-rw-r--r-- | 2021/09/first_v2.go | 64 | ||||
-rw-r--r-- | 2021/09/input | 100 | ||||
-rw-r--r-- | 2021/09/second.go | 85 |
5 files changed, 323 insertions, 0 deletions
diff --git a/2021/09/example b/2021/09/example new file mode 100644 index 0000000..6dee4a4 --- /dev/null +++ b/2021/09/example @@ -0,0 +1,5 @@ +2199943210 +3987894921 +9856789892 +8767896789 +9899965678 diff --git a/2021/09/first.go b/2021/09/first.go new file mode 100644 index 0000000..edfca87 --- /dev/null +++ b/2021/09/first.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +// DOES NOT WORK!!! + +func countMins(prev []byte, prevS []bool) (score int) { + for i, v := range prevS { + if v { + fmt.Println(prev[i]) + score += int(prev[i]) + 1 + } + } + return +} + +func main() { + score := 0 + + s := bufio.NewScanner(os.Stdin) + s.Scan() + width := len(s.Text()) + prevS := make([]bool, width) + prev := make([]byte, width) + for i, ch := range s.Text() { + prev[i] = byte(ch) - '0' + if i == 0 { + prevS[i] = true + } else if prev[i] < prev[i-1] { + prevS[i-1] = false + prevS[i] = true + } + } + + curS := make([]bool, width) + cur := make([]byte, width) + fmt.Println(s.Text()) + for s.Scan() { + for i, ch := range s.Text() { + cur[i] = byte(ch) - '0' + if i > 0 && cur[i] < cur[i-1] { + curS[i-1] = false + if cur[i] < prev[i] { + prevS[i] = false + curS[i] = true + } else { + curS[i] = false + } + } else { + if cur[i] < prev[i] { + prevS[i] = false + } + curS[i] = false + } + } + fmt.Printf("%+v\n", prevS) + fmt.Println(s.Text()) + score += countMins(prev, prevS) + copy(prev, cur) + copy(prevS, curS) + } + score += countMins(prev, prevS) + fmt.Printf("%+v\n", prevS) + fmt.Println(score) +} diff --git a/2021/09/first_v2.go b/2021/09/first_v2.go new file mode 100644 index 0000000..0c11bcd --- /dev/null +++ b/2021/09/first_v2.go @@ -0,0 +1,64 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +var ( + grid [][]byte = make([][]byte, 0) + width, height int +) + +func isMin(x, y int) bool { + var left, right, top, bottom byte + if x == 0 { + left = '9' + } else { + left = grid[x-1][y] + } + if x == height-1 { + right = '9' + } else { + right = grid[x+1][y] + } + if y == 0 { + top = '9' + } else { + top = grid[x][y-1] + } + if y == width-1 { + bottom = '9' + } else { + bottom = grid[x][y+1] + } + return grid[x][y] < top && grid[x][y] < bottom && grid[x][y] < left && grid[x][y] < right +} + +func main() { + s := bufio.NewScanner(os.Stdin) + s.Scan() + width = len(s.Text()) + for { + line := make([]byte, width) + for i, ch := range s.Text() { + line[i] = byte(ch) + } + grid = append(grid, line) + if !s.Scan() { + break + } + } + height = len(grid) + + score := 0 + for y := 0; y < width; y++ { + for x := 0; x < height; x++ { + if isMin(x, y) { + score += int(grid[x][y]-'0') + 1 + } + } + } + fmt.Println(score) +} diff --git a/2021/09/input b/2021/09/input new file mode 100644 index 0000000..c299a49 --- /dev/null +++ b/2021/09/input @@ -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 diff --git a/2021/09/second.go b/2021/09/second.go new file mode 100644 index 0000000..6cba3dd --- /dev/null +++ b/2021/09/second.go @@ -0,0 +1,85 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "sort" +) + +var ( + grid [][]byte = make([][]byte, 0) + width, height int +) + +func isMin(x, y int) bool { + var left, right, top, bottom byte + if x == 0 { + left = '9' + } else { + left = grid[x-1][y] + } + if x == height-1 { + right = '9' + } else { + right = grid[x+1][y] + } + if y == 0 { + top = '9' + } else { + top = grid[x][y-1] + } + if y == width-1 { + bottom = '9' + } else { + bottom = grid[x][y+1] + } + return grid[x][y] < top && grid[x][y] < bottom && grid[x][y] < left && grid[x][y] < right +} + +type Min struct { + x, y int +} + +var minimums []Min + +func main() { + s := bufio.NewScanner(os.Stdin) + s.Scan() + width = len(s.Text()) + for { + line := make([]byte, width) + for i, ch := range s.Text() { + line[i] = byte(ch) + } + grid = append(grid, line) + if !s.Scan() { + break + } + } + height = len(grid) + + for y := 0; y < width; y++ { + for x := 0; x < height; x++ { + if isMin(x, y) { + minimums = append(minimums, Min{x, y}) + } + } + } + + basins := make([]int, 0) + for _, m := range minimums { + basins = append(basins, computeBasinSize(m.x, m.y)) + } + sort.Ints(basins) + basins = basins[len(basins)-3:] + fmt.Println(basins[0] * basins[1] * basins[2]) +} + +func computeBasinSize(x, y int) int { + if x < 0 || x >= height || y < 0 || y >= width || grid[x][y] == '9' { + return 0 + } + grid[x][y] = '9' + return 1 + computeBasinSize(x-1, y) + computeBasinSize(x+1, y) + computeBasinSize(x, y-1) + computeBasinSize(x, y+1) +} |