Skip to content

Instantly share code, notes, and snippets.

@SleeplessByte
Last active December 5, 2020 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SleeplessByte/bbbc0803f2f97c8655ad0f49266e9e92 to your computer and use it in GitHub Desktop.
Save SleeplessByte/bbbc0803f2f97c8655ad0f49266e9e92 to your computer and use it in GitHub Desktop.
Advent of Code 2020: Day 1 - Report Repair
expenses = File.readlines('input.txt').map(&:to_i)
def find_matching_entries(entries, summation = 2020)
entries = entries.dup
while entries.size > 0
current = entries.shift
match = entries.find do |compare|
current + compare == summation
end
break [current, match] if match
end
end
# This matches 3 numbers. Comment line 19, 21 and 22, and remove 2020 - expense
# to get the result for 2 numbers.
entries = expenses.each do |expense|
entries = find_matching_entries(expenses, 2020 - expense)
break [expense, *entries] if entries
end
puts entries.inject(:*)
1511
1344
1925
1970
1864
1951
1557
1984
1743
1526
1972
1945
1969
1760
2008
1592
736
1963
1994
2009
1777
1856
1899
1926
1850
687
2005
1094
1949
1326
2002
1805
1493
1341
1828
1778
1767
1364
1973
1768
1929
1377
2000
1726
1913
2001
1574
1859
1793
1957
1959
1388
1593
1392
724
1962
1999
252
1982
1662
1892
1610
1343
1831
1862
1991
1394
1946
1935
1986
1911
1358
1322
1956
1988
1758
1490
1998
1744
1844
1294
1764
1543
1560
1562
1747
1870
1292
1989
1752
1471
1980
1897
1544
1914
1923
1944
1375
1987
1993
1742
1975
1479
1977
1934
1939
1950
1992
1983
1474
1643
2010
1814
1942
322
1425
1646
1878
1410
1927
1761
1948
1779
1753
1847
274
1659
1773
1960
1772
1674
1809
1568
1978
1952
1947
1976
1953
1961
1937
1932
1781
2007
1941
1393
1573
1745
169
89
1408
1974
1810
1979
1967
890
1958
1930
1954
1759
720
1936
1576
1407
2004
1964
1462
1875
1943
1938
2006
1739
1378
1922
1924
2003
1792
1985
1729
1966
1355
1940
1928
1357
1955
1896
1115
1836
1971
1329
1807
1997
1359
1801
1933
1965
1981
1711
1905
1625
1968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment