-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
80 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/gdejong/advent-of-code-2024/internal/input" | ||
"github.com/gdejong/advent-of-code-2024/internal/must" | ||
"io" | ||
"os" | ||
"slices" | ||
) | ||
|
||
func main() { | ||
f := must.NoError(os.Open("cmd/day1/real_input.txt")) | ||
lines := input.Content("cmd/day1/real_input.txt") | ||
|
||
part1answer := part1(f) | ||
fmt.Println(part1answer) | ||
fmt.Println(part1(lines)) | ||
|
||
// Reset the file so we can it again for part 2 | ||
must.NoError(f.Seek(0, io.SeekStart)) | ||
|
||
part2answer := part2(f) | ||
fmt.Println(part2answer) | ||
fmt.Println(part2(lines)) | ||
} | ||
|
||
func part2(f *os.File) int { | ||
s := bufio.NewScanner(f) | ||
|
||
func part1(lines []string) int { | ||
var leftList []int | ||
var rightList []int | ||
|
||
for s.Scan() { | ||
line := s.Text() | ||
|
||
for _, line := range lines { | ||
var leftNumber int | ||
var rightNumber int | ||
must.NoError(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber)) | ||
leftList = append(leftList, leftNumber) | ||
rightList = append(rightList, rightNumber) | ||
} | ||
|
||
counterMap := make(map[int]int) | ||
slices.Sort(leftList) | ||
slices.Sort(rightList) | ||
|
||
for _, value := range rightList { | ||
if _, ok := counterMap[value]; ok { | ||
counterMap[value]++ | ||
} else { | ||
counterMap[value] = 1 | ||
} | ||
if len(leftList) != len(rightList) { | ||
panic("expected same length") | ||
} | ||
|
||
similarityScore := 0 | ||
for _, value := range leftList { | ||
counterMapValue, ok := counterMap[value] | ||
if !ok { | ||
continue | ||
distanceSum := 0 | ||
for i := 0; i < len(leftList); i++ { | ||
distance := leftList[i] - rightList[i] | ||
|
||
// Take the absolute value | ||
if distance < 0 { | ||
distance = distance * -1 | ||
} | ||
|
||
similarityScore += value * counterMapValue | ||
distanceSum += distance | ||
} | ||
|
||
return similarityScore | ||
return distanceSum | ||
} | ||
|
||
func part1(f *os.File) int { | ||
s := bufio.NewScanner(f) | ||
|
||
func part2(lines []string) int { | ||
var leftList []int | ||
var rightList []int | ||
|
||
for s.Scan() { | ||
line := s.Text() | ||
|
||
for _, line := range lines { | ||
var leftNumber int | ||
var rightNumber int | ||
must.NoError(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber)) | ||
leftList = append(leftList, leftNumber) | ||
rightList = append(rightList, rightNumber) | ||
} | ||
|
||
slices.Sort(leftList) | ||
slices.Sort(rightList) | ||
counterMap := make(map[int]int) | ||
|
||
if len(leftList) != len(rightList) { | ||
panic("expected same length") | ||
for _, value := range rightList { | ||
if _, ok := counterMap[value]; ok { | ||
counterMap[value]++ | ||
} else { | ||
counterMap[value] = 1 | ||
} | ||
} | ||
|
||
distanceSum := 0 | ||
for i := 0; i < len(leftList); i++ { | ||
distance := leftList[i] - rightList[i] | ||
|
||
// Take the absolute value | ||
if distance < 0 { | ||
distance = distance * -1 | ||
similarityScore := 0 | ||
for _, value := range leftList { | ||
counterMapValue, ok := counterMap[value] | ||
if !ok { | ||
continue | ||
} | ||
|
||
distanceSum += distance | ||
similarityScore += value * counterMapValue | ||
} | ||
|
||
return distanceSum | ||
return similarityScore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters