|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | +"flag" |
| 5 | +"fmt" |
| 6 | +"sort" |
| 7 | +"strings" |
| 8 | +"time" |
| 9 | + |
| 10 | +"github.com/Galzzly/adventofcode/utilities" |
| 11 | +) |
| 12 | + |
| 13 | +/* |
| 14 | +Read in the input file, and parse it using the utility function to split the data into |
| 15 | +useable slices. |
| 16 | +Use the countCalories function to calculate the colories that each elf is carrying, and |
| 17 | +reverse sort the results to allow for quicker processing afterwards. |
| 18 | +Part 1 requires the total amount of calories being carried by the elf carrying the most. |
| 19 | +Part 2 requires the total amount of calories being carried by the top three elves. |
| 20 | +*/ |
| 21 | + |
| 22 | +func main() { |
| 23 | +start := time.Now() |
| 24 | +var file string |
| 25 | +flag.StringVar(&file, "input", "input.txt", "") |
| 26 | +flag.Parse() |
| 27 | + |
| 28 | +elves := utilities.ReadFile(file, func(f string) [][]string { |
| 29 | +res := [][]string{} |
| 30 | +for _, line := range strings.Split(strings.TrimSpace(f), "\n\n") { |
| 31 | +res = append(res, strings.Split(line, "\n")) |
| 32 | +} |
| 33 | +return res |
| 34 | +}) |
| 35 | + |
| 36 | +calories := countCalories(elves) |
| 37 | +t1 := time.Now() |
| 38 | +fmt.Println("Part 1:", calories[0], "- Took:", time.Since(t1)) |
| 39 | +t2 := time.Now() |
| 40 | +fmt.Println("Part 2:", utilities.SumArray(calories[:3]), "- Took:", time.Since(t2)) |
| 41 | +fmt.Println("Total time:", time.Since(start)) |
| 42 | +} |
| 43 | + |
| 44 | +func countCalories(elves [][]string) (res []int) { |
| 45 | +res = make([]int, len(elves)) |
| 46 | +for i := range elves { |
| 47 | +for _, c := range elves[i] { |
| 48 | +res[i] += utilities.Atoi(c) |
| 49 | +} |
| 50 | +} |
| 51 | +sort.Sort(sort.Reverse(sort.IntSlice(res))) |
| 52 | +return |
| 53 | +} |
0 commit comments