Compare commits
4 Commits
14d8b98100
...
8dde43f7ca
| Author | SHA1 | Date | |
|---|---|---|---|
| 8dde43f7ca | |||
| 258f012633 | |||
| 3ecc3de9bf | |||
| 4727c2b822 |
3
2020/day01/go.mod
Normal file
3
2020/day01/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module 2020/day01
|
||||
|
||||
go 1.25.4
|
||||
200
2020/day01/input.txt
Normal file
200
2020/day01/input.txt
Normal file
@@ -0,0 +1,200 @@
|
||||
2004
|
||||
1671
|
||||
1678
|
||||
1304
|
||||
1242
|
||||
1882
|
||||
1605
|
||||
1034
|
||||
1883
|
||||
1589
|
||||
1881
|
||||
1546
|
||||
1713
|
||||
1218
|
||||
1982
|
||||
1395
|
||||
1277
|
||||
1417
|
||||
1497
|
||||
1499
|
||||
1847
|
||||
1989
|
||||
1172
|
||||
1684
|
||||
1243
|
||||
1843
|
||||
1661
|
||||
1662
|
||||
1421
|
||||
1790
|
||||
1344
|
||||
1458
|
||||
1074
|
||||
1809
|
||||
1990
|
||||
1369
|
||||
1386
|
||||
1736
|
||||
1972
|
||||
1634
|
||||
1229
|
||||
1123
|
||||
1870
|
||||
1595
|
||||
1934
|
||||
1399
|
||||
1732
|
||||
1545
|
||||
1208
|
||||
368
|
||||
1907
|
||||
1143
|
||||
443
|
||||
1929
|
||||
1965
|
||||
1872
|
||||
1738
|
||||
1967
|
||||
997
|
||||
1473
|
||||
1041
|
||||
1991
|
||||
1868
|
||||
1180
|
||||
1409
|
||||
1379
|
||||
1568
|
||||
1163
|
||||
1869
|
||||
1391
|
||||
1956
|
||||
1249
|
||||
1505
|
||||
351
|
||||
2001
|
||||
462
|
||||
1219
|
||||
1731
|
||||
1802
|
||||
1798
|
||||
1626
|
||||
1438
|
||||
1099
|
||||
1477
|
||||
1980
|
||||
1708
|
||||
1666
|
||||
1066
|
||||
1121
|
||||
1359
|
||||
1426
|
||||
1734
|
||||
1768
|
||||
1836
|
||||
1453
|
||||
923
|
||||
1660
|
||||
1878
|
||||
1522
|
||||
1024
|
||||
1429
|
||||
232
|
||||
1952
|
||||
1730
|
||||
1763
|
||||
1981
|
||||
1388
|
||||
1337
|
||||
1317
|
||||
1922
|
||||
1044
|
||||
1999
|
||||
1341
|
||||
1178
|
||||
1524
|
||||
1185
|
||||
1257
|
||||
1256
|
||||
1061
|
||||
1262
|
||||
1022
|
||||
1778
|
||||
1917
|
||||
1205
|
||||
1272
|
||||
1842
|
||||
1533
|
||||
1194
|
||||
1746
|
||||
1691
|
||||
1617
|
||||
1667
|
||||
1940
|
||||
1171
|
||||
1792
|
||||
1773
|
||||
1411
|
||||
1902
|
||||
1859
|
||||
1978
|
||||
1764
|
||||
1482
|
||||
1276
|
||||
735
|
||||
1716
|
||||
1915
|
||||
1675
|
||||
1126
|
||||
1830
|
||||
1227
|
||||
1299
|
||||
1535
|
||||
1700
|
||||
1658
|
||||
1771
|
||||
1823
|
||||
1055
|
||||
1602
|
||||
1590
|
||||
1983
|
||||
1885
|
||||
1735
|
||||
103
|
||||
1766
|
||||
14
|
||||
1486
|
||||
1939
|
||||
1525
|
||||
1916
|
||||
1279
|
||||
544
|
||||
1406
|
||||
1674
|
||||
1948
|
||||
1971
|
||||
1651
|
||||
1715
|
||||
1943
|
||||
1784
|
||||
2008
|
||||
1800
|
||||
1720
|
||||
1557
|
||||
1467
|
||||
1371
|
||||
1637
|
||||
1345
|
||||
1924
|
||||
1565
|
||||
1976
|
||||
1827
|
||||
1890
|
||||
1848
|
||||
1465
|
||||
1573
|
||||
1231
|
||||
1310
|
||||
1754
|
||||
1569
|
||||
1532
|
||||
58
2020/day01/main.go
Normal file
58
2020/day01/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseInput(file string) []int {
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read input file: %v", err)
|
||||
}
|
||||
lines := strings.Fields(string(content))
|
||||
var data []int
|
||||
for _, line := range lines {
|
||||
num, err := strconv.Atoi(line)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to convert string to int: %v", err)
|
||||
}
|
||||
data = append(data, num)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func PartOne(data []int) int {
|
||||
seen := make(map[int]bool)
|
||||
for _, number := range data {
|
||||
complement := 2020 - number
|
||||
if seen[complement] {
|
||||
return number * complement
|
||||
}
|
||||
seen[number] = true
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func PartTwo(data []int) int {
|
||||
seen := make(map[int]bool)
|
||||
for idx := range data {
|
||||
for _, number := range data[idx+1:] {
|
||||
complement := 2020 - data[idx] - number
|
||||
if seen[complement] {
|
||||
return complement * data[idx] * number
|
||||
}
|
||||
}
|
||||
seen[data[idx]] = true
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := parseInput("input.txt")
|
||||
fmt.Println("Part 1:", PartOne(data))
|
||||
fmt.Println("Part 2:", PartTwo(data))
|
||||
}
|
||||
21
2020/day01/main_test.go
Normal file
21
2020/day01/main_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
input := []int{1721, 979, 366, 299, 675, 1456}
|
||||
expected := 514579
|
||||
got := PartOne(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne(%v) = %d, want %d", input, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
input := []int{1721, 979, 366, 299, 675, 1456}
|
||||
expected := 241861950
|
||||
got := PartTwo(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo(%v) = %d, want %d", input, got, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user