-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd03.go
121 lines (103 loc) · 2.4 KB
/
d03.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bufio"
"log"
"os"
"sort"
"strconv"
"strings"
)
var (
impossibleTriangles = 0
impossibleTriples = 0
)
func processTriangles(line string) {
s := strings.Fields(line)
var numbers = []int{}
for _, i := range s {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
numbers = append(numbers, j)
}
sort.Ints(numbers)
if (numbers[0] + numbers[1]) > numbers[2] {
impossibleTriangles = impossibleTriangles + 1
}
}
func d03() int {
log.Printf("Day 3\n")
file, err := os.Open("input/d03.txt")
if err != nil {
log.Fatalf("Failed to read input file %v \n", err)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
textLine := scanner.Text()
processTriangles(textLine)
}
log.Printf("Number of Impossible Triangles %v\n", impossibleTriangles)
return impossibleTriangles
}
func processTriples(sc *bufio.Scanner) {
lineOne := sc.Text()
sc.Scan()
lineTwo := sc.Text()
sc.Scan()
lineThree := sc.Text()
ones := strings.Fields(lineOne)
twos := strings.Fields(lineTwo)
threes := strings.Fields(lineThree)
var numones = []int{}
var numtwos = []int{}
var numthrees = []int{}
for index := 0; index < 3; index++ {
o, _ := strconv.Atoi(ones[index])
t, _ := strconv.Atoi(twos[index])
th, _ := strconv.Atoi(threes[index])
switch index {
case 0:
numones = append(numones, o)
numones = append(numones, t)
numones = append(numones, th)
case 1:
numtwos = append(numtwos, o)
numtwos = append(numtwos, t)
numtwos = append(numtwos, th)
case 2:
numthrees = append(numthrees, o)
numthrees = append(numthrees, t)
numthrees = append(numthrees, th)
}
}
sort.Ints(numones)
sort.Ints(numtwos)
sort.Ints(numthrees)
if (numones[0] + numones[1]) > numones[2] {
impossibleTriples = impossibleTriples + 1
}
if (numtwos[0] + numtwos[1]) > numtwos[2] {
impossibleTriples = impossibleTriples + 1
}
if (numthrees[0] + numthrees[1]) > numthrees[2] {
impossibleTriples = impossibleTriples + 1
}
}
func d03Part2() int {
log.Printf("Day3 Part 2\n")
file, err := os.Open("input/d03.txt")
if err != nil {
log.Fatalf("Failed to read input file %v \n", err)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
processTriples(scanner)
}
log.Printf("Number of Impossible Triples %v\n", impossibleTriples)
return impossibleTriples
}