#My AOC solutions :3

1 messages · Page 1 of 1 (latest)

forest surge
#

This is the main.rs btw
||

use std::{collections::HashMap, io::Write, process::ExitCode};

mod one;
mod two;
mod three;
...

fn main() -> ExitCode {
    let stdin = std::io::stdin();
    let mut challenge = String::with_capacity(5);

    print!("Challenge >> ");
    std::io::stdout().flush().unwrap();

    let readresult = stdin.read_line(&mut challenge);

    match readresult {
        Ok(_) => (),
        Err(err) => {
            println!("Userinput Error: {}", err);
            return ExitCode::FAILURE;
        }
    }

    challenge.pop();

    let mut input: String;

    let inputresult = std::fs::read_to_string(std::format!("./inputs/{}.txt", challenge.chars().nth(0).unwrap()));
    match inputresult {
        Ok(ok) => {
            input = ok;
        }
        Err(err) => {
            println!("File Error: {}", err);
            return ExitCode::FAILURE;
        }
    }

    let mut functions = HashMap::<&str, Box<dyn Fn(& mut String) -> String>>::with_capacity(50);
    
    functions.insert("1.1", Box::new(one::first));
    functions.insert("1.2", Box::new(one::second));
    functions.insert("2.1", Box::new(two::first));
    functions.insert("2.2", Box::new(two::second));
    functions.insert("3.1", Box::new(three::first));
    functions.insert("3.2", Box::new(three::second));
    ...





    let functionoption = functions.get(&challenge.as_str());
    let function; 
    match functionoption {
        Some(func) =>{
            function = func;
        }
        None => {
            println!("Lookup Error: {} not found", input);
            return ExitCode::FAILURE;
        }
    }

    let result = function(&mut input);

    println!("{}", result);


    return ExitCode::SUCCESS;
}

||

#

Day 1:
||

use std::collections::HashMap;

fn get_cols(input: &String) -> (Vec<u32>, Vec<u32>){
    let mut col1: Vec<u32> = Vec::new();
    let mut col2: Vec<u32> = Vec::new();

    for row in input.lines(){
        let splitted = row.split_once("   ").unwrap();
        col1.push(splitted.0.parse::<u32>().unwrap());
        col2.push(splitted.1.parse::<u32>().unwrap());
    }

    (col1, col2)
}


pub fn first(input: &mut String) -> String{
    let columns = get_cols(&input);

    let mut col1 = columns.0;
    let mut col2 = columns.1;

    col1.sort();
    col2.sort();

    let mut differences: Vec<u32> = Vec::new();
    
    for pair in col1.iter().zip(&col2){
        differences.push(pair.0.abs_diff(*pair.1));
    }

    let sum: u64 = differences.iter().sum::<u32>() as u64;

    format!("{}", sum)
}

pub fn second(input: &mut String) -> String{
    let columns = get_cols(&input);

    let col1 = columns.0;
    let col2 = columns.1;

    let mut occurances = HashMap::<u32, u64>::new();

    let mut sum = 0u64;

    for num in col1{
        match occurances.get(&num) {
            Some(x) => {
                sum += x;
            },
            None => {
                let x = col2.iter().fold(0u64, |acc, entry| {
                    if *entry == num{
                        return acc + 1;
                    }
                    acc
                }) * (num as u64);
                sum += x;
                occurances.insert(num, x);
            }
        }
    }
    
    

    format!("{}", sum)
}

||

#

ig this chat is probably gonna be full of black boxes

keen pivot
#
left = []
right = []
with open('aoc2024-1.dat') as f:
    for x in f:
        A, B = x.split()
        left.append(A)
        right.append(B)

print(sum(map(lambda a, b: abs(int(a) - int(b)), sorted(left), sorted(right))))
keen pivot
#

was thinking of doing it in pascal... but that would take longer

somber stump
#
fn main() {
    let list = include_str!("input.txt")
        .lines()
        .map(|x| {
            x.split("   ")
                .map(|x| x.parse::<usize>().unwrap())
                .collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();

    let mut list1 = list.iter().map(|x| x[0]).collect::<Vec<_>>();
    list1.sort();

    let mut list2 = list.iter().map(|x| x[1]).collect::<Vec<_>>();
    list2.sort();

    println!(
        "Part 1: {}",
        list1
            .iter()
            .enumerate()
            .map(|(idx, y)| list2[idx].abs_diff(*y))
            .sum::<usize>()
    );

    println!(
        "Part 2: {}",
        list1
            .iter()
            .map(|y| *y * list2.iter().filter(|a| **a == *y).count())
            .sum::<usize>()
    );
}
#

this is my d1

keen pivot
#

python is just stupid easy for this stuff

#

I did it in scheme one year... nearly as easy

south helm
#

we're posting aoc solutions here ig :^) ```scm
(use-modules (ice-9 textual-ports) (ice-9 pretty-print) (srfi srfi-1) (ice-9 receive) (srfi srfi-26))

(define (read-input)
(let ([line (get-line (current-input-port))])
(if (eof-object? line)
'()
(cons (filter identity (map string->number (string-split line #\space)))
(read-input)))))

(define (abs- a b) (abs (- a b)))

(receive (left right)
(unzip2 (read-input))
(pretty-print (fold + 0 (map abs- (sort left <) (sort right <))))
(pretty-print (fold + 0 (map (λ (v) (* v (count (cut = v <>) right))) left))))

somber stump
#

wtf

molten aurora
#

quick and dirty 3a

somber stump
#

funny color theme

molten aurora
#

someone should do this with regex

molten aurora
#

even worse 3b

vital obsidian
molten aurora
somber stump
#

:3

modern ferry