|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + fmt.Println("Part 1: ", part1()) |
| 12 | + fmt.Println("Part 2: ", part2(3)) |
| 13 | +} |
| 14 | + |
| 15 | +func part1() int64 { |
| 16 | + highestTotal := int64(0) |
| 17 | + inventories := parseElvesInventory(readInput("input.txt")) |
| 18 | + sumOfInventories := getSumArray(inventories) |
| 19 | + |
| 20 | + for _, total := range sumOfInventories { |
| 21 | + if total > highestTotal { |
| 22 | + highestTotal = int64(total) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return highestTotal |
| 27 | +} |
| 28 | + |
| 29 | +func part2(n int) int64 { |
| 30 | + // Finds n highest numbers in the input |
| 31 | + inventories := parseElvesInventory(readInput("input.txt")) |
| 32 | + sumOfInventories := getSumArray(inventories) |
| 33 | + // Sort sumOfInventories |
| 34 | + // Return the first n numbers |
| 35 | + |
| 36 | + for i := 0; i < len(sumOfInventories); i++ { |
| 37 | + for j := 0; j < len(sumOfInventories); j++ { |
| 38 | + if sumOfInventories[i] > sumOfInventories[j] { |
| 39 | + sumOfInventories[i], sumOfInventories[j] = sumOfInventories[j], sumOfInventories[i] |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return sum(sumOfInventories[:n]) |
| 45 | +} |
| 46 | + |
| 47 | +func readInput(inputfile string) []string { |
| 48 | + // Read the input file |
| 49 | + input, err := os.Open(inputfile) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + defer input.Close() |
| 54 | + var lines []string |
| 55 | + scanner := bufio.NewScanner(input) |
| 56 | + for scanner.Scan() { |
| 57 | + lines = append(lines, scanner.Text()) |
| 58 | + } |
| 59 | + return lines |
| 60 | +} |
| 61 | + |
| 62 | +func parseElvesInventory(input []string) map[int][]int64 { |
| 63 | + elvesInventory := make(map[int][]int64) |
| 64 | + inventoryIndex := 0 |
| 65 | + for _, line := range input { |
| 66 | + if elvesInventory[inventoryIndex] == nil { |
| 67 | + elvesInventory[inventoryIndex] = make([]int64, 0) |
| 68 | + } |
| 69 | + |
| 70 | + if line == "" { |
| 71 | + inventoryIndex++ |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + // Add the new item to the inventory |
| 76 | + item, err := strconv.ParseInt(line, 10, 64) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + elvesInventory[inventoryIndex] = append(elvesInventory[inventoryIndex], item) |
| 83 | + |
| 84 | + } |
| 85 | + return elvesInventory |
| 86 | +} |
| 87 | + |
| 88 | +func getSumArray(input map[int][]int64) []int64 { |
| 89 | + sumArray := make([]int64, len(input)) |
| 90 | + for i := 0; i < len(input); i++ { |
| 91 | + sumArray[i] = sum(input[i]) |
| 92 | + } |
| 93 | + return sumArray |
| 94 | +} |
| 95 | + |
| 96 | +func sum(input []int64) int64 { |
| 97 | + sum := int64(0) |
| 98 | + for _, item := range input { |
| 99 | + sum += item |
| 100 | + } |
| 101 | + return sum |
| 102 | +} |
0 commit comments