23
submitted 11 months ago* (last edited 11 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 6: Wait for It


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] bugsmith@programming.dev 2 points 11 months ago* (last edited 11 months ago)

Today's problems felt really refreshing after yesterday.

Solution in Rust ๐Ÿฆ€

View formatted code on GitLab

Code

use std::{
    collections::HashSet,
    env, fs,
    io::{self, BufRead, BufReader, Read},
};

fn main() -> io::Result<()> {
    let args: Vec = env::args().collect();
    let filename = &args[1];
    let file1 = fs::File::open(filename)?;
    let file2 = fs::File::open(filename)?;
    let reader1 = BufReader::new(file1);
    let reader2 = BufReader::new(file2);

    println!("Part one: {}", process_part_one(reader1));
    println!("Part two: {}", process_part_two(reader2));
    Ok(())
}

fn parse_data(reader: BufReader) -> Vec> {
    let lines = reader.lines().flatten();
    let data: Vec<_> = lines
        .map(|line| {
            line.split(':')
                .last()
                .expect("text after colon")
                .split_whitespace()
                .map(|s| s.parse::().expect("numbers"))
                .collect::>()
        })
        .collect();
    data
}

fn calculate_ways_to_win(time: u64, dist: u64) -> HashSet {
    let mut wins = HashSet
:new(); for t in 1..time { let d = t * (time - t); if d > dist { wins.insert(t); } } wins } fn process_part_one(reader: BufReader) -> u64 { let data = parse_data(reader); let results: Vec<_> = data[0].iter().zip(data[1].iter()).collect(); let mut win_method_qty: Vec = Vec::new(); for r in results { win_method_qty.push(calculate_ways_to_win(*r.0, *r.1).len() as u64); } win_method_qty.iter().product() } fn process_part_two(reader: BufReader) -> u64 { let data = parse_data(reader); let joined_data: Vec<_> = data .iter() .map(|v| { v.iter() .map(|d| d.to_string()) .collect::>() .join("") .parse::() .expect("all digits") }) .collect(); calculate_ways_to_win(joined_data[0], joined_data[1]).len() as u64 } #[cfg(test)] mod tests { use super::*; const INPUT: &str = "Time: 7 15 30 Distance: 9 40 200"; #[test] fn test_process_part_one() { let input_bytes = INPUT.as_bytes(); assert_eq!(288, process_part_one(BufReader::new(input_bytes))); } #[test] fn test_process_part_two() { let input_bytes = INPUT.as_bytes(); assert_eq!(71503, process_part_two(BufReader::new(input_bytes))); } }

:::

this post was submitted on 06 Dec 2023
23 points (96.0% liked)

Advent Of Code

736 readers
2 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS