Skip to content

Commit

Permalink
2024 Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Dec 1, 2024
1 parent ab6db6b commit 9dd08d6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 40 deletions.
84 changes: 84 additions & 0 deletions 2024/day01/day01.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package day01

import (
"bufio"
"log"
"os"
"slices"
"strconv"
"strings"
)

func Solution1(filepath string) int {
l, r := readInput(filepath)
slices.Sort(l)
slices.Sort(r)

result := 0
similarMap := make(map[int]int)
for i := 0; i < len(l); i++ {
diff := r[i] - l[i]
if diff < 0 {
diff *= -1
}

similarMap[r[i]] += 1
result += diff
}

return result
}

func Solution2(filepath string) int {
l, r := readInput(filepath)
slices.Sort(l)
slices.Sort(r)

similarMap := make(map[int]int)
for i := 0; i < len(l); i++ {
similarMap[r[i]] += 1
}

result := 0
for _, n := range l {
result += n * similarMap[n]
}

return result
}

func readInput(filepath string) (left, right []int) {
file, err := os.Open(filepath)
if err != nil {
log.Fatalf("Failed to open the input file: %s\n", err.Error())
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
inputNumPair := strings.SplitN(scanner.Text(), " ", 2)
if len(inputNumPair) != 2 {
log.Fatalf("Failed to parse input numbers pair %s\n", scanner.Text())
}
l, r := parseInt(strings.TrimSpace(inputNumPair[0])), parseInt(strings.TrimSpace(inputNumPair[1]))

left = append(left, l)
right = append(right, r)
}
if err = scanner.Err(); err != nil {
log.Fatalf("Error during input file read: %s\n", err.Error())
}

if len(left) != len(right) {
log.Fatal("left and right length dont match")
}

return left, right
}

func parseInt(strNum string) int {
num, err := strconv.ParseInt(strNum, 10, 64)
if err != nil {
log.Fatalf("Failed to parse number %s with: %s\n", err.Error())

Check failure on line 80 in 2024/day01/day01.go

View workflow job for this annotation

GitHub Actions / build

log.Fatalf format %s reads arg #2, but call has 1 arg
}

return int(num)
}
86 changes: 46 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,78 @@ import (
aoc2023day17 "advent-of-code/2023/day17"
aoc2023day18 "advent-of-code/2023/day18"
aoc2023day19 "advent-of-code/2023/day19"
aoc2024day01 "advent-of-code/2024/day01"
"fmt"
"time"
)

func runSolution(day, part int, s func() int) {
func runSolution(year, day, part int, s func() int) {
start := time.Now()
res := s()
elapsed := time.Since(start)
fmt.Printf("Day %02d, solution %d: %v - %v\n", day, part, res, elapsed.Round(time.Microsecond))
fmt.Printf("%d: Day %02d, solution %d: %v - %v\n", year, day, part, res, elapsed.Round(time.Microsecond))
}

func main() {
runSolution(1, 1, func() int { return aoc2023day01.Solution1("2023/inputs/day01") })
runSolution(1, 2, func() int { return aoc2023day01.Solution2("2023/inputs/day01") })
// 2023
runSolution(2023, 1, 1, func() int { return aoc2023day01.Solution1("2023/inputs/day01") })
runSolution(2023, 1, 2, func() int { return aoc2023day01.Solution2("2023/inputs/day01") })

runSolution(2, 1, func() int { return aoc2023day02.Solution1("2023/inputs/day02") })
runSolution(2, 2, func() int { return aoc2023day02.Solution2("2023/inputs/day02") })
runSolution(2023, 2, 1, func() int { return aoc2023day02.Solution1("2023/inputs/day02") })
runSolution(2023, 2, 2, func() int { return aoc2023day02.Solution2("2023/inputs/day02") })

runSolution(3, 1, func() int { return aoc2023day03.Solution1("2023/inputs/day03") })
runSolution(3, 2, func() int { return aoc2023day03.Solution2("2023/inputs/day03") })
runSolution(2023, 3, 1, func() int { return aoc2023day03.Solution1("2023/inputs/day03") })
runSolution(2023, 3, 2, func() int { return aoc2023day03.Solution2("2023/inputs/day03") })

runSolution(4, 1, func() int { return aoc2023day04.Solution1("2023/inputs/day04") })
runSolution(4, 2, func() int { return aoc2023day04.Solution2("2023/inputs/day04") })
runSolution(2023, 4, 1, func() int { return aoc2023day04.Solution1("2023/inputs/day04") })
runSolution(2023, 4, 2, func() int { return aoc2023day04.Solution2("2023/inputs/day04") })

runSolution(5, 1, func() int { return aoc2023day05.Solution1("2023/inputs/day05") })
runSolution(5, 2, func() int { return aoc2023day05.Solution2("2023/inputs/day05") })
runSolution(2023, 5, 1, func() int { return aoc2023day05.Solution1("2023/inputs/day05") })
runSolution(2023, 5, 2, func() int { return aoc2023day05.Solution2("2023/inputs/day05") })

runSolution(6, 1, func() int { return aoc2023day06.Solution1("2023/inputs/day06") })
runSolution(6, 2, func() int { return aoc2023day06.Solution2("2023/inputs/day06") })
runSolution(2023, 6, 1, func() int { return aoc2023day06.Solution1("2023/inputs/day06") })
runSolution(2023, 6, 2, func() int { return aoc2023day06.Solution2("2023/inputs/day06") })

runSolution(7, 1, func() int { return aoc2023day07.Solution1("2023/inputs/day07") })
runSolution(7, 2, func() int { return aoc2023day07.Solution2("2023/inputs/day07") })
runSolution(2023, 7, 1, func() int { return aoc2023day07.Solution1("2023/inputs/day07") })
runSolution(2023, 7, 2, func() int { return aoc2023day07.Solution2("2023/inputs/day07") })

runSolution(8, 1, func() int { return aoc2023day08.Solution1("2023/inputs/day08") })
runSolution(8, 2, func() int { return aoc2023day08.Solution2("2023/inputs/day08") })
runSolution(2023, 8, 1, func() int { return aoc2023day08.Solution1("2023/inputs/day08") })
runSolution(2023, 8, 2, func() int { return aoc2023day08.Solution2("2023/inputs/day08") })

runSolution(9, 1, func() int { return aoc2023day09.Solution1("2023/inputs/day09") })
runSolution(9, 2, func() int { return aoc2023day09.Solution2("2023/inputs/day09") })
runSolution(2023, 9, 1, func() int { return aoc2023day09.Solution1("2023/inputs/day09") })
runSolution(2023, 9, 2, func() int { return aoc2023day09.Solution2("2023/inputs/day09") })

runSolution(10, 1, func() int { return aoc2023day10.Solution1("2023/inputs/day10") })
runSolution(10, 2, func() int { return aoc2023day10.Solution2("2023/inputs/day10") })
runSolution(2023, 10, 1, func() int { return aoc2023day10.Solution1("2023/inputs/day10") })
runSolution(2023, 10, 2, func() int { return aoc2023day10.Solution2("2023/inputs/day10") })

runSolution(11, 1, func() int { return aoc2023day11.Solution1("2023/inputs/day11") })
runSolution(11, 2, func() int { return aoc2023day11.Solution2("2023/inputs/day11") })
runSolution(2023, 11, 1, func() int { return aoc2023day11.Solution1("2023/inputs/day11") })
runSolution(2023, 11, 2, func() int { return aoc2023day11.Solution2("2023/inputs/day11") })

runSolution(12, 1, func() int { return aoc2023day12.Solution1("2023/inputs/day12") })
runSolution(12, 2, func() int { return aoc2023day12.Solution2("2023/inputs/day12") })
runSolution(2023, 12, 1, func() int { return aoc2023day12.Solution1("2023/inputs/day12") })
runSolution(2023, 12, 2, func() int { return aoc2023day12.Solution2("2023/inputs/day12") })

runSolution(13, 1, func() int { return aoc2023day13.Solution1("2023/inputs/day13") })
runSolution(13, 2, func() int { return aoc2023day13.Solution2("2023/inputs/day13") })
runSolution(2023, 13, 1, func() int { return aoc2023day13.Solution1("2023/inputs/day13") })
runSolution(2023, 13, 2, func() int { return aoc2023day13.Solution2("2023/inputs/day13") })

runSolution(14, 1, func() int { return aoc2023day14.Solution1("2023/inputs/day14") })
runSolution(14, 2, func() int { return aoc2023day14.Solution2("2023/inputs/day14") })
runSolution(2023, 14, 1, func() int { return aoc2023day14.Solution1("2023/inputs/day14") })
runSolution(2023, 14, 2, func() int { return aoc2023day14.Solution2("2023/inputs/day14") })

runSolution(15, 1, func() int { return aoc2023day15.Solution1("2023/inputs/day15") })
runSolution(15, 2, func() int { return aoc2023day15.Solution2("2023/inputs/day15") })
runSolution(2023, 15, 1, func() int { return aoc2023day15.Solution1("2023/inputs/day15") })
runSolution(2023, 15, 2, func() int { return aoc2023day15.Solution2("2023/inputs/day15") })

runSolution(16, 1, func() int { return aoc2023day16.Solution1("2023/inputs/day16") })
runSolution(16, 2, func() int { return aoc2023day16.Solution2("2023/inputs/day16") })
runSolution(2023, 16, 1, func() int { return aoc2023day16.Solution1("2023/inputs/day16") })
runSolution(2023, 16, 2, func() int { return aoc2023day16.Solution2("2023/inputs/day16") })

runSolution(17, 1, func() int { return aoc2023day17.Solution1("2023/inputs/day17") })
runSolution(17, 2, func() int { return aoc2023day17.Solution2("2023/inputs/day17") })
runSolution(2023, 17, 1, func() int { return aoc2023day17.Solution1("2023/inputs/day17") })
runSolution(2023, 17, 2, func() int { return aoc2023day17.Solution2("2023/inputs/day17") })

runSolution(18, 1, func() int { return aoc2023day18.Solution1("2023/inputs/day18") })
runSolution(18, 2, func() int { return aoc2023day18.Solution2("2023/inputs/day18") })
runSolution(2023, 18, 1, func() int { return aoc2023day18.Solution1("2023/inputs/day18") })
runSolution(2023, 18, 2, func() int { return aoc2023day18.Solution2("2023/inputs/day18") })

runSolution(19, 1, func() int { return aoc2023day19.Solution1("2023/inputs/day19") })
runSolution(19, 2, func() int { return aoc2023day19.Solution2("2023/inputs/day19") })
runSolution(2023, 19, 1, func() int { return aoc2023day19.Solution1("2023/inputs/day19") })
runSolution(2023, 19, 2, func() int { return aoc2023day19.Solution2("2023/inputs/day19") })

// 2024
runSolution(2024, 1, 1, func() int { return aoc2024day01.Solution1("2024/inputs/day01") })
runSolution(2024, 1, 2, func() int { return aoc2024day01.Solution2("2024/inputs/day01") })
}

0 comments on commit 9dd08d6

Please sign in to comment.