Skip to content

Commit e440425

Browse files
committed
refactoring
1 parent d5fc343 commit e440425

File tree

7 files changed

+164
-188
lines changed

7 files changed

+164
-188
lines changed

Diff for: Day1/part1.go

-37
This file was deleted.

Diff for: Day1/part2.go

-63
This file was deleted.

Diff for: Day1/solution.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"../util"
8+
)
9+
10+
func scanLines(path string, freq int, freqDict map[int]bool) (int, error) {
11+
12+
scanner, _ := util.ReadFile(path)
13+
14+
var firstRepeated int
15+
var foundValue = false
16+
17+
for scanner.Scan() {
18+
lineFreq, err := strconv.Atoi(scanner.Text())
19+
20+
if err != nil {
21+
return 0, err
22+
}
23+
24+
freq += lineFreq
25+
26+
_, freqItem := freqDict[freq]
27+
28+
if freqItem {
29+
firstRepeated = freq
30+
foundValue = true
31+
break
32+
}
33+
34+
freqDict[freq] = true
35+
}
36+
37+
if !foundValue {
38+
return scanLines(path, freq, freqDict)
39+
}
40+
41+
return firstRepeated, nil
42+
}
43+
44+
func part1() int {
45+
46+
scanner, _ := util.ReadFile("input.txt")
47+
freq := 0
48+
49+
for scanner.Scan() {
50+
lineFreq, _ := strconv.Atoi(scanner.Text())
51+
52+
freq += lineFreq
53+
}
54+
55+
return freq
56+
}
57+
58+
func part2() int {
59+
freq := 0
60+
freqDict := map[int]bool{0: true}
61+
62+
firstRepeated, _ := scanLines("input.text", freq, freqDict)
63+
64+
return firstRepeated
65+
}
66+
67+
func main() {
68+
fmt.Println("[Sol 1] Frequency: ", part1())
69+
fmt.Println("[Sol 2] First Repeated Frequency: ", part2())
70+
}

Diff for: Day2/part1.go

-57
This file was deleted.

Diff for: Day2/part2.go renamed to Day2/solution.go

+59-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,67 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
6-
"os"
75
"strings"
6+
7+
"../util"
88
)
99

10-
func main() {
11-
file, err := os.Open("input.txt")
12-
if err != nil {
13-
fmt.Println("File reading error", err)
14-
return
10+
func part1() int {
11+
scanner, _ := util.ReadFile("input.txt")
12+
13+
twoWords := 0
14+
threeWords := 0
15+
16+
for scanner.Scan() {
17+
word := scanner.Text()
18+
letterList := strings.Split(word, "")
19+
letterDict := map[string]int{}
20+
21+
counted2 := false
22+
counted3 := false
23+
24+
for _, letter := range letterList {
25+
26+
_, ok := letterDict[letter]
27+
28+
if !ok {
29+
letterDict[letter] = 0
30+
}
31+
32+
letterDict[letter]++
33+
}
34+
35+
for _, frequency := range letterDict {
36+
if frequency == 2 && !counted2 {
37+
twoWords++
38+
counted2 = true
39+
} else if frequency == 3 && !counted3 {
40+
threeWords++
41+
counted3 = true
42+
}
43+
}
1544
}
1645

17-
scanner := bufio.NewScanner(file)
46+
return twoWords * threeWords
47+
48+
}
49+
50+
func checkIds(id1 []string, id2 []string) string {
51+
var commonLetters strings.Builder
52+
53+
for index, letter := range id1 {
54+
if letter == id2[index] {
55+
commonLetters.WriteString(letter)
56+
}
57+
}
58+
59+
return commonLetters.String()
60+
}
61+
62+
func part2() string {
63+
scanner, _ := util.ReadFile("input.txt")
64+
1865
var lines [][]string
1966
var checkLines [][]string
2067
var commonID string
@@ -43,22 +90,11 @@ func main() {
4390
}
4491
}
4592

46-
if err := scanner.Err(); err != nil {
47-
fmt.Println("File scanning error", err)
48-
}
49-
50-
fmt.Println("Box Id: ", commonID)
93+
return commonID
5194

5295
}
5396

54-
func checkIds(id1 []string, id2 []string) string {
55-
var commonLetters strings.Builder
56-
57-
for index, letter := range id1 {
58-
if letter == id2[index] {
59-
commonLetters.WriteString(letter)
60-
}
61-
}
62-
63-
return commonLetters.String()
97+
func main() {
98+
fmt.Println("[Sol 1] Checksum: ", part1())
99+
fmt.Println("[Sol 2] Common ID: ", part2())
64100
}

Diff for: Day3/solution.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
6-
"os"
75
"regexp"
86
"strconv"
7+
8+
"../util"
99
)
1010

1111
type Coords struct {
1212
X, Y int
1313
}
1414

1515
func main() {
16-
file, err := os.Open("input.txt")
17-
if err != nil {
18-
fmt.Println("File reading error", err)
19-
return
20-
}
2116

22-
scanner := bufio.NewScanner(file)
17+
scanner, _ := util.ReadFile("input.txt")
2318
fabric := make([][]int, 1000*1000)
2419

2520
overlappedCells := map[string][]Coords{}

Diff for: util/fs.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package util
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
)
8+
9+
var cachedFiles map[string]*os.File
10+
11+
func ReadFile(path string) (*bufio.Scanner, error) {
12+
13+
_, ok := cachedFiles[path]
14+
15+
var file *os.File
16+
var err error
17+
18+
if ok {
19+
file = cachedFiles[path]
20+
} else {
21+
file, err = os.Open(path)
22+
}
23+
24+
if err != nil {
25+
fmt.Println("File reading error", err)
26+
return nil, err
27+
}
28+
29+
file.Seek(0, 0)
30+
31+
return bufio.NewScanner(file), nil
32+
}

0 commit comments

Comments
 (0)