From cd94bf4a8fc508b62a16e522291bfba1efdc2b5c Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 1 Dec 2020 13:53:24 -0600 Subject: [PATCH] 2020 day 1 Complete --- 2020/day01/input | 200 +++++++++++++++++++++++++++++++++++++++++++ 2020/day01/main.go | 41 +++++++++ 2020/day01/testinput | 6 ++ helpers/helpers.go | 9 ++ 4 files changed, 256 insertions(+) create mode 100644 2020/day01/input create mode 100644 2020/day01/main.go create mode 100644 2020/day01/testinput diff --git a/2020/day01/input b/2020/day01/input new file mode 100644 index 0000000..f260c56 --- /dev/null +++ b/2020/day01/input @@ -0,0 +1,200 @@ +1632 +1438 +1811 +1943 +1883 +1698 +1976 +1972 +1794 +1726 +1850 +1789 +1524 +1701 +1454 +1594 +1655 +1018 +1828 +1867 +1959 +1541 +1596 +1998 +1916 +1894 +1727 +1812 +1800 +1897 +1534 +1712 +1825 +1629 +1827 +81 +1855 +1621 +1694 +1663 +1793 +1685 +1616 +1899 +1688 +1652 +1719 +1589 +1649 +1742 +1905 +922 +1695 +1747 +1989 +1968 +1678 +1709 +1938 +1920 +1429 +1556 +2005 +1728 +1484 +1746 +1702 +1456 +1917 +1670 +1433 +1538 +1806 +1667 +1505 +963 +1478 +2003 +1955 +1689 +1490 +1523 +1615 +1784 +1624 +583 +1465 +1443 +1489 +1873 +1485 +1773 +1704 +352 +505 +1705 +1844 +1599 +1778 +1846 +1533 +1535 +1965 +1987 +828 +1755 +1823 +1639 +1981 +1763 +1758 +1819 +1569 +1580 +358 +1786 +1964 +1604 +1805 +1822 +1941 +1993 +1939 +1975 +1966 +1852 +1310 +1687 +1718 +641 +1715 +1995 +1603 +1444 +1641 +1961 +1536 +1771 +1267 +1749 +1944 +1519 +1445 +1818 +1558 +1922 +1452 +1901 +1915 +1957 +1840 +1785 +1946 +1683 +1918 +1847 +1690 +1716 +1627 +1571 +1985 +1455 +435 +1856 +1527 +1660 +1555 +1557 +1591 +1906 +1646 +1656 +1620 +1618 +1598 +1606 +1808 +1509 +1551 +1723 +1835 +1610 +1820 +1942 +1767 +1549 +1607 +1781 +1612 +1864 +2007 +1908 +1650 +1449 +1886 +1878 +1895 +1869 +1469 +1507 diff --git a/2020/day01/main.go b/2020/day01/main.go new file mode 100644 index 0000000..b2d7252 --- /dev/null +++ b/2020/day01/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToIntSlice() + fmt.Println("# Part 1") + part1(inp) + fmt.Println("# Part 2") + part2(inp) +} + +func part1(input []int) { + for i := 0; i < len(input); i++ { + for j := i + 1; j < len(input); j++ { + if input[i]+input[j] == 2020 { + fmt.Printf("Entries %d and %d sum to 2020\n", i, j) + fmt.Printf("Answer: %d\n", input[i]*input[j]) + return + } + } + } +} + +func part2(input []int) { + for i := 0; i < len(input); i++ { + for j := i + 1; j < len(input); j++ { + for k := j + 1; k < len(input); k++ { + if input[i]+input[j]+input[k] == 2020 { + fmt.Printf("Entries %d, %d, and %d sum to 2020\n", i, j, k) + fmt.Printf("Answer: %d\n", input[i]*input[j]*input[k]) + return + } + } + } + } +} diff --git a/2020/day01/testinput b/2020/day01/testinput new file mode 100644 index 0000000..e3fb011 --- /dev/null +++ b/2020/day01/testinput @@ -0,0 +1,6 @@ +1721 +979 +366 +299 +675 +1456 diff --git a/helpers/helpers.go b/helpers/helpers.go index 9a7432a..8981d02 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -76,6 +76,15 @@ func GetArgNumber(i int) string { return "" } +func StdinToIntSlice() []int { + var ret []int + st := StdinToStringSlice() + for _, v := range st { + ret = append(ret, Atoi(v)) + } + return ret +} + func StdinToStringSlice() []string { var input []string scanner := bufio.NewScanner(os.Stdin)