Skip to content

Commit

Permalink
feat: aoc notes blog
Browse files Browse the repository at this point in the history
  • Loading branch information
Jitesh117 committed Dec 1, 2024
1 parent 203b9c5 commit 84413c4
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions content/blog/aoc_2024.md
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)
}

```

0 comments on commit 84413c4

Please sign in to comment.