advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 556377afe92d6c618cd6bc911b022fa7306210ef
parent 48b9ea0345fa7ca21e178e4103f07898c57e41d4
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Tue,  1 Dec 2020 18:55:09 -0600

Add 2020 day 01

Diffstat:
M.gitignore | 17+++++++++--------
A2020/day01/input.txt | 200+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2020/day01/main.go | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 261 insertions(+), 8 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -3,10 +3,11 @@
 !.gitignore
 !README.org
 
-!*
-!*/day-*/
-!*/day-*/*.cc
-!*/day-*/*.jl
-!*/day-*/*.rs
-!*/day-*/Makefile
-!*/day-*/input.txt-
\ No newline at end of file
+!*/
+!*/day*/
+!*/day*/*.cc
+!*/day*/*.jl
+!*/day*/*.rs
+!*/day*/*.go
+!*/day*/Makefile
+!*/day*/input.txt+
\ No newline at end of file
diff --git a/2020/day01/input.txt b/2020/day01/input.txt
@@ -0,0 +1,200 @@
+1914
+1931
+1892
+1584
+1546
+1988
+1494
+1709
+1624
+1755
+1849
+1430
+1890
+1675
+1604
+1580
+1500
+1277
+1729
+1456
+2002
+1075
+1512
+895
+1843
+1921
+1904
+1989
+1407
+1552
+1714
+757
+1733
+1459
+1777
+1440
+1649
+1409
+1662
+1968
+1775
+1998
+1754
+1938
+1964
+1415
+1990
+1997
+1870
+1664
+1145
+1782
+1820
+1625
+1599
+1530
+1759
+1575
+1614
+1869
+1620
+1818
+1295
+1667
+1361
+1520
+1555
+1485
+1502
+1983
+1104
+1973
+1433
+1906
+1583
+1562
+1493
+1945
+1528
+1600
+1814
+1712
+1848
+1454
+1801
+1710
+1824
+1426
+1977
+1511
+1644
+1302
+1428
+1513
+1261
+1761
+1680
+1731
+1724
+1970
+907
+600
+1613
+1091
+1571
+1418
+1806
+1542
+1909
+1445
+1344
+1937
+1450
+1865
+1561
+1962
+1637
+1803
+1889
+365
+1810
+1791
+1591
+1532
+1863
+1658
+1808
+1816
+1837
+1764
+1443
+1805
+1616
+1403
+1656
+1661
+1734
+1930
+1120
+1920
+1227
+1618
+1640
+1586
+1982
+1534
+1278
+1269
+1572
+1654
+1472
+1974
+1748
+1425
+1553
+1708
+1394
+1417
+1746
+1745
+1834
+1787
+1298
+1786
+1966
+1768
+1932
+1523
+1356
+1547
+1634
+1951
+1922
+222
+1461
+1628
+1888
+1639
+473
+1568
+1783
+572
+1522
+1934
+1629
+1283
+1550
+1859
+2007
+1996
+1822
+996
+1911
+1689
+1537
+1793
+1762
+1677
+1266
+1715
diff --git a/2020/day01/main.go b/2020/day01/main.go
@@ -0,0 +1,52 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+)
+
+func readInput(filename string) (res []int) {
+	file, _ := os.Open(filename)
+	defer file.Close()
+
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		i, _ := strconv.Atoi(scanner.Text())
+		res = append(res, i)
+	}
+	return
+}
+
+func main() {
+	input := readInput("./input.txt")
+	fmt.Println(part1(input))
+	fmt.Println(part2(input))
+}
+
+func part1(input []int) int {
+	resids := make(map[int]int)
+	for _, v := range input {
+		if resid, ok := resids[2020-v]; ok {
+			return resid * v
+		} else {
+			resids[v] = v
+		}
+	}
+	return 0
+}
+
+func part2(input []int) int {
+	resids := make(map[int]int)
+	for i := 0; i < len(input); i++ {
+		for j := i + 1; j < len(input); j++ {
+			if resid_prod, ok := resids[2020-input[j]]; ok {
+				return resid_prod * input[j]
+			} else {
+				resids[input[i]+input[j]] = input[i] * input[j]
+			}
+		}
+	}
+	return 0
+}