This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt; | |
use std::fs; | |
#[derive(Clone)] | |
pub struct Tile { | |
number: i32, | |
called: bool | |
} | |
impl Tile { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::cmp; | |
use std::fs; | |
#[derive(Debug, PartialEq, Eq, Hash)] | |
pub struct Point { | |
x: i32, | |
y: i32 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
use std::collections::HashSet; | |
use std::collections::HashMap; | |
#[derive(Debug)] | |
pub struct SevenSegmentData { | |
training: Vec<String>, | |
output: Vec<String> | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)") |
OlderNewer