Browse Source

Solved part2

Piotr Czajkowski 2 weeks ago
parent
commit
487fd49d65
1 changed files with 22 additions and 0 deletions
  1. 22 0
      01/code.go

+ 22 - 0
01/code.go

@@ -51,6 +51,27 @@ func part1(lefts, rights []int) int {
 	return distance
 }
 
+func part2(lefts, rights []int) int {
+	onTheLeft := make(map[int]int)
+	for _, left := range lefts {
+		onTheLeft[left] = 0
+	}
+
+	for _, right := range rights {
+		_, ok := onTheLeft[right]
+		if ok {
+			onTheLeft[right]++
+		}
+	}
+
+	var result int
+	for _, left := range lefts {
+		result += left * onTheLeft[left]
+	}
+
+	return result
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -64,4 +85,5 @@ func main() {
 
 	lefts, rights := readInput(file)
 	fmt.Println("Part1:", part1(lefts, rights))
+	fmt.Println("Part2:", part2(lefts, rights))
 }