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/'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('0');
            return;
        }
        char bank[512];
        bank[511] = '\0';
        char *ptr = bank + 511;
        while (n != 0) {
            *(--ptr) = (n % 10) + '0';
            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