summaryrefslogtreecommitdiffstats
path: root/2020/day1
diff options
context:
space:
mode:
Diffstat (limited to '2020/day1')
-rw-r--r--2020/day1/README.md13
-rw-r--r--2020/day1/input200
-rw-r--r--2020/day1/sumto.pl28
-rw-r--r--2020/day1/threesum.pl44
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
5Simple 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 @@
1997
21582
31790
41798
51094
61831
71879
81730
91995
101702
111680
121869
131964
141777
151862
161928
171997
181741
191604
201691
211219
221458
231749
241717
251786
261665
271724
281998
291589
301828
311953
321848
331500
341590
351968
361948
371323
381800
391986
40679
411907
421916
431820
441661
451479
461808
471824
481825
491952
501666
511541
521791
531906
541638
551557
561999
571710
581549
591912
601974
611628
621748
631411
641978
651865
661932
671839
681892
691981
701807
71357
72912
731443
741972
751816
761890
771029
781175
791522
801750
812001
821655
831955
841949
851660
86233
871891
881994
891934
901908
911573
921712
931622
941770
951574
961778
971851
982004
991818
1001200
1011229
1021110
1031005
1041716
1051765
1061835
1071773
10815
1091914
1101833
1111689
1121843
1131718
1141872
115390
1161941
1171178
1181670
1191899
1201864
1211913
1222010
1231855
1241797
1251767
1261673
1271657
1281607
1291305
1301341
1311662
1321845
1331980
1341534
1351789
1361876
1371849
1381926
1391958
140977
1411709
1421647
1431832
1441785
1451854
1461667
1471679
1481970
1491186
1502000
1511681
1521684
1531614
1541988
1551561
1561594
1571636
1581327
1591696
1601915
1611045
1621829
1631079
1641295
1651213
1661714
1671992
1681984
1691951
1701687
1711842
1721792
17387
1741732
175428
1761799
1771850
1781962
1791629
1801965
1811142
1821040
183131
1841844
1851454
1861779
1871369
1881960
1891887
1901725
1911893
1921465
1931676
1941826
1951462
1961408
1971937
1981643
1991069
2001759
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 @@
1use strict;
2use warnings;
3use DDP;
4use Smart::Comments;
5
6open my $fh, '<', "input" or die "no input present, $!";
7chomp(my @nums = <$fh>);
8close $fh;
9
10@nums = sort { $a <=> $b } @nums;
11
12my $l_idx = 0;
13my $r_idx = $#nums;
14
15my $total = $nums[$l_idx] + $nums[$r_idx];
16
17while ($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
28print $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 @@
1use strict;
2use warnings;
3use DDP;
4# use Smart::Comments;
5
6open my $fh, '<', "input" or die "no input present, $!";
7chomp(my @nums = <$fh>);
8close $fh;
9
10@nums = sort { $a <=> $b } @nums;
11
12# fix one index, solve two sum problem
13my $fixed_idx = 0;
14my $l_idx = 1;
15my $r_idx = $#nums;
16
17my $total = $nums[$fixed_idx] + $nums[$l_idx] + $nums[$r_idx];
18
19while ($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
44print $nums[$fixed_idx] * $nums[$l_idx] * $nums[$r_idx];