Day 1

Back to index | Next: Day 02

Day 1 was pretty simple, and it’s also the day I’ve had the most solutions sent in for. Given I don’t really have a way to group these anons by pseudonym, I may as well chuck all the unsorted solutions in here.

The first rustanon
Rust code
use std::collections::HashSet;
    use std::fs::File;
    use std::io::{BufRead, BufReader};
    
    fn main() {
        println!("Day1!");
    
        println!("Input");
        parts("input", &2020);
    
        println!("BIG input");
        parts("input\_big", &99920044);
    }
    
    fn parts(input: &str, sum: &i64) {
        let expenses = File::open(input)
            .map(BufReader::new)
            .map(|br| br.lines().map(|l| l.unwrap().parse::<i64>().unwrap()))
            .unwrap()
            .collect::<HashSet<i64>>();
    
        // part 1
        let (p1v1, p1v2) = find2\_n(sum, sum, &expenses).unwrap();
        println!("P1: {} \* {} = {}", p1v1, p1v2, p1v1 \* p1v2);
    
        // part 2
        let (p2v1, p2v2, p2v3) = expenses
            .iter()
            .find\_map(|v1| find2\_n(&(sum - v1), v1, &expenses).map(|(v2, v3)| (\*v1, v2, v3)))
            .unwrap();
        let result = p2v1 as i128 \* p2v2 as i128 \* p2v3 as i128;
        println!("P2: {} \* {} \* {} = {}", p2v1, p2v2, p2v3, result);
    }
    
    fn find2\_n(n: &i64, exclude: &i64, expenses: &HashSet<i64>) -> Option<(i64, i64)> {
        return expenses
            .iter()
            .filter(|&v| v < n && v != exclude)
            .find\_map(|v1| {
                expenses
                    .get(&(n - v1))
                    .filter(|&v2| v1 != v2 && v2 != exclude)
                    .map(|v2| (\*v1, \*v2))
            });
    }
    
And the second!
This seems more terse/elegant than the first guy’s
pub fn solve(input: &str) -> (u32, u32) {
       let mut input: Vec<u32> = input.lines().map(|s| s.parse().unwrap()).collect();
       input.sort\_unstable();
    
       let solve\_a = |target, i| {
           input
               .iter()
               .skip(i)
               .copied()
               .take\_while(|&a| a < target)
               .enumerate()
               .find(|&(j, a)| input[i + j + 1..].binary\_search(&(target - a)).is\_ok())
               .map(|(\_, a)| a \* (target - a))
       };
    
       (
           solve\_a(2020, 0).unwrap(),
           input
               .iter()
               .copied()
               .enumerate()
               .filter\_map(|(i, a)| solve\_a(2020 - a, i + 1).map(|b| a \* b))
               .next()
               .unwrap()
       )
    }
    
A brave JS webdev who dared to venture into the thread!
Did you need that many newlines, though?
//kyle nodejs
    
    const i = [
        1780,
    1693,
    1830,
    1756,
    1858,
    1868,
    1968,
    1809,
    1996,
    1962,
    1800,
    1974,
    1805,
    1795,
    170,
    1684,
    1659,
    1713,
    1848,
    1749,
    1717,
    1734,
    956,
    1782,
    1834,
    1785,
    1786,
    1994,
    1652,
    1669,
    1812,
    1954,
    1984,
    1665,
    1987,
    1562,
    2004,
    2010,
    1551,
    961,
    1854,
    2005,
    1883,
    1965,
    475,
    1776,
    1791,
    262,
    1912,
    1227,
    1486,
    1989,
    1857,
    825,
    1683,
    1991,
    1875,
    1982,
    1654,
    1767,
    1673,
    1973,
    1886,
    1731,
    1745,
    1770,
    1995,
    1721,
    1662,
    1679,
    1783,
    1999,
    1889,
    1746,
    1902,
    2003,
    1698,
    1794,
    1798,
    1951,
    1953,
    2007,
    1899,
    1658,
    1705,
    62,
    1819,
    1708,
    1666,
    2006,
    1763,
    1732,
    1613,
    1841,
    1747,
    1489,
    1845,
    2008,
    1885,
    2002,
    1735,
    1656,
    1771,
    1950,
    1704,
    1737,
    1748,
    1759,
    1802,
    2000,
    1955,
    1738,
    1761,
    1765,
    1853,
    1900,
    1709,
    1979,
    1911,
    1775,
    1813,
    1949,
    1966,
    1774,
    1977,
    1757,
    1992,
    2009,
    1956,
    1840,
    1988,
    1985,
    1993,
    1718,
    1976,
    1078,
    1997,
    1897,
    1792,
    1790,
    1801,
    1871,
    1727,
    1700,
    1485,
    942,
    1686,
    1859,
    1676,
    802,
    1952,
    1998,
    1961,
    1844,
    1808,
    1703,
    1980,
    1766,
    1963,
    1849,
    1670,
    1716,
    1957,
    1660,
    1816,
    1762,
    1829,
    526,
    359,
    2001,
    1874,
    1778,
    1873,
    1511,
    1810,
    1699,
    1970,
    1690,
    1978,
    1892,
    1691,
    1781,
    1777,
    1975,
    1967,
    1694,
    1969,
    1959,
    1910,
    1826,
    1672,
    1655,
    1839,
    1986,
    1872,
    1983,
    1981,
    1972,
    1772,
    1760
    ];
    var l = [];
    var m = [];
    var l2 = [];
    
    i.forEach(e1 => {
        if (e1 > 1010) m.push(e1);
        else l.push(e1);
    
        i.forEach(e2 => {
           if (e1+e2<2020 && e1 != e2) l2.push([e1,e2]) 
        });
    });
    
    l.forEach(e1 => {
        m.forEach(e2 => {
            if (e1+e2==2020) console.log(e1\*e2);
        });
    });  
    
    l2.forEach(e1 => {
        i.forEach(e2 => {
            if(e1[0]+e1[1]+e2 == 2020 && e1[0]!=e2 && e1[1]!=e2) console.log (e1[0]\*e1[1]\*e2)
        });
    });
    

And that’s it for the non-pseudonymous entries for day 1. You can also check individual anons’ pages linked at the bottom of the main page, like the unkown cnile or yours truly, the archivist.

Below is an auto-generated list of EVERY entry with ‘day 1’ in the title, which will contain the above, as well. ###AUTO-ACQUIRED DATA FOLLOWS…
archivist/day01-1.c
archivist/day01-1.c
\#include "../array.h"
    \#include "../fileload.h"
    
    int main (int argc, char\*\*argv){
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char \* input = file\_contents(argv[1]);
        char\*dispos = input;
        array data = arr\_init(0, sizeof(int));
    
        char \* line = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char \* val = strsep(&line, " ");
            int temp = atoi(val);
            arr\_add(&data, &temp);
        };
        for (int j = 0;j<data.size;j++){
            printf("val: %d\n", arr\_get(int, data, j));
        }
        
        for (size\_t i = 0;i<data.size;i++){
            for (size\_t j = 0;j<data.size;j++){
                if (arr\_get(int,data,i) + arr\_get(int,data,j)== 2020){
                    printf("Found!  The multiple is: %d\n", arr\_get(int,data,i) \* arr\_get(int,data,j) );
                }
            }
        }
    
        return 0;
    }
    
archivist/day01-2.c
archivist/day01-2.c
\#include "../array.h"
    \#include "../fileload.h"
    
    int main (int argc, char\*\*argv){
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char \* input = file\_contents(argv[1]);
        char\*dispos = input;
        array data = arr\_init(0, sizeof(int));
    
        char \* line = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char \* val = strsep(&line, " ");
            int temp = atoi(val);
            arr\_add(&data, &temp);
        };
        for (int j = 0;j<data.size;j++){
            //printf("val: %d\n", arr\_get(int, data, j));
        }
        
        for (size\_t i = 0;i<data.size;i++){
            for (size\_t j = 0;j<data.size;j++){
                for (size\_t k = 0;k<data.size;k++){
                    if (arr\_get(int,data,i) + arr\_get(int,data,j) + arr\_get(int,data,k)== 2020){
                        printf("Found!  The multiple is: %d\n",  arr\_get(int,data,i) 
                                                                \*arr\_get(int,data,j)
                                                                \*arr\_get(int,data,k));
                    }
                }
            }
        }
    
        return 0;
    }
    
archivist/day01-3.c
archivist/day01-3.c
\#include "../array.h"
    \#include "../fileload.h"
    \#include <stdlib.h>
    
    //For /g/&\#39;s bigboy dataset.  Target sum is 99920044
    
    int MAX\_SIZE = 100000;
    
    int compare(const void\* a, const void\* b){
        return (\*(int \*)a < \*(int \*)b) ? -1 : 1;
        return 0;
    }
    
    
    int search\_up(int\*vals, int left, int right, int max){
        int here;
        while (right-left > 1){
            here = (left+right)/2;
            if(vals[here] == max) return here;
            if(vals[here] > max) right = here;
            if(vals[here] < max) left = here;
        }
        if(vals[right] > max){
            return left;
        }
            return right;
    }
    
    int search\_down(int\*vals, int left, int right, int min){
        int here;
        while (right-left > 1){
            here = (left+right)/2;
            if(vals[here] == min) return here;
            if(vals[here] > min) right = here;
            if(vals[here] < min) left = here;
        }
        if(vals[left] < min){
            return right;
        }
            return left;
    }
    
    int main (int argc, char\*\*argv){
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char \* input = file\_contents(argv[1]);
    
        int size = 0;
        const int target = 99920044;
        int\*arr = malloc(sizeof(int)\*MAX\_SIZE);
        char \* line = 0;
        while((line = strsep(&input, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char \* val = strsep(&line, " ");
            arr[size++] = atoi(val);
            if (size > MAX\_SIZE) {
                printf("Too many inputs\n");
                exit(-1);}
        };
        //int (\*func)(const void \*, const void \*, void \*) = &compare;
        qsort(&arr[0], size, sizeof(int), compare);
        for(int i = 0; i < size; i++){
            //printf("%d\n", arr[i]);
        }
        //a<b<c
        int true\_left = 0;
        int true\_right= size-1;
        //first pass
        while(true\_right>true\_left+2){
            int any=1;
            while(any){
                any = 0;
                int new\_true\_right = search\_up(arr,true\_left+2,true\_right,target-arr[true\_left]-arr[true\_left+1]);
                if(new\_true\_right<true\_right){
                    true\_right = new\_true\_right;
                    any++;
                }
                int new\_true\_left = search\_down(arr,true\_left,true\_right-2,target-arr[true\_right]-arr[true\_right-1]);
                if(new\_true\_left>true\_left){
                    true\_left=new\_true\_left;
                    any++;
                }
                //if(any)printf("true left, true right %d, %d\n", true\_left, true\_right);
            }
    
    
            int left=true\_left;
            int right = true\_right-1;
            int starget = target - arr[true\_right];
            any = 1;
            while(any){
                any = 0;
                int new\_right = search\_up(arr,left+1,right,starget-arr[left]);
                if(new\_right<right){
                    right = new\_right;
                    any++;
                }
                int new\_left = search\_down(arr,left,right-1,starget-arr[right]);
                if(new\_left>left){
                    left=new\_left;
                    any++;
                }
                //printf("%d, %d\n", left, right);
            }
            //printf("starting iteration\n");
            while(left != right ){
                int tempright = search\_up(arr, left+1, right,starget-arr[left]);
                if(arr[left]+arr[tempright] == starget){
                    printf("FOUND IT!  the nums are %d, %d, %d\n", arr[left], arr[right], arr[true\_right]);
                    printf("Sum is: %d, compared with %d\n", arr[left]+ arr[right]+ arr[true\_right], target);
                    return 0;
                }
                left++;
            }
            true\_right--;   
        }
    
        
    
        return 0;
    }
    
day-1/AoC-2020-day01.1.R
day-1/AoC-2020-day01.1.R
\#AdventofCode 2020 puzzle 1
    setwd("E:/Desktop/")
    df=read.table("E:/desktop/input.txt")
    a=0
    for (i in row(df)) {
        for (j in row(df)) {
          if ( (df$V1[i]+df$V1[j])==2020 ){ 
            print(df$V1[i])
            print(df$V1[j])
            print(i)
            print(j)
            a=a+1}
          if (a >= 1) break
        }
      if (a >= 1) break
    }
    print(df$V1[i]\*df$V1[j])
    
day-1/AoC-2020-day01.2.R
day-1/AoC-2020-day01.2.R
\#AdventofCode 2020 puzzle 1
    df=read.table("E:/desktop/input.txt")
    a=0
    for (i in row(df)) {
      for (j in row(df)) {
        for (l in row(df)) {
          if ( (df$V1[i]+df$V1[j]+df$V1[l])==2020 ){ 
            print(df$V1[i])
            print(df$V1[j])
            print(df$V1[l])
            print(i)
            print(j)
            print(l)
            a=a+1
            if (a >= 1) break}
          if (a >= 1) break
        }
        if (a >= 1) break
      }
      if (a >= 1) break
    }
    print(df$V1[i]\*df$V1[j]\*df$V1[l])
    
day-1/day01-1.rs
day-1/day01-1.rs
use std::collections::HashSet;
    use std::fs::File;
    use std::io::{BufRead, BufReader};
    
    fn main() {
        println!("Day1!");
    
        println!("Input");
        parts("input", &2020);
    
        println!("BIG input");
        parts("input\_big", &99920044);
    }
    
    fn parts(input: &str, sum: &i64) {
        let expenses = File::open(input)
            .map(BufReader::new)
            .map(|br| br.lines().map(|l| l.unwrap().parse::<i64>().unwrap()))
            .unwrap()
            .collect::<HashSet<i64>>();
    
        // part 1
        let (p1v1, p1v2) = find2\_n(sum, sum, &expenses).unwrap();
        println!("P1: {} \* {} = {}", p1v1, p1v2, p1v1 \* p1v2);
    
        // part 2
        let (p2v1, p2v2, p2v3) = expenses
            .iter()
            .find\_map(|v1| find2\_n(&(sum - v1), v1, &expenses).map(|(v2, v3)| (\*v1, v2, v3)))
            .unwrap();
        let result = p2v1 as i128 \* p2v2 as i128 \* p2v3 as i128;
        println!("P2: {} \* {} \* {} = {}", p2v1, p2v2, p2v3, result);
    }
    
    fn find2\_n(n: &i64, exclude: &i64, expenses: &HashSet<i64>) -> Option<(i64, i64)> {
        return expenses
            .iter()
            .filter(|&v| v < n && v != exclude)
            .find\_map(|v1| {
                expenses
                    .get(&(n - v1))
                    .filter(|&v2| v1 != v2 && v2 != exclude)
                    .map(|v2| (\*v1, \*v2))
            });
    }
    
day-1/day01-2.rs
day-1/day01-2.rs
pub fn solve(input: &str) -> (u32, u32) {
      let mut input: Vec<u32> = input.lines().map(|s| s.parse().unwrap()).collect();
      input.sort\_unstable();
    
      let solve\_a = |target, i| {
          input
              .iter()
              .skip(i)
              .copied()
              .take\_while(|&a| a < target)
              .enumerate()
              .find(|&(j, a)| input[i + j + 1..].binary\_search(&(target - a)).is\_ok())
              .map(|(\_, a)| a \* (target - a))
      };
    
      (
          solve\_a(2020, 0).unwrap(),
          input
              .iter()
              .copied()
              .enumerate()
              .filter\_map(|(i, a)| solve\_a(2020 - a, i + 1).map(|b| a \* b))
              .next()
              .unwrap()
      )
    }
    
day-1/day01.js
day-1/day01.js
//kyle nodejs
    
    const i = [
        1780,
    1693,
    1830,
    1756,
    1858,
    1868,
    1968,
    1809,
    1996,
    1962,
    1800,
    1974,
    1805,
    1795,
    170,
    1684,
    1659,
    1713,
    1848,
    1749,
    1717,
    1734,
    956,
    1782,
    1834,
    1785,
    1786,
    1994,
    1652,
    1669,
    1812,
    1954,
    1984,
    1665,
    1987,
    1562,
    2004,
    2010,
    1551,
    961,
    1854,
    2005,
    1883,
    1965,
    475,
    1776,
    1791,
    262,
    1912,
    1227,
    1486,
    1989,
    1857,
    825,
    1683,
    1991,
    1875,
    1982,
    1654,
    1767,
    1673,
    1973,
    1886,
    1731,
    1745,
    1770,
    1995,
    1721,
    1662,
    1679,
    1783,
    1999,
    1889,
    1746,
    1902,
    2003,
    1698,
    1794,
    1798,
    1951,
    1953,
    2007,
    1899,
    1658,
    1705,
    62,
    1819,
    1708,
    1666,
    2006,
    1763,
    1732,
    1613,
    1841,
    1747,
    1489,
    1845,
    2008,
    1885,
    2002,
    1735,
    1656,
    1771,
    1950,
    1704,
    1737,
    1748,
    1759,
    1802,
    2000,
    1955,
    1738,
    1761,
    1765,
    1853,
    1900,
    1709,
    1979,
    1911,
    1775,
    1813,
    1949,
    1966,
    1774,
    1977,
    1757,
    1992,
    2009,
    1956,
    1840,
    1988,
    1985,
    1993,
    1718,
    1976,
    1078,
    1997,
    1897,
    1792,
    1790,
    1801,
    1871,
    1727,
    1700,
    1485,
    942,
    1686,
    1859,
    1676,
    802,
    1952,
    1998,
    1961,
    1844,
    1808,
    1703,
    1980,
    1766,
    1963,
    1849,
    1670,
    1716,
    1957,
    1660,
    1816,
    1762,
    1829,
    526,
    359,
    2001,
    1874,
    1778,
    1873,
    1511,
    1810,
    1699,
    1970,
    1690,
    1978,
    1892,
    1691,
    1781,
    1777,
    1975,
    1967,
    1694,
    1969,
    1959,
    1910,
    1826,
    1672,
    1655,
    1839,
    1986,
    1872,
    1983,
    1981,
    1972,
    1772,
    1760
    ];
    var l = [];
    var m = [];
    var l2 = [];
    
    i.forEach(e1 => {
        if (e1 > 1010) m.push(e1);
        else l.push(e1);
    
        i.forEach(e2 => {
           if (e1+e2<2020 && e1 != e2) l2.push([e1,e2]) 
        });
    });
    
    l.forEach(e1 => {
        m.forEach(e2 => {
            if (e1+e2==2020) console.log(e1\*e2);
        });
    });  
    
    l2.forEach(e1 => {
        i.forEach(e2 => {
            if(e1[0]+e1[1]+e2 == 2020 && e1[0]!=e2 && e1[1]!=e2) console.log (e1[0]\*e1[1]\*e2)
        });
    });
    
steveklabnik/steveklabnik-day01.rs
steveklabnik/steveklabnik-day01.rs
fn solve(input: &str) -> (u32, u32) {
      let mut input: Vec<u32> = input.lines().map(|s| s.parse().unwrap()).collect();
      input.sort\_unstable();
    
      let solve\_a = |target, i| {
          input
              .iter()
              .skip(i)
              .copied()
              .take\_while(|&a| a < target)
              .enumerate()
              .find(|&(j, a)| input[i + j + 1..].binary\_search(&(target - a)).is\_ok())
              .map(|(\_, a)| a \* (target - a))
      };
    
      (
          solve\_a(2020, 0).unwrap(),
          input
              .iter()
              .copied()
              .enumerate()
              .filter\_map(|(i, a)| solve\_a(2020 - a, i + 1).map(|b| a \* b))
              .next()
              .unwrap()
      )
    }
    
unknown-cnile/ucnile-day01.c
unknown-cnile/ucnile-day01.c
\#include <stdlib.h>
    \#include <stdio.h>
    
    typedef \_\_int128 num\_t;
    
    void die(char \*str) {
        fprintf(stderr, "[ERROR] %s\n", str);
        exit(-1);
    }
    
    void swap(num\_t \*a, num\_t \*b) {
        num\_t tmp = \*a;
        \*a = \*b;
        \*b = tmp;
    }
    
    // accending order
    void my\_qsort(num\_t \*a, size\_t len) {
        switch (len) {
            case 2:
                if (a[0] > a[1]) swap(a, a+1);
            case 0:
            case 1:
                return;
            default:;
                num\_t mid = a[0];
                size\_t nxt = 1;
                for (size\_t i = 1; i < len; i++) {
                    if (a[i] <= mid) {
                        swap(a+nxt, a+i);
                        nxt++;
                    }
                }
                swap(a, a+nxt-1);
                my\_qsort(a, nxt-1);
                my\_qsort(a+nxt, len-nxt);
        }
    }
    
    int check\_for(num\_t \*a, size\_t len, num\_t val) {
        if (len == 0) return 0;
        else if (a[len/2] == val) return 1;
        else if (a[len/2] < val) return check\_for(a + len/2 + 1, (len-1)/2, val);
        else return check\_for(a, len/2, val);
    }
    
    \#define TARGET 99920044L
    
    \#define MAX\_DATA 1024L \* 1024L
    
    num\_t data\_store[MAX\_DATA];
    size\_t data\_pos = 0;
    
    void part\_one() {
        my\_qsort(data\_store, data\_pos);
        for (size\_t i = 0; i < data\_pos; i++) {
            num\_t r = TARGET - data\_store[i];
            if (check\_for(data\_store+i, data\_pos-i, r)) {
                printf("P1: %lld\n", (long long) (data\_store[i] \* r));
            }
        }
    }
    
    void print\_num(num\_t n) {
        if (n == 0) {
            putchar(&\#39;0&\#39;);
            return;
        }
        char bank[512];
        bank[511] = &\#39;\0&\#39;;
        char \*ptr = bank + 511;
        while (n != 0) {
            \*(--ptr) = (n % 10) + &\#39;0&\#39;;
            n /= 10;
        }
        printf("%s", ptr);
    }
    
    void part\_two() {
        my\_qsort(data\_store, data\_pos);
        for (size\_t i = 0; (i < data\_pos) && (data\_store[i] <= (TARGET/3)); i++) {
            num\_t r = TARGET - data\_store[i];
            for (size\_t j = i + 1; (j < data\_pos) && (data\_store[j] <= (r/2)); j++) {
                num\_t rr = r - data\_store[j];
                if (check\_for(data\_store+j+1, data\_pos-j-1, rr)) {
                    printf("P2: ");
                    print\_num(data\_store[i] \* data\_store[j] \* rr);
                    printf("\n");
                }
            }
        }
    }
    
    int main(int argc, char \*\*argv) {
        // read input
        FILE \*fd = fopen("bb.txt", "r");
        if (fd == NULL) die("failed to read");
        while (1) {
            if (data\_pos == MAX\_DATA) die("too many numbers");
            long long n;
            switch (fscanf(fd, "%lld", &n)) {
                case EOF:
                    if (ferror(fd)) die("failed to read");
                    goto done;
                case 0:
                    die("failed to match");
            }
            data\_store[data\_pos] = (num\_t) n;
            data\_pos++;
        }
        done:
        // part 2
        part\_two();
        return 0;
    }
    

Tags: log