-
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
1 changed file
with
84 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
+++ | ||
title = "Advent Of Code 2024: My notes" | ||
date = 2024-12-01T11:10:46+05:30 | ||
draft = false | ||
showToc = true | ||
cover.image = '' | ||
tags = ["tech"] | ||
+++ | ||
|
||
> The aoc_lib package used in these solutions is my own custom helper functions library. You can find the code [here](https://github.com/jitesh117/aoc_lib). I'll try to post my solutions daily. These are probably not the most optimized solutions. That is not my goal with advent of code honestly, I do these just for fun. | ||
## Day 1 | ||
|
||
[Day 1](https://adventofcode.com/2024/day/1) was pretty straightforward. Just some basic array magic. I don't think the code needs much explanation. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
aoc_lib "github.com/Jitesh117/aoc_lib" | ||
) | ||
|
||
func part1(input []string) int { | ||
var leftList, rightList []int | ||
|
||
for _, line := range input { | ||
nums := aoc_lib.StringToIntSlice(line) | ||
leftList = append(leftList, nums[0]) | ||
rightList = append(rightList, nums[1]) | ||
} | ||
|
||
sort.Ints(leftList) | ||
sort.Ints(rightList) | ||
|
||
result := 0 | ||
for i := 0; i < len(leftList); i++ { | ||
diff := leftList[i] - rightList[i] | ||
if diff < 0 { | ||
diff = -diff | ||
} | ||
result += diff | ||
} | ||
|
||
return result | ||
} | ||
|
||
func part2(input []string) int { | ||
var leftList, rightList []int | ||
|
||
for _, line := range input { | ||
nums := aoc_lib.StringToIntSlice(line) | ||
leftList = append(leftList, nums[0]) | ||
rightList = append(rightList, nums[1]) | ||
} | ||
|
||
rightCount := make(map[int]int) | ||
for _, num := range rightList { | ||
rightCount[num]++ | ||
} | ||
|
||
similarityScore := 0 | ||
for _, num := range leftList { | ||
if count, exists := rightCount[num]; exists { | ||
similarityScore += num * count | ||
} | ||
} | ||
|
||
return similarityScore | ||
} | ||
|
||
func main() { | ||
lines := aoc_lib.ReadLines("input.txt") | ||
|
||
resultPart1 := part1(lines) | ||
resultPart2 := part2(lines) | ||
|
||
fmt.Printf("Part 1: %d\n", resultPart1) | ||
fmt.Printf("Part 2: %d\n", resultPart2) | ||
} | ||
|
||
``` |