-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.go
55 lines (50 loc) · 2.42 KB
/
day1.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
package main
import "fmt"
// Given a range of ints, and a target number,
// find and return the two values that total the target
func findTwoNums(input []int, target int) (int, int) {
for _, num := range input {
for _, num2 := range input {
if num + num2 == target {
return num, num2
}
}
}
return -1, -1
}
func partOne(input []int, target int) {
a1, a2 := findTwoNums(input, target)
fmt.Println("partOne answer is ", a1 * a2)
}
// Given a range of ints, and a target number, find and return the three values
// that total the target
func findThreeNums(input []int, target int) (int, int, int) {
for _, num := range input {
for _, num2 := range input {
// If we're already at or past the target, there is no third value
// that could possibly work, so don't look
if num + num2 >= target {
continue;
}
// Look for a third value that will add to the target
for _, num3 := range input {
if num + num2 + num3 == target {
return num, num2, num3
}
}
}
}
return -1, -1, -1
}
func partTwo(input []int, target int) {
a1, a2, a3 := findThreeNums(input, target)
fmt.Println("partTwo answer is ", a1 * a2 * a3)
}
// Find and multiply two ints which, when added, total 2020
func main() {
// input := []int{1721,979,366,299,675,1456}
input := []int{1975,1600,113,1773,1782,1680,1386,1682,1991,1640,1760,1236,1159,1259,1279,1739,1826,1888,1072,416,1632,1656,1273,1631,1079,1807,1292,1128,1841,1915,1619,1230,1950,1627,1966,774,1425,1983,1616,1633,1559,1925,960,1407,1708,1211,1666,1910,1960,1125,1242,1884,1829,1881,1585,1731,1753,1784,1095,1267,1756,1226,1107,1664,1710,2000,1181,1997,1607,1889,1613,1859,1479,1763,1692,1967,522,1719,1816,1714,1331,1976,1160,1899,1906,1783,1061,2006,1993,1717,2009,1563,1733,1866,1651,1437,1517,1113,1743,1240,1629,1868,1912,1296,1873,1673,1996,1814,1215,1927,1956,1970,1887,1702,1495,1754,1621,1055,1538,1693,1840,1685,1752,1933,1727,1648,1792,1734,1305,1446,1764,1890,1904,1560,1698,1645,1214,1516,1064,1729,1835,1642,1932,1683,962,1081,1943,1502,1622,196,1972,1916,1850,1205,1971,1937,1575,1401,1351,2005,1917,1670,1388,1051,1941,1751,1169,510,217,1948,1120,1635,1636,1511,1691,1589,1410,1902,1572,1871,1423,1114,1806,1282,1193,1974,388,1398,1992,1263,1786,1723,1206,1363,1177,1646,1231,1140,1088,1322}
target := 2020
partOne(input, target)
partTwo(input, target)
}