Skip to content

Instantly share code, notes, and snippets.

@jacobhyphenated
jacobhyphenated / Day.kt
Last active December 2, 2022 14:46
Advent of Code 2022 Day2
// I'll probably stop sharing my interface file soon.
// Added some changes for reading the input file
interface Day<T> {
fun getInput(): T
fun part1(input: T): Number
fun part2(input: T): Number
fun run() {
val input = getInput()
var start = System.nanoTime()
@jacobhyphenated
jacobhyphenated / day1.kt
Created December 1, 2022 16:31
Advent of Code 2022 Day 1
// Use this "Day" Interface to facilitate running the code. This will be re-used for each day's problem.
interface Day<T> {
fun getInput(): T
fun part1(input: T): Number
fun part2(input: T): Number
fun run() {
val input = getInput()
var start = System.currentTimeMillis()
println("Part 1: ${part1(input)} (${System.currentTimeMillis() - start}ms)")
use std::collections::HashMap;
use std::fs;
// The struct mostly exists because I wanted to build a graph with edges.
// But I had to abandon that approach due to being bad at Rust.
// https://github.com/nrc/r4cppp/blob/master/graphs/README.md
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct Cave {
name: String,
is_large: bool,
use std::collections::HashSet;
use std::cmp;
use std::fs;
// Part 1 - a lot of logic is reused for parts 1 and 2
// go one step at a time, counting the number of flashes each step
pub fn flash_after_steps(octopi: &Vec<Vec<i32>>, steps: i32) -> i32 {
let mut octopi = octopi.clone();
let mut flashes = 0;
for _ in 0..steps {
use std::collections::HashMap;
use std::fs;
// Part 1 & Part 2
// Both parts ended up being so similar, that I combined both into one method
// Returns a tuple - first value is part 1, second is part 2
// Keeps a LIFO stack of the next closing character.
// Each time an open character is encountered, the corresponding close character is added to the stack.
// if the next character is a closing character but not the next value on the stack
// then this is an illegal line - score appropriately
use std::cmp;
use std::collections::HashSet;
// Part 1 - used a lot of helper methods to share code between parts
// Find the low points, add 1, then sum the values
pub fn count_low_points(grid: &Vec<Vec<i32>>) -> i32 {
find_low_points(grid).iter()
.map(|&(r,c)| grid[r][c] + 1)
.sum()
}
use std::fs;
use std::collections::HashSet;
use std::collections::HashMap;
#[derive(Debug)]
pub struct SevenSegmentData {
training: Vec<String>,
output: Vec<String>
}
use std::cmp;
use std::fs;
fn calc_gas(subs: &Vec<i32>, position: i32) -> i32 {
subs.iter().fold(0, |acc, sub| acc + (sub - position).abs())
}
// 1+2+3+4..n == (n * (n+1)) / 2
fn calc_gas_exp(subs: &Vec<i32>, position: i32) -> i32 {
subs.iter().fold(0, |acc, sub| {
use std::fs;
use std::collections::HashMap;
/**
* Part 1: Brute force (~350ms)
* loop one day at a time, updating the counters for each fish
* and add new fish when required.
*/
pub fn calc_growth(fish: &Vec<i32>, days: usize) -> usize {
let mut fish = fish.clone();
use std::collections::HashMap;
use std::cmp;
use std::fs;
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Point {
x: i32,
y: i32
}