updated Day01 to use original input provided by adventofcode

This commit is contained in:
2024-12-11 20:11:23 +01:00
parent e6fdf88f0d
commit a215df0b04
5 changed files with 1067 additions and 2050 deletions

View File

@@ -1,31 +1,41 @@
package main
import (
"encoding/json"
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
)
func original() {
fmt.Println("Original Code")
content, err := ioutil.ReadFile("./input.json")
file, err := os.Open("./input.txt")
if err != nil {
log.Fatal("Error when opening input.json: ", err)
}
var payload map[string][]int
err = json.Unmarshal(content, &payload)
if err != nil {
log.Fatal("Error when unmarshalling input.json: ", err)
fileScanner := bufio.NewScanner(file)
fileScanner.Split(bufio.ScanLines)
var (
listA, listB []int
)
for fileScanner.Scan() {
slice := strings.Fields(fileScanner.Text())
num1, _ := strconv.Atoi(slice[0])
num2, _ := strconv.Atoi(slice[1])
listA = append(listA, num1)
listB = append(listB, num2)
}
// Part 1
listA := payload["list_a"]
listB := payload["list_b"]
file.Close()
// Part 1
sort.Ints(listA)
sort.Ints(listB)
@@ -41,9 +51,6 @@ func original() {
fmt.Println("Sum of Part 1: ", sumPart1)
// Part 2
listA = payload["list_a"]
listB = payload["list_b"]
sumPart2 := 0
for _, v1 := range listA {