Advent of Code 2020 (this time in Rust)

Daily notes

use aoc2020::util;
use crate::util;
use super::util;
use super::super::util;
(echo 'ibase=2'; cat inputs/day5.txt | perl -pe 's/B/1/g; s/F/0/g; s/R/1/g; s/L/0/g;' | sort) | bc | pbcopy
struct Instruction {
op: String,
arg: i32,
}
enum Op {
Nop(i32),
Acc(i32),
Jmp(i32),
}
fn is_pair_sum(n: u64, nums: &[u64]) -> bool{
// Why can't I make the lambda look like: |(a, b)| a + b == n?
nums.iter().combinations(2).any(|x| x[0] + x[1] == n)
}
fn is_pair_sum(n: u64, nums: &[u64]) -> bool{
nums.iter().tuple_combinations().any(|(a, b)| a + b == n)
}
is_pair_sum(n, &nums[(i as i32 - preamble_len) as usize..i])
Time: 2961ms
Time: 7ms
let jolts: &[i32];
jolts.iter().map(|x| x);
// primes: [(59, 4), (31, 6), (19, 7), (13, 1), (7, 0)]
n = 2093560 (mod 3162341)
let mask: Op = ...;
let mask: Op::Mask = Op::Mask { ... };
Op::Mask { ones, zeros: _, xs }
$ cargo run --release --bin day15 0,20,7,16,1,18,15 30000000
Compiling aoc2020 v0.1.0 (/Users/danvk/github/aoc2020)
Finished release [optimized] target(s) in 0.52s
Running `target/release/day15 0,20,7,16,1,18,15 30000000`
nums: [0, 20, 7, 16, 1, 18, 15]
last spoken: 129262 after 30000000 rounds (2317 ms)
$ time python3 py/day15.py 0,20,7,16,1,18,15 30000000
After 30000000, last_spoken=129262
python3 py/day15.py 0,20,7,16,1,18,15 30000000 13.51s user 0.18s system 99% cpu 13.721 total
lazy_static! {
static ref DS: Vec<(i32, i32, i32, i32)> = {
let mut v: Vec<(i32, i32, i32, i32)> = vec![];
for dx in -1..=1 {
for dy in -1..=1 {
for dz in -1..=1 {
if dx != 0 || dy != 0 || dz != 0 {
v.push((dx, dy, dz, dw));
}
}
}
}
assert_eq!(26, v.len());
v
};
}
I wish that type weren’t “{unknown}”!
fn match_str<'a>(&self, txt: &'a str, rules: &HashMap<i32, Rule>) -> Option<&'a str> {

--

--

Software Developer @sidewalklabs, author of Effective TypeScript

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store