Skip to content

Instantly share code, notes, and snippets.

@0e4ef622
0e4ef622 / phase.rs
Created December 16, 2019 20:01
day 16 phase calculator
fn next_phase(nums: &[i64], strt: usize) -> Vec<i64> {
let mut pfs = vec![0];
pfs.extend(nums);
for i in 1..pfs.len() {
pfs[i] += pfs[i-1];
}
let mut newl = Vec::with_capacity(nums.len());
for n in strt..nums.len()+strt {
@0e4ef622
0e4ef622 / part2.rs
Last active December 16, 2019 20:55
aoc 2019 day 16 part 2
pub fn part2(input: &str) -> i64 {
// parse input
let onums = input
.trim()
.bytes()
.map(|x| (x - b'0') as i64)
.collect::<Vec<_>>();
let offset = input[..7].parse::<usize>().unwrap();
@0e4ef622
0e4ef622 / solution.rs
Created December 13, 2019 05:53
aoc 2019 day 13
use std::collections::*;
pub fn part1(input: &str) -> impl std::fmt::Display {
let prog = input.trim().split(",").map(|x| x.parse().unwrap()).collect();
let mut vm = ic::Icvm::new(prog);
vm.run();
let mut map = HashMap::new();
let a = vm.drain_outputs().collect::<Vec<_>>();
for ch in a.chunks(3) {
map.insert((ch[0], ch[1]), ch[2]);
}
@0e4ef622
0e4ef622 / solution.rs
Created December 11, 2019 09:12
aoc 2019 day 11
use std::collections::*;
pub fn part1(input: &str) -> impl std::fmt::Display {
let mut botp = (0i128, 0i128);
let mut botd = 0i32; // 0 = up, 1 = right, 2 = down, 3 = left
let mut vm = ic::Icvm::new(input.trim().split(",").map(|x| x.parse::<i128>().unwrap()).collect());
let mut paint = HashMap::new();
while vm.status() != ic::Status::Finished {
vm.run();
@0e4ef622
0e4ef622 / main.rs
Created December 10, 2019 21:10
aoc day 10
use std::io::Read;
mod solution;
// const INPUT: &'static str = include_str!("../in");
fn main() {
let mut input = String::new();
std::io::stdin().read_to_string(&mut input);
let p1 = solution::part1(&input);
println!("part 1: {}", p1);
let p2 = solution::part2(&input);
println!("part 2: {}", p2);
@0e4ef622
0e4ef622 / vm.rs
Created December 9, 2019 18:30
intcode vm
use std::collections::VecDeque;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Status {
Ready,
WaitingForInput,
Finished,
}
#[derive(Clone, Debug)]
[package]
name = "day7"
version = "0.1.0"
authors = ["Matthew Tran <0e4ef622@gmail.com>"]
edition = "2018"
[dev-dependencies]
criterion = "*"
[[bench]]
@0e4ef622
0e4ef622 / expand.rs
Created December 8, 2019 00:33
aoc 2018 d5p1 in rust's type system but macros expanded
// 100,000 isn't actually required unless you're trying to compile with relatively large inputs
// such as the input from AoC
#![recursion_limit = "100000"]
#![allow(dead_code)]
#![allow(non_camel_case_types)]
//! The General Idea
//!
//! Associated typesin traits can be used to make type level functions. A trait of the form shown
//! below can be viewed as a function that takes one type, `Self` and returns a new type, `O`. To
@0e4ef622
0e4ef622 / intcodevm.rs
Created December 7, 2019 09:49
intcode vm from aoc 2019
use std::collections::VecDeque;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Status {
Ready,
WaitingForInput,
Finished,
}
#[derive(Clone, Debug)]
@0e4ef622
0e4ef622 / copypastacode.rs
Created December 7, 2019 05:36
aoc 2019 day 7
pub fn part1(input: &str) -> isize {
let mut max = 0isize;
let mut phases = [0,0,0,0,0];
for a in 0..5 {
for b in 0..5 {
if a==b { continue;}
for c in 0..5 {
if c==a { continue;}
if c==b { continue;}
for d in 0..5 {