GithubHelp home page GithubHelp logo

axllind / adventofcode2022 Goto Github PK

View Code? Open in Web Editor NEW
54.0 3.0 4.0 573 KB

Solutions to all 25 Advent of Code 2022 in Rust πŸ¦€ Less than 100 lines per day, total runtime of less than 1 second.

Home Page: https://adventofcode.com/2022

License: MIT License

Rust 98.33% Shell 1.67%
advent-of-code advent-of-code-2022 advent-of-code-2022-rust advent-of-code-rust aoc aoc2022 rust

adventofcode2022's Introduction

Hi, I'm Axel! ✌️ I am

  • MSc graduate of Computer Science from KTH, with a masters in Computer Security
  • a software engineer focusing on security
  • a big fan of Rust πŸ¦€

Advent of Code πŸŽ„

AdventOfCode is the world's largest programming competition, taking place every year during December. πŸŽ„ Last year over 200k people participated. I am one of less than 400 people who have completed every single year, earning 350/350 ⭐

  • 2023, in Rust. Here we go!
  • 2022, in Rust. 25th place in the world on day 19!
  • 2021, in Rust with competitive global leaderboard placings, with efficient and clean solutions.
  • 2020, in Rust with high global leaderboard placings.
  • 2019, fast solutions to all 25 problems in Rust.
  • 2018, solutions to all 25 problems in type-annotated Python 3.10.
  • 2017, solutions to all 25 problems in Haskell.
  • 2016, solutions to all 25 problems in OCaml.
  • 2015, solutions to all 25 problems in Clojure.

Some things I've done

  • libwebb, a http webserver framework written from scratch in C! πŸ•ΈοΈ
  • tauri-pw-manager, a desktop password manager written in Rust/Typescript with Tauri! πŸ–₯️
  • cryptopals-rs, solutions to the famous cryptography challenges in Rust! πŸ”“
  • AxOS, a work-in-progress operating system written in Rust πŸ¦€
  • wasm-sandbox-demo, an exploration into the security aspects of WebAssembly through tic-tac-toe πŸ•΅οΈβ€β™€οΈ
  • mdtable-cli, a cli tool which makes creating markdown tables much easier πŸ“Š
  • easy_io, a Rust library which makes IO in competetive programming a breeze πŸƒβ€β™€οΈ
  • synacor_challenge, a solution to the Synacor challenge with a detailed write-up of the process 🍻
  • Number-Recognition, a three-layer artificial neural network written from scratch in C++ πŸ€–
  • Sense, a React app Chrome-extension which displays useful information on your new-tab page ⏱
  • simple-slack-verification, a npm package for Slack signed secret authentication πŸ”
  • lc3-rs, a Little Computer 3 virtual machine πŸ’»
  • Strive, a Doodle-jump style mobile game made in Unity and C# πŸ“±

adventofcode2022's People

Contributors

axllind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

adventofcode2022's Issues

How Does Day 16 Work?

I've been struggling to complete day 16, my part 1 runs but part 2 is just too slow, and I came across this repo while looking for ways to optimize my solution (thanks for making this repository public, BTW)! I've been looking over your solutions trying to understand them. It's mostly clear and concise, very well written, but I don't quite understand how your solution to Day 16 works. Link to Day 16 in case you need a refresher.

I'll copy and paste your solution below with comments that I've added which explain my thoughts

use hashbrown::HashMap;
use itertools::Itertools;

#[aoc::main(16)]
fn main(input: &str) -> (u16, u16) {
  // vec of valve tuples in descending order of flow
  let valves = input.lines()
    .map(|l| {
      let mut words = l.split(|c: char| !c.is_uppercase() && !c.is_ascii_digit()).filter(|w| !w.is_empty()).skip(1);
      let valve = words.next().unwrap();
      let flow = words.next().unwrap().parse::<u16>().unwrap();
      let tunnels = words.collect::<Vec<_>>();
      (valve, flow, tunnels)
    })
    .sorted_by_key(|(_, flow, _)| -(*flow as i32))
    .collect::<Vec<_>>();
  // map of labels to their index in the valves, flow, and adj vectors
  let labels = valves.iter().enumerate().map(|(i, v)| (v.0, i)).collect::<HashMap<_, _>>();
  let flow = valves.iter().map(|(_,flow,_)| *flow).collect::<Vec<_>>();
  // vec of available valves from each valve
  let adj = valves.iter().map(|(_, _, tunnels)| tunnels.iter().map(|t| labels[t]).collect::<Vec<_>>()).collect::<Vec<_>>();
  // index marking ending of all "interesting" valves
  let m = valves.iter().position(|(_, flow, _)| *flow == 0).unwrap();
  // 2^(interesting_valves.len()), number of possible configurations of "interesting" valves ??
  let mm = 1 << m;

  // opt[time][node][available valves]
  let mut opt = vec![vec![vec![0; mm]; valves.len()]; 30];
  for t in 1..30 { // iterate over time...
    for i in 0..valves.len() { // iterate over all valves
      let ii = 1 << i; // 2^i
      for x in 0..mm { // iterate over all possible valve configurations
        let mut tmp = opt[t][i][x]; // should always be 0
        if ii & x != 0 && t >= 2 { // if this valve should be "on" for this particular configuration
          // I don't understand what x - ii is, or why it's flow[i] * t... isn't that backwards if we are progressing forwards in time?
          // how does this take into account the "cost" to get to a new location?
          tmp = tmp.max(opt[t-1][i][x - ii] + flow[i] * t as u16);
        }
        // optimal for this iteration is max(this location, adjacent locations...)
        opt[t][i][x] = tmp.max(adj[i].iter().map(|&j| opt[t-1][j][x]).max().unwrap());
      }
    }
  }

  let start = labels["AA"];
  let p1 = opt[29][start][mm - 1]; // opt[t of interest][starting position][??]
  // ??
  let p2 = (0..mm/2).map(|x| opt[25][start][x] + opt[25][start][mm-1-x]).max().unwrap();
  (p1, p2)
}

I would really appreciate it if you'd take the time to explain this to me. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.