diff options
Diffstat (limited to '2020/day1')
-rw-r--r-- | 2020/day1/README.md | 13 | ||||
-rw-r--r-- | 2020/day1/input | 200 | ||||
-rw-r--r-- | 2020/day1/sumto.pl | 28 | ||||
-rw-r--r-- | 2020/day1/threesum.pl | 44 |
4 files changed, 285 insertions, 0 deletions
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 @@ | |||
1 | # Day 1 | ||
2 | |||
3 | ## First Part | ||
4 | |||
5 | Simple two sum problem | ||
6 | |||
7 | perl sumto.pl | ||
8 | |||
9 | ## Second part | ||
10 | |||
11 | [Three sum problem](https://www.callicoder.com/three-sum-problem/), which is like two sum problem after fixing the first index | ||
12 | |||
13 | 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 @@ | |||
1 | 997 | ||
2 | 1582 | ||
3 | 1790 | ||
4 | 1798 | ||
5 | 1094 | ||
6 | 1831 | ||
7 | 1879 | ||
8 | 1730 | ||
9 | 1995 | ||
10 | 1702 | ||
11 | 1680 | ||
12 | 1869 | ||
13 | 1964 | ||
14 | 1777 | ||
15 | 1862 | ||
16 | 1928 | ||
17 | 1997 | ||
18 | 1741 | ||
19 | 1604 | ||
20 | 1691 | ||
21 | 1219 | ||
22 | 1458 | ||
23 | 1749 | ||
24 | 1717 | ||
25 | 1786 | ||
26 | 1665 | ||
27 | 1724 | ||
28 | 1998 | ||
29 | 1589 | ||
30 | 1828 | ||
31 | 1953 | ||
32 | 1848 | ||
33 | 1500 | ||
34 | 1590 | ||
35 | 1968 | ||
36 | 1948 | ||
37 | 1323 | ||
38 | 1800 | ||
39 | 1986 | ||
40 | 679 | ||
41 | 1907 | ||
42 | 1916 | ||
43 | 1820 | ||
44 | 1661 | ||
45 | 1479 | ||
46 | 1808 | ||
47 | 1824 | ||
48 | 1825 | ||
49 | 1952 | ||
50 | 1666 | ||
51 | 1541 | ||
52 | 1791 | ||
53 | 1906 | ||
54 | 1638 | ||
55 | 1557 | ||
56 | 1999 | ||
57 | 1710 | ||
58 | 1549 | ||
59 | 1912 | ||
60 | 1974 | ||
61 | 1628 | ||
62 | 1748 | ||
63 | 1411 | ||
64 | 1978 | ||
65 | 1865 | ||
66 | 1932 | ||
67 | 1839 | ||
68 | 1892 | ||
69 | 1981 | ||
70 | 1807 | ||
71 | 357 | ||
72 | 912 | ||
73 | 1443 | ||
74 | 1972 | ||
75 | 1816 | ||
76 | 1890 | ||
77 | 1029 | ||
78 | 1175 | ||
79 | 1522 | ||
80 | 1750 | ||
81 | 2001 | ||
82 | 1655 | ||
83 | 1955 | ||
84 | 1949 | ||
85 | 1660 | ||
86 | 233 | ||
87 | 1891 | ||
88 | 1994 | ||
89 | 1934 | ||
90 | 1908 | ||
91 | 1573 | ||
92 | 1712 | ||
93 | 1622 | ||
94 | 1770 | ||
95 | 1574 | ||
96 | 1778 | ||
97 | 1851 | ||
98 | 2004 | ||
99 | 1818 | ||
100 | 1200 | ||
101 | 1229 | ||
102 | 1110 | ||
103 | 1005 | ||
104 | 1716 | ||
105 | 1765 | ||
106 | 1835 | ||
107 | 1773 | ||
108 | 15 | ||
109 | 1914 | ||
110 | 1833 | ||
111 | 1689 | ||
112 | 1843 | ||
113 | 1718 | ||
114 | 1872 | ||
115 | 390 | ||
116 | 1941 | ||
117 | 1178 | ||
118 | 1670 | ||
119 | 1899 | ||
120 | 1864 | ||
121 | 1913 | ||
122 | 2010 | ||
123 | 1855 | ||
124 | 1797 | ||
125 | 1767 | ||
126 | 1673 | ||
127 | 1657 | ||
128 | 1607 | ||
129 | 1305 | ||
130 | 1341 | ||
131 | 1662 | ||
132 | 1845 | ||
133 | 1980 | ||
134 | 1534 | ||
135 | 1789 | ||
136 | 1876 | ||
137 | 1849 | ||
138 | 1926 | ||
139 | 1958 | ||
140 | 977 | ||
141 | 1709 | ||
142 | 1647 | ||
143 | 1832 | ||
144 | 1785 | ||
145 | 1854 | ||
146 | 1667 | ||
147 | 1679 | ||
148 | 1970 | ||
149 | 1186 | ||
150 | 2000 | ||
151 | 1681 | ||
152 | 1684 | ||
153 | 1614 | ||
154 | 1988 | ||
155 | 1561 | ||
156 | 1594 | ||
157 | 1636 | ||
158 | 1327 | ||
159 | 1696 | ||
160 | 1915 | ||
161 | 1045 | ||
162 | 1829 | ||
163 | 1079 | ||
164 | 1295 | ||
165 | 1213 | ||
166 | 1714 | ||
167 | 1992 | ||
168 | 1984 | ||
169 | 1951 | ||
170 | 1687 | ||
171 | 1842 | ||
172 | 1792 | ||
173 | 87 | ||
174 | 1732 | ||
175 | 428 | ||
176 | 1799 | ||
177 | 1850 | ||
178 | 1962 | ||
179 | 1629 | ||
180 | 1965 | ||
181 | 1142 | ||
182 | 1040 | ||
183 | 131 | ||
184 | 1844 | ||
185 | 1454 | ||
186 | 1779 | ||
187 | 1369 | ||
188 | 1960 | ||
189 | 1887 | ||
190 | 1725 | ||
191 | 1893 | ||
192 | 1465 | ||
193 | 1676 | ||
194 | 1826 | ||
195 | 1462 | ||
196 | 1408 | ||
197 | 1937 | ||
198 | 1643 | ||
199 | 1069 | ||
200 | 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 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use DDP; | ||
4 | use Smart::Comments; | ||
5 | |||
6 | open my $fh, '<', "input" or die "no input present, $!"; | ||
7 | chomp(my @nums = <$fh>); | ||
8 | close $fh; | ||
9 | |||
10 | @nums = sort { $a <=> $b } @nums; | ||
11 | |||
12 | my $l_idx = 0; | ||
13 | my $r_idx = $#nums; | ||
14 | |||
15 | my $total = $nums[$l_idx] + $nums[$r_idx]; | ||
16 | |||
17 | while ($total != 2020) { | ||
18 | |||
19 | if ($total < 2020) { | ||
20 | $l_idx++; # total too low, increase | ||
21 | } else { | ||
22 | $r_idx--; # total too high, decrease | ||
23 | } | ||
24 | |||
25 | $total = $nums[$l_idx] + $nums[$r_idx]; | ||
26 | } | ||
27 | |||
28 | 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 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use DDP; | ||
4 | # use Smart::Comments; | ||
5 | |||
6 | open my $fh, '<', "input" or die "no input present, $!"; | ||
7 | chomp(my @nums = <$fh>); | ||
8 | close $fh; | ||
9 | |||
10 | @nums = sort { $a <=> $b } @nums; | ||
11 | |||
12 | # fix one index, solve two sum problem | ||
13 | my $fixed_idx = 0; | ||
14 | my $l_idx = 1; | ||
15 | my $r_idx = $#nums; | ||
16 | |||
17 | my $total = $nums[$fixed_idx] + $nums[$l_idx] + $nums[$r_idx]; | ||
18 | |||
19 | while ($total != 2020) { | ||
20 | |||
21 | if ($total < 2020) { | ||
22 | $l_idx++; # total too low, increase | ||
23 | } else { | ||
24 | $r_idx--; # total too high, decrease | ||
25 | } | ||
26 | |||
27 | # fixed index might not be correct | ||
28 | if ($l_idx > $r_idx) { | ||
29 | $fixed_idx++; | ||
30 | $l_idx = $fixed_idx + 1; | ||
31 | $r_idx = $#nums; | ||
32 | } | ||
33 | |||
34 | $total = $nums[$fixed_idx] + $nums[$l_idx] + $nums[$r_idx]; | ||
35 | ### $total | ||
36 | |||
37 | ### $fixed_idx | ||
38 | ### $l_idx | ||
39 | ### $r_idx | ||
40 | |||
41 | # print("fixed: $nums[$fixed_idx]\nleft: $nums[$l_idx]\nright: $nums[$r_idx]\n"); | ||
42 | } | ||
43 | |||
44 | print $nums[$fixed_idx] * $nums[$l_idx] * $nums[$r_idx]; | ||