1
0

7 Коммитууд 8c08a78806 ... ddb2e8f96b

Эзэн SHA1 Мессеж Огноо
  Piotr Czajkowski ddb2e8f96b Added description 1 долоо хоног өмнө
  Piotr Czajkowski 3f4d626971 DRY 1 долоо хоног өмнө
  Piotr Czajkowski f86a1f286b Solved part2 1 долоо хоног өмнө
  Piotr Czajkowski c04a463b07 Preparing for part2 1 долоо хоног өмнө
  Piotr Czajkowski bd559fffa7 Solved part1 1 долоо хоног өмнө
  Piotr Czajkowski 727ebb771e Not yet 1 долоо хоног өмнө
  Piotr Czajkowski ee6e2cb930 Able to read input 1 долоо хоног өмнө
3 өөрчлөгдсөн 295 нэмэгдсэн , 0 устгасан
  1. 112 0
      10/code.go
  2. 130 0
      10/description.txt
  3. 53 0
      10/input

+ 112 - 0
10/code.go

@@ -0,0 +1,112 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+)
+
+type Point struct {
+	y, x  int
+	value byte
+}
+
+func (p *Point) key() string {
+	return fmt.Sprintf("%d_%d", p.y, p.x)
+}
+
+func readInput(file *os.File) [][]byte {
+	scanner := bufio.NewScanner(file)
+	var matrix [][]byte
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		matrix = append(matrix, []byte(line))
+	}
+
+	return matrix
+}
+
+var directions [][]int = [][]int{
+	{0, -1}, {1, 0}, {0, 1}, {-1, 0},
+}
+
+func getMoves(reindeer Point, matrix [][]byte, xMax, yMax int) []Point {
+	var moves []Point
+	for _, direction := range directions {
+		move := Point{x: reindeer.x + direction[0], y: reindeer.y + direction[1]}
+		if move.x >= 0 && move.x < xMax &&
+			move.y >= 0 && move.y < yMax && matrix[move.y][move.x]-reindeer.value == 1 {
+			move.value = matrix[move.y][move.x]
+			moves = append(moves, move)
+		}
+	}
+
+	return moves
+}
+
+func hike(reindeer Point, matrix [][]byte, xMax, yMax int, oneWay bool) int {
+	var nines int
+	visited := make(map[string]bool)
+
+	moves := []Point{reindeer}
+	for len(moves) > 0 {
+		current := moves[0]
+		moves = moves[1:]
+		if matrix[current.y][current.x] == '9' {
+			nines++
+		}
+
+		newMoves := getMoves(current, matrix, xMax, yMax)
+		for _, newMove := range newMoves {
+			if oneWay {
+				if !visited[newMove.key()] {
+					moves = append(moves, newMove)
+					visited[newMove.key()] = true
+				}
+			} else {
+				moves = append(moves, newMove)
+			}
+		}
+	}
+
+	return nines
+}
+
+func solve(matrix [][]byte, oneWay bool) int {
+	var result int
+	xMax := len(matrix[0])
+	yMax := len(matrix)
+
+	for y := range matrix {
+		for x := range matrix[y] {
+			if matrix[y][x] == '0' {
+				reindeer := Point{x: x, y: y, value: '0'}
+				result += hike(reindeer, matrix, xMax, yMax, oneWay)
+			}
+		}
+	}
+
+	return result
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+	}
+
+	matrix := readInput(file)
+	fmt.Println("Part1:", solve(matrix, true))
+	fmt.Println("Part2:", solve(matrix, false))
+}

+ 130 - 0
10/description.txt

@@ -0,0 +1,130 @@
+--- Day 10: Hoof It ---
+
+You all arrive at a Lava Production Facility on a floating island in the sky. As the others begin to search the massive industrial complex, you feel a small nose boop your leg and look down to discover a reindeer wearing a hard hat.
+
+The reindeer is holding a book titled "Lava Island Hiking Guide". However, when you open the book, you discover that most of it seems to have been scorched by lava! As you're about to ask how you can help, the reindeer brings you a blank topographic map of the surrounding area (your puzzle input) and looks up at you excitedly.
+
+Perhaps you can help fill in the missing hiking trails?
+
+The topographic map indicates the height at each position using a scale from 0 (lowest) to 9 (highest). For example:
+
+0123
+1234
+8765
+9876
+
+Based on un-scorched scraps of the book, you determine that a good hiking trail is as long as possible and has an even, gradual, uphill slope. For all practical purposes, this means that a hiking trail is any path that starts at height 0, ends at height 9, and always increases by a height of exactly 1 at each step. Hiking trails never include diagonal steps - only up, down, left, or right (from the perspective of the map).
+
+You look up from the map and notice that the reindeer has helpfully begun to construct a small pile of pencils, markers, rulers, compasses, stickers, and other equipment you might need to update the map with hiking trails.
+
+A trailhead is any position that starts one or more hiking trails - here, these positions will always have height 0. Assembling more fragments of pages, you establish that a trailhead's score is the number of 9-height positions reachable from that trailhead via a hiking trail. In the above example, the single trailhead in the top left corner has a score of 1 because it can reach a single 9 (the one in the bottom left).
+
+This trailhead has a score of 2:
+
+...0...
+...1...
+...2...
+6543456
+7.....7
+8.....8
+9.....9
+
+(The positions marked . are impassable tiles to simplify these examples; they do not appear on your actual topographic map.)
+
+This trailhead has a score of 4 because every 9 is reachable via a hiking trail except the one immediately to the left of the trailhead:
+
+..90..9
+...1.98
+...2..7
+6543456
+765.987
+876....
+987....
+
+This topographic map contains two trailheads; the trailhead at the top has a score of 1, while the trailhead at the bottom has a score of 2:
+
+10..9..
+2...8..
+3...7..
+4567654
+...8..3
+...9..2
+.....01
+
+Here's a larger example:
+
+89010123
+78121874
+87430965
+96549874
+45678903
+32019012
+01329801
+10456732
+
+This larger example has 9 trailheads. Considering the trailheads in reading order, they have scores of 5, 6, 5, 3, 1, 3, 5, 3, and 5. Adding these scores together, the sum of the scores of all trailheads is 36.
+
+The reindeer gleefully carries over a protractor and adds it to the pile. What is the sum of the scores of all trailheads on your topographic map?
+
+Your puzzle answer was 776.
+--- Part Two ---
+
+The reindeer spends a few minutes reviewing your hiking trail map before realizing something, disappearing for a few minutes, and finally returning with yet another slightly-charred piece of paper.
+
+The paper describes a second way to measure a trailhead called its rating. A trailhead's rating is the number of distinct hiking trails which begin at that trailhead. For example:
+
+.....0.
+..4321.
+..5..2.
+..6543.
+..7..4.
+..8765.
+..9....
+
+The above map has a single trailhead; its rating is 3 because there are exactly three distinct hiking trails which begin at that position:
+
+.....0.   .....0.   .....0.
+..4321.   .....1.   .....1.
+..5....   .....2.   .....2.
+..6....   ..6543.   .....3.
+..7....   ..7....   .....4.
+..8....   ..8....   ..8765.
+..9....   ..9....   ..9....
+
+Here is a map containing a single trailhead with rating 13:
+
+..90..9
+...1.98
+...2..7
+6543456
+765.987
+876....
+987....
+
+This map contains a single trailhead with rating 227 (because there are 121 distinct hiking trails that lead to the 9 on the right edge and 106 that lead to the 9 on the bottom edge):
+
+012345
+123456
+234567
+345678
+4.6789
+56789.
+
+Here's the larger example from before:
+
+89010123
+78121874
+87430965
+96549874
+45678903
+32019012
+01329801
+10456732
+
+Considering its trailheads in reading order, they have ratings of 20, 24, 10, 4, 1, 4, 5, 8, and 5. The sum of all trailhead ratings in this larger example topographic map is 81.
+
+You're not sure how, but the reindeer seems to have crafted some tiny flags out of toothpicks and bits of paper and is using them to mark trailheads on your topographic map. What is the sum of the ratings of all trailheads?
+
+Your puzzle answer was 1657.
+
+Both parts of this puzzle are complete! They provide two gold stars: **

+ 53 - 0
10/input

@@ -0,0 +1,53 @@
+65456780121098450187634569763232345103213014321223456
+56565097632787543296323478890141234234102125630310767
+67874198545601612345610165765650105145693496543407898
+32923238723432709654326543034789076008786587854343210
+41010129610549878723017832129878989019895674963210349
+03498034534698965014898901038965432123107895674310478
+12567012345787854198743230123453219654256456789321565
+01698107659834723085650134562104508723340349876459874
+98787238543225610176590121875698698812451210145960123
+07456549850110765203485430984788767904322109236871234
+12349654763234894312176534983109456765012218347120105
+45898763120125234523054325672112309876544367898034876
+36765812034576127654369010766043218987432458765565989
+29876903107681038923478001897652345898901089014877856
+10945789218996987610569123078901236754321198123966987
+89032654309345678510787430121230109865120567001855496
+70121023498210709425698945230381210778034056532743345
+63210110567601812334321876545496321689765123443412210
+54216787014503956745430965496587434509894329856301101
+09105496923212345866787634987676543213456018763456932
+18012387836781078975898523432345232122347890345367873
+27601078745896769784309410541230143021038901276216784
+34587669654305854693210305670321056523427987689105698
+03490548741214903543211234781987897012310094576543567
+12321239230903212650106521892016798987454123467612430
+12332102107812121786787430752105678901960013438900421
+01489237876543080695890124567234787017871012543321530
+90574323965012891234301233208943296323702387654487651
+87665010121026700343210120112350185410213498703599341
+96556901212345619656903234013461234554396569812678210
+03467832303434328743874105684570789689487876101432387
+12346345694540345012565106799688778776536964566501498
+01653210789691236522210239888799669890123453677890567
+18764101988780987121329845677234550187652122989108906
+89765001877101071030456776540112341290343001010267210
+67896982769612132549105689432101032301234578981354310
+78767853458743247678014988654312121000225665432458981
+69656765432658958976529812763243029810116767898567652
+30345899891067867789438701890156710723209852101438943
+21230014780890986290105610981260823654367643067324321
+10101423634761870121234327876501994569498543458015100
+45672344543052765436543234566782987678534412109176211
+34985495652143781287430110545093456767623303678989321
+43856784743430690398710325432112129865414510510076450
+32981012892121589459621234521001036772301623423165569
+01670143345023498764560149693456345681066739654234678
+12365294256510107643498238782347652397659848765985589
+03454385107898212532567345601098701498943707678876432
+12763476543789123431052101598709876510232110569845001
+29854307630076034521043015691612389323101023454032132
+38765218921165087654321126780543432134569878998108941
+47894356712234198765230105621234567023878565089237650
+21012349803343289890121234310123498012967432176546321