From 0bb7cd7007a2b851a5f3d73a7e435efa27a3ce87 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Tue, 1 Dec 2020 19:40:21 -0500 Subject: [PATCH] day 1 --- 01/input | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 01/main.go | 88 +++++++++++++++++++++++ 2 files changed, 288 insertions(+) create mode 100644 01/input create mode 100644 01/main.go diff --git a/01/input b/01/input new file mode 100644 index 0000000..2c9864e --- /dev/null +++ b/01/input @@ -0,0 +1,200 @@ +1711 +1924 +1384 +1590 +1876 +1918 +2003 +1514 +1608 +1984 +1706 +1375 +1476 +1909 +1615 +1879 +1940 +1945 +1899 +1510 +1657 +1685 +1588 +1884 +1864 +1995 +1648 +1713 +1532 +1556 +1572 +1667 +1861 +1773 +1501 +1564 +1756 +395 +1585 +1717 +1553 +1487 +1617 +1808 +1780 +1570 +1881 +1992 +1894 +1772 +1837 +2002 +1659 +1731 +1873 +1760 +552 +1575 +1597 +1986 +1416 +1398 +1737 +1027 +1457 +198 +1904 +1753 +1727 +633 +1577 +1944 +1369 +1400 +1843 +1966 +1008 +1681 +1890 +1939 +1605 +1548 +1953 +1839 +1409 +1592 +1744 +1761 +1613 +1412 +1759 +703 +1498 +1941 +1425 +1528 +1469 +1728 +1447 +1406 +1797 +1543 +1682 +1722 +1723 +1893 +1644 +796 +1505 +1715 +1729 +1943 +1626 +1602 +1964 +1509 +1816 +1660 +1399 +1996 +1750 +1701 +1963 +1979 +1558 +1506 +1465 +2001 +1935 +1616 +1990 +1946 +1818 +1892 +1431 +1832 +1688 +2004 +1424 +1716 +1897 +1931 +1557 +1389 +1872 +1640 +1670 +1911 +1427 +1730 +211 +1420 +1488 +1689 +1383 +1967 +1594 +642 +1622 +1627 +1607 +1372 +1596 +1451 +1693 +1380 +1745 +1908 +1785 +1646 +1824 +1418 +1258 +1664 +1631 +1459 +1901 +1838 +1794 +1815 +1388 +1809 +1920 +1411 +1593 +1676 +1610 +1629 +1512 +1522 +1649 +1740 +1695 +1504 +1856 +1791 +1898 +1661 +1806 +1851 \ No newline at end of file diff --git a/01/main.go b/01/main.go new file mode 100644 index 0000000..5cda322 --- /dev/null +++ b/01/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +func main() { + partOne() + partTwo() +} + +// [...] they need you to find the two entries that sum to 2020 and then multiply those two numbers together. +// For example, suppose your expense report contained the following: +// 1721 +// 979 +// 366 +// 299 +// 675 +// 1456 +// In this list, the two entries that sum to 2020 are 1721 and 299. +// Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. + +func partOne() { + f, _ := os.Open("input") + reader := bufio.NewReader(f) + scanner := bufio.NewScanner(reader) + + numbers := []int{} + + for scanner.Scan() { + i, _ := strconv.Atoi(scanner.Text()) + numbers = append(numbers, i) + } + + for i := range numbers { + if result := checkTwoSums(numbers[i], numbers[i+1:]); result != 0 { + fmt.Println(result) + return + } + } +} + +func checkTwoSums(i int, rest []int) int { + for _, j := range rest { + if i+j == 2020 { + return i * j + } + } + return 0 +} + +// Can find three numbers in your expense report that meet the same criteria? +// Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. +// Multiplying them together produces the answer, 241861950. + +func partTwo() { + f, _ := os.Open("input") + reader := bufio.NewReader(f) + scanner := bufio.NewScanner(reader) + + numbers := []int{} + + for scanner.Scan() { + i, _ := strconv.Atoi(scanner.Text()) + numbers = append(numbers, i) + } + + for i := range numbers { + for j := range numbers[i+1:] { + if result := checkThreeSums(numbers[i], numbers[j], numbers[j+1:]); result != 0 { + fmt.Println(result) + return + } + } + } +} + +func checkThreeSums(i int, j int, rest []int) int { + for _, k := range rest { + if i+j+k == 2020 { + return i * j * k + } + } + return 0 +}