From 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 01:24:32 +0300 Subject: 2020, tracking --- 2020/day1/README.md | 13 ++++ 2020/day1/input | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2020/day1/sumto.pl | 28 +++++++ 2020/day1/threesum.pl | 44 +++++++++++ 4 files changed, 285 insertions(+) create mode 100644 2020/day1/README.md create mode 100644 2020/day1/input create mode 100644 2020/day1/sumto.pl create mode 100644 2020/day1/threesum.pl (limited to '2020/day1') diff --git a/2020/day1/README.md b/2020/day1/README.md new file mode 100644 index 0000000..f8ae070 --- /dev/null +++ b/2020/day1/README.md @@ -0,0 +1,13 @@ +# Day 1 + +## First Part + +Simple two sum problem + + perl sumto.pl + +## Second part + +[Three sum problem](https://www.callicoder.com/three-sum-problem/), which is like two sum problem after fixing the first index + + perl threesum.pl diff --git a/2020/day1/input b/2020/day1/input new file mode 100644 index 0000000..b221b17 --- /dev/null +++ b/2020/day1/input @@ -0,0 +1,200 @@ +997 +1582 +1790 +1798 +1094 +1831 +1879 +1730 +1995 +1702 +1680 +1869 +1964 +1777 +1862 +1928 +1997 +1741 +1604 +1691 +1219 +1458 +1749 +1717 +1786 +1665 +1724 +1998 +1589 +1828 +1953 +1848 +1500 +1590 +1968 +1948 +1323 +1800 +1986 +679 +1907 +1916 +1820 +1661 +1479 +1808 +1824 +1825 +1952 +1666 +1541 +1791 +1906 +1638 +1557 +1999 +1710 +1549 +1912 +1974 +1628 +1748 +1411 +1978 +1865 +1932 +1839 +1892 +1981 +1807 +357 +912 +1443 +1972 +1816 +1890 +1029 +1175 +1522 +1750 +2001 +1655 +1955 +1949 +1660 +233 +1891 +1994 +1934 +1908 +1573 +1712 +1622 +1770 +1574 +1778 +1851 +2004 +1818 +1200 +1229 +1110 +1005 +1716 +1765 +1835 +1773 +15 +1914 +1833 +1689 +1843 +1718 +1872 +390 +1941 +1178 +1670 +1899 +1864 +1913 +2010 +1855 +1797 +1767 +1673 +1657 +1607 +1305 +1341 +1662 +1845 +1980 +1534 +1789 +1876 +1849 +1926 +1958 +977 +1709 +1647 +1832 +1785 +1854 +1667 +1679 +1970 +1186 +2000 +1681 +1684 +1614 +1988 +1561 +1594 +1636 +1327 +1696 +1915 +1045 +1829 +1079 +1295 +1213 +1714 +1992 +1984 +1951 +1687 +1842 +1792 +87 +1732 +428 +1799 +1850 +1962 +1629 +1965 +1142 +1040 +131 +1844 +1454 +1779 +1369 +1960 +1887 +1725 +1893 +1465 +1676 +1826 +1462 +1408 +1937 +1643 +1069 +1759 diff --git a/2020/day1/sumto.pl b/2020/day1/sumto.pl new file mode 100644 index 0000000..931f5e2 --- /dev/null +++ b/2020/day1/sumto.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use DDP; +use Smart::Comments; + +open my $fh, '<', "input" or die "no input present, $!"; +chomp(my @nums = <$fh>); +close $fh; + +@nums = sort { $a <=> $b } @nums; + +my $l_idx = 0; +my $r_idx = $#nums; + +my $total = $nums[$l_idx] + $nums[$r_idx]; + +while ($total != 2020) { + + if ($total < 2020) { + $l_idx++; # total too low, increase + } else { + $r_idx--; # total too high, decrease + } + + $total = $nums[$l_idx] + $nums[$r_idx]; +} + +print $nums[$l_idx] * $nums[$r_idx]; diff --git a/2020/day1/threesum.pl b/2020/day1/threesum.pl new file mode 100644 index 0000000..8f2083a --- /dev/null +++ b/2020/day1/threesum.pl @@ -0,0 +1,44 @@ +use strict; +use warnings; +use DDP; +# use Smart::Comments; + +open my $fh, '<', "input" or die "no input present, $!"; +chomp(my @nums = <$fh>); +close $fh; + +@nums = sort { $a <=> $b } @nums; + +# fix one index, solve two sum problem +my $fixed_idx = 0; +my $l_idx = 1; +my $r_idx = $#nums; + +my $total = $nums[$fixed_idx] + $nums[$l_idx] + $nums[$r_idx]; + +while ($total != 2020) { + + if ($total < 2020) { + $l_idx++; # total too low, increase + } else { + $r_idx--; # total too high, decrease + } + + # fixed index might not be correct + if ($l_idx > $r_idx) { + $fixed_idx++; + $l_idx = $fixed_idx + 1; + $r_idx = $#nums; + } + + $total = $nums[$fixed_idx] + $nums[$l_idx] + $nums[$r_idx]; + ### $total + + ### $fixed_idx + ### $l_idx + ### $r_idx + + # print("fixed: $nums[$fixed_idx]\nleft: $nums[$l_idx]\nright: $nums[$r_idx]\n"); +} + +print $nums[$fixed_idx] * $nums[$l_idx] * $nums[$r_idx]; -- cgit v1.2.3-70-g09d2