readying 2022 to merge

This commit is contained in:
Dylan Thies
2023-12-12 08:58:38 -05:00
parent 6eaaa4630e
commit 3b75d20516
64 changed files with 0 additions and 0 deletions

204
2022/Cargo.lock generated Normal file
View File

@@ -0,0 +1,204 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "2022.0.0"
dependencies = [
"itertools",
]
[[package]]
name = "day10"
version = "2022.0.0"
[[package]]
name = "day11"
version = "2022.0.0"
dependencies = [
"derive-getters",
"itertools",
"log",
"nom",
]
[[package]]
name = "day12"
version = "0.1.0"
dependencies = [
"itertools",
"log",
]
[[package]]
name = "day13"
version = "2022.0.0"
dependencies = [
"log",
"nom",
]
[[package]]
name = "day14"
version = "2022.0.0"
dependencies = [
"nom",
]
[[package]]
name = "day15"
version = "2022.0.0"
dependencies = [
"itertools",
"nom",
]
[[package]]
name = "day16"
version = "2022.0.0"
dependencies = [
"itertools",
"nom",
]
[[package]]
name = "day17"
version = "2022.0.0"
dependencies = [
"itertools",
"nom",
]
[[package]]
name = "day2"
version = "2022.0.0"
[[package]]
name = "day3"
version = "2022.0.0"
[[package]]
name = "day4"
version = "2022.0.0"
dependencies = [
"nom",
]
[[package]]
name = "day5"
version = "2022.0.0"
dependencies = [
"nom",
]
[[package]]
name = "day6"
version = "2022.0.0"
dependencies = [
"itertools",
]
[[package]]
name = "day7"
version = "2022.0.0"
[[package]]
name = "day8"
version = "2022.0.0"
dependencies = [
"itertools",
]
[[package]]
name = "day9"
version = "2022.0.0"
[[package]]
name = "derive-getters"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2c35ab6e03642397cdda1dd58abbc05d418aef8e36297f336d5aba060fe8df"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memchr"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76fc44e2588d5b436dbc3c6cf62aef290f90dab6235744a93dfe1cc18f451e2c"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "proc-macro2"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"

34
2022/Cargo.toml Normal file
View File

@@ -0,0 +1,34 @@
[workspace]
members = [
"day1",
"day2",
"day3",
"day4",
"day5",
"day6",
"day7",
"day8",
"day9",
"day10",
"day11",
"day12",
"day13",
"day14",
"day15",
"day16",
"day17",
]
resolver = "2"
[workspace.package]
version = "2022.0.0"
edition = "2021"
authors = ["Dylan Thies"]
repository = "https://github.com/smellyfis/AOC-2022.git"
[workspace.dependencies]
derive-getters = "0.3.0"
itertools = "0.11.0"
log = "0.4.20"
nom = "7.1.3"

7
2022/day1/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "0.1.0"

11
2022/day1/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "day1"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools.workspace = true

2240
2022/day1/input Normal file

File diff suppressed because it is too large Load Diff

77
2022/day1/src/main.rs Normal file
View File

@@ -0,0 +1,77 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
use itertools::Itertools;
fn part1(input: &[u64]) -> String {
input.iter().max().unwrap().to_string()
}
fn part2(input: &[u64]) -> String {
input
.iter()
//order the elves since we don't care about position anymore
.sorted_by(|a, b| b.cmp(a))
.take(3)
.copied()
.sum::<u64>()
.to_string()
}
fn parse_input(input: &str) -> Vec<u64> {
input.lines().fold(vec![0_u64], |mut acc, line| {
//empty lines mean new elf
if line.is_empty() {
acc.push(0_u64);
} else {
// the first time through is an edge case preventing an else here
let last = acc.last_mut().unwrap();
*last += line.parse::<u64>().unwrap();
}
acc
})
}
fn main() -> std::io::Result<()> {
let file = fs::read_to_string("input")?;
let elves = parse_input(&file);
//part 1 is get the max
println!("Part 1: {}", part1(&elves));
//Part 2 is get the sum of the largest 3
println!("Part 2: {}", part2(&elves));
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "2000
3000
4000
5000
6000
7000
8000
9000
10000";
#[test]
fn part1_works() {
let input = parse_input(INPUT);
assert_eq!(part1(&input), "24000")
}
#[test]
fn part2_works() {
let input = parse_input(INPUT);
assert_eq!(part2(&input), "45000")
}
}

7
2022/day10/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day10"
version = "0.1.0"

10
2022/day10/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "day10"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

138
2022/day10/input Normal file
View File

@@ -0,0 +1,138 @@
addx 1
noop
addx 4
noop
noop
addx 7
noop
noop
noop
addx 3
noop
noop
addx 5
addx -1
addx 1
addx 5
addx 3
noop
addx 3
noop
addx -1
noop
addx 3
addx 5
addx -38
addx 7
addx 10
addx -14
addx 5
addx 30
addx -25
noop
addx 2
addx 3
addx -2
addx 2
addx 5
addx 2
addx 2
addx -21
addx 22
addx 5
addx 2
addx 3
noop
addx -39
addx 1
noop
noop
addx 3
addx 5
addx 4
addx -5
addx 4
addx 4
noop
addx -9
addx 12
addx 5
addx 2
addx -1
addx 6
addx -2
noop
addx 3
addx 3
addx 2
addx -37
addx 39
addx -33
addx -1
addx 1
addx 8
noop
noop
noop
addx 2
addx 20
addx -19
addx 4
noop
noop
noop
addx 3
addx 2
addx 5
noop
addx 1
addx 4
addx -21
addx 22
addx -38
noop
noop
addx 7
addx 32
addx -27
noop
addx 3
addx -2
addx 2
addx 5
addx 2
addx 2
addx 3
addx -2
addx 2
noop
addx 3
addx 5
addx 2
addx 3
noop
addx -39
addx 2
noop
addx 4
addx 8
addx -8
addx 6
addx -1
noop
addx 5
noop
noop
noop
addx 3
addx 5
addx 2
addx -11
addx 12
addx 2
noop
addx 3
addx 2
addx 5
addx -6
noop

48
2022/day10/src/main.rs Normal file
View File

@@ -0,0 +1,48 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::str;
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let reader = BufReader::new(file);
let mut part1: Vec<_> = Vec::new();
let mut part2 = [[b'.'; 40]; 6];
let mut x_reg = 1;
let mut pc = 0_u32;
reader.lines().for_each(|line| {
let line = line.unwrap();
let op = match line.split(' ').collect::<Vec<_>>()[..] {
["addx", x] => Some(x.parse::<u32>().unwrap()),
["noop"] => None,
_ => panic!("invalid command: {line}"),
};
let steps = if op.is_some() { 2 } else { 1 };
for i in 0..steps {
let row = pc / 40;
let col = pc % 40;
if col - 1 == x_reg || col == x_reg || col + 1 == x_reg {
part2[row as usize][col as usize] = b'#';
}
pc += 1;
if pc < 221 && (pc - 20) % 40 == 0 {
part1.push(pc * x_reg);
}
if i == steps - 1 {
if let Some(x) = op {
x_reg += x;
}
}
}
});
println!("Part 1: {}", part1.iter().sum::<u32>());
for row in part2 {
println!("Part 2: {}", str::from_utf8(&row).unwrap());
}
Ok(())
}

102
2022/day11/Cargo.lock generated Normal file
View File

@@ -0,0 +1,102 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day11"
version = "0.1.0"
dependencies = [
"derive-getters",
"itertools",
"log",
"nom",
]
[[package]]
name = "derive-getters"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2c35ab6e03642397cdda1dd58abbc05d418aef8e36297f336d5aba060fe8df"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "proc-macro2"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"

15
2022/day11/Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "day11"
description = "AOC 2022 Day 11"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
derive-getters.workspace = true
itertools.workspace = true
log.workspace = true
nom.workspace = true

378
2022/day11/src/main.rs Normal file
View File

@@ -0,0 +1,378 @@
#![warn(clippy::all, clippy::pedantic)]
use derive_getters::Getters;
use itertools::Itertools;
use log::{debug, trace};
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::multispace1,
error::Error,
multi::separated_list1,
sequence::{delimited, preceded},
IResult, Parser,
};
use std::{
collections::VecDeque, error, fmt::Display, fs::File, io::Read, num::ParseIntError,
str::FromStr,
};
pub type BoxError = std::boxed::Box<
dyn std::error::Error // must implement Error to satisfy ?
+ std::marker::Send // needed for threads
+ std::marker::Sync, // needed for threads
>;
#[derive(Debug)]
enum MyParseError {
ParseIntError(ParseIntError),
}
impl error::Error for MyParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
// The cause is the underlying implementation error type. Is implicitly
// cast to the trait object `&error::Error`. This works because the
// underlying type already implements the `Error` trait.
Self::ParseIntError(ref e) => Some(e),
}
}
}
impl From<ParseIntError> for MyParseError {
fn from(value: ParseIntError) -> Self {
Self::ParseIntError(value)
}
}
impl Display for MyParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ParseIntError(err) => {
write!(f, "There was a problem parsing an integer: {err}")
}
}
}
}
#[derive(Debug, Clone, Copy)]
enum Tok {
Num(u64),
Old,
}
impl Tok {
pub fn get_value_or(&self, current: u64) -> u64 {
match self {
Self::Num(i) => *i,
Self::Old => current,
}
}
pub fn parse_tok(input: &str) -> IResult<&str, Self> {
alt((
tag("old").map(|_| Self::Old),
nom::character::complete::u64.map(Self::Num),
))(input)
}
}
impl Default for Tok {
fn default() -> Self {
Self::Old
}
}
impl FromStr for Tok {
type Err = MyParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val = match s {
"old" => Self::Old,
x => Self::Num(x.parse()?),
};
Ok(val)
}
}
#[derive(Debug, Default, Clone, Copy)]
enum Op {
Add(Tok, Tok),
Mul(Tok, Tok),
#[default]
Noop,
}
impl Op {
pub fn do_op(&self, current: u64) -> u64 {
match self {
Self::Add(a, b) => a.get_value_or(current) + b.get_value_or(current),
Self::Mul(a, b) => a.get_value_or(current) * b.get_value_or(current),
Self::Noop => panic!("No operation implemented"),
}
}
pub fn parse_op(input: &str) -> IResult<&str, Self> {
let (input, _) = tag("Operation: new = ")(input)?;
let (input, value_1) = Tok::parse_tok(input)?;
let (input, operator) =
delimited(multispace1, alt((tag("*"), tag("+"))), multispace1)(input)?;
let (input, value_2) = Tok::parse_tok(input)?;
let op = match operator {
"+" => Self::Add(value_1, value_2),
"*" => Self::Mul(value_1, value_2),
_ => Self::Noop,
};
Ok((input, op))
}
}
impl FromStr for Op {
type Err = MyParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val = match s.split_whitespace().collect::<Vec<_>>()[..] {
["new", "=", a, "+", b] => Self::Add(a.parse()?, b.parse()?),
["new", "=", a, "*", b] => Self::Mul(a.parse()?, b.parse()?),
_ => Self::Noop,
};
Ok(val)
}
}
#[derive(Debug, Default, Getters, Clone, Copy)]
struct Test {
divisor: u64,
true_to: usize,
false_to: usize,
}
impl Test {
pub fn get_to(&self, item: u64) -> usize {
match item % self.divisor {
0 => self.true_to,
_ => self.false_to,
}
}
pub fn parse_test(input: &str) -> IResult<&str, Self> {
trace!("parse test 1");
let (input, divisor) =
preceded(tag("Test: divisible by "), nom::character::complete::u64)(input)?;
trace!("parse test 2");
let (input, _) = multispace1(input)?;
trace!("parse test 3");
let (input, true_to) = match preceded(
tag("If true: throw to monkey "),
nom::character::complete::u64.map(|x| usize::try_from(x).unwrap()),
)(input)
{
Err(e) => {
println!("{e:?}");
Err(e)
}
x => x,
}?;
trace!("parse test 4");
let (input, _) = multispace1(input)?;
trace!("parse test 5");
let (input, false_to) = preceded(
tag("If false: throw to monkey "),
nom::character::complete::u64.map(|x| usize::try_from(x).unwrap()),
)(input)?;
trace!("parse test 6");
Ok((
input,
Self {
divisor,
true_to,
false_to,
},
))
}
}
#[derive(Debug, Default, Getters, Clone)]
struct Ape {
items: VecDeque<u64>,
operation: Op,
test: Test,
inspected: u64,
}
impl Ape {
pub fn catch(&mut self, item: u64) {
self.items.push_back(item);
}
pub fn inspect(&mut self, relief: bool) -> Option<u64> {
let relief: u64 = if relief { 3 } else { 1 };
let item = self.items.pop_front()?;
self.inspected += 1;
Some(self.operation.do_op(item) / relief)
}
pub fn throw(&self, item: u64) -> usize {
self.test.get_to(item)
}
//impl nom::ParseTo<Ape> for String{
// todo!()
// }
pub fn parse_monkey(input: &str) -> IResult<&str, Self> {
trace!("parse monkey 1");
let (input, _id) =
delimited(tag("Monkey "), nom::character::complete::u64, tag(":"))(input)?;
trace!("parse monkey 2");
let (input, _) = multispace1(input)?;
trace!("parse monkey 3");
let (input, items) = match preceded(
tag("Starting items: "),
separated_list1(tag(", "), nom::character::complete::u64),
)(input)
{
Err(x) => {
println!("{x:?}");
Err(x)
}
x => x,
}?;
trace!("parse monkey 4");
let items = VecDeque::from(items);
let (input, _) = multispace1(input)?;
trace!("parse monkey 5");
let (input, operation) = Op::parse_op(input)?;
trace!("parse monkey 6");
let (input, _) = multispace1(input)?;
trace!("parse monkey 7");
let (input, test) = match Test::parse_test(input) {
Err(e) => {
println!("{e:?}");
Err(e)
}
x => x,
}?;
trace!("parse monkey 8");
Ok((
input,
Self {
items,
operation,
test,
inspected: 0,
},
))
}
}
impl FromStr for Ape {
type Err = Error<String>;
fn from_str(_s: &str) -> Result<Self, Self::Err> {
todo!()
//Self::parse_monkey(s)?.finalize()
}
}
#[derive(Debug, Default, Getters, Clone)]
struct Monkeys {
monkeys: Vec<Ape>,
}
impl FromStr for Monkeys {
type Err = Error<String>;
fn from_str(_s: &str) -> Result<Self, Self::Err> {
todo!()
}
}
impl AsRef<Vec<Ape>> for Monkeys {
fn as_ref(&self) -> &Vec<Ape> {
self.monkeys.as_ref()
}
}
struct MonkeyReader {
buffer: String,
}
impl MonkeyReader {
pub fn new(file: &str) -> Result<Self, std::io::Error> {
let mut f = File::open(file)?;
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
Ok(Self { buffer })
}
}
impl IntoIterator for MonkeyReader {
type Item = Ape;
type IntoIter = MonkeyReaderIter;
fn into_iter(self) -> Self::IntoIter {
MonkeyReaderIter {
content: self.buffer,
}
}
}
struct MonkeyReaderIter {
content: String,
}
impl Iterator for MonkeyReaderIter {
type Item = Ape;
fn next(&mut self) -> Option<Self::Item> {
match self.content.as_str() {
"" => {
trace!("done");
None
}
x => {
trace!("Starting ape parse");
let (input, _) = nom::combinator::opt(tag::<_, _, Error<&str>>("\n\n"))(x).ok()?;
trace!("pass the optional");
let (input, monkey) = Ape::parse_monkey(input).ok()?;
trace!("Saving off ape");
self.content = String::from(input);
debug!("{monkey:?}");
Some(monkey)
}
}
}
}
fn main() {
let mut monkeys = MonkeyReader::new("./test.txt")
.unwrap()
.into_iter()
.collect::<Vec<_>>();
println!("{monkeys:?}");
//parse
let mut monkeys_game2 = monkeys.clone();
//game 1
for _ in 0..20 {
for i in 0..monkeys.len() {
while let Some(item) = monkeys[i].inspect(true) {
let throw_to = monkeys[i].throw(item);
monkeys[throw_to].catch(item);
}
}
}
println!("{monkeys:?}");
let val = monkeys
.iter()
.map(Ape::inspected)
.sorted()
.rev()
.take(2)
.product::<u64>();
println!("Part 1: {val}");
let magic = monkeys_game2
.iter()
.map(|x| x.test().divisor())
.product::<u64>();
for _ in 0..10_000 {
for i in 0..monkeys_game2.len() {
while let Some(item) = monkeys_game2[i].inspect(false) {
let item = item % magic;
let throw_to = monkeys_game2[i].throw(item);
monkeys_game2[throw_to].catch(item);
}
}
}
let val2 = monkeys_game2
.iter()
.map(Ape::inspected)
.sorted()
.rev()
.take(2)
.product::<u64>();
println!("part 2: {val2}");
}

55
2022/day11/test.txt Normal file
View File

@@ -0,0 +1,55 @@
Monkey 0:
Starting items: 65, 78
Operation: new = old * 3
Test: divisible by 5
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 78, 86, 79, 73, 64, 85, 88
Operation: new = old + 8
Test: divisible by 11
If true: throw to monkey 4
If false: throw to monkey 7
Monkey 2:
Starting items: 69, 97, 77, 88, 87
Operation: new = old + 2
Test: divisible by 2
If true: throw to monkey 5
If false: throw to monkey 3
Monkey 3:
Starting items: 99
Operation: new = old + 4
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 5
Monkey 4:
Starting items: 60, 57, 52
Operation: new = old * 19
Test: divisible by 7
If true: throw to monkey 7
If false: throw to monkey 6
Monkey 5:
Starting items: 91, 82, 85, 73, 84, 53
Operation: new = old + 5
Test: divisible by 3
If true: throw to monkey 4
If false: throw to monkey 1
Monkey 6:
Starting items: 88, 74, 68, 56
Operation: new = old * old
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 2
Monkey 7:
Starting items: 54, 82, 72, 71, 53, 99, 67
Operation: new = old + 1
Test: divisible by 19
If true: throw to monkey 6
If false: throw to monkey 0

32
2022/day12/Cargo.lock generated Normal file
View File

@@ -0,0 +1,32 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day12"
version = "0.1.0"
dependencies = [
"itertools",
"log",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"

10
2022/day12/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "day12"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.11.0"
log = "0.4.20"

201
2022/day12/src/main.rs Normal file
View File

@@ -0,0 +1,201 @@
#![warn(clippy::all, clippy::pedantic)]
use std::{
collections::VecDeque,
fs::File,
io::{prelude::*, BufReader},
};
pub type BoxError = std::boxed::Box<
dyn std::error::Error // must implement Error to satisfy ?
+ std::marker::Send // needed for threads
+ std::marker::Sync, // needed for threads
>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
struct Square {
pub height: u8,
pub pos: (usize, usize),
}
impl Square {
pub fn _index(&self, grid_size: &(usize, usize)) -> usize {
grid_size.1 * self.pos.0 + self.pos.1
}
pub fn can_go_to(&self, other: &Self) -> bool {
let from_height = self.height;
let to_height = other.height;
if from_height + 1 < to_height {
return false;
}
let (from_row, from_col) = self.pos;
let (to_row, to_col) = other.pos;
(from_row == to_row || from_col == to_col)
&& from_row + 1 >= to_row
&& from_row <= to_row + 1
&& from_col + 1 >= to_col
&& from_col <= to_col + 1
}
}
#[derive(Debug, Default, Clone)]
struct Game {
board: Vec<Square>,
distances: Vec<Option<u64>>,
board_size: (usize, usize),
start_pos: (usize, usize),
end_pos: (usize, usize),
}
impl From<File> for Game {
fn from(file: File) -> Self {
let buffer = BufReader::new(file);
let mut board = Vec::new();
let mut board_size = (0_usize, 0_usize);
let mut start_pos = (0_usize, 0_usize);
let mut end_pos = (0_usize, 0_usize);
for (row, line) in buffer
.lines()
.enumerate()
.map(|(row, col)| (row, col.unwrap()))
{
board_size.0 += 1;
board_size.1 = if board_size.1 == 0 {
line.len()
} else {
board_size.1
};
for (col, val) in line.as_bytes().iter().enumerate() {
let height = match val {
b'E' => {
end_pos = (row, col);
25
}
b'S' => {
start_pos = (row, col);
0
}
x => x - b'a',
};
board.push(Square {
pos: (row, col),
height,
});
}
}
let mut distances = std::iter::repeat(None)
.take(board.len())
.collect::<Vec<Option<u64>>>();
let index = board_size.1 * end_pos.0 + end_pos.1;
distances[index] = Some(0);
let mut game = Game {
board,
distances,
board_size,
start_pos,
end_pos,
};
game._set_distances();
game
}
}
impl Game {
fn pos_to_index(&self, pos: &(usize, usize)) -> usize {
self.board_size.1 * pos.0 + pos.1
}
fn _get_adjacent_from(&self, pos: &(usize, usize)) -> Vec<(usize, usize)> {
let mut vec = Vec::new();
let index = self.pos_to_index(pos);
if pos.0 != 0 {
let new_pos = (pos.0 - 1, pos.1);
let new_index = self.pos_to_index(&new_pos);
if self
.board
.get(new_index)
.unwrap()
.can_go_to(self.board.get(index).unwrap())
{
vec.push(new_pos);
}
}
if pos.0 + 1 < self.board_size.0 {
let new_pos = (pos.0 + 1, pos.1);
let new_index = self.pos_to_index(&new_pos);
if self.board[new_index].can_go_to(&self.board[index]) {
vec.push(new_pos);
}
}
if pos.1 != 0 {
let new_pos = (pos.0, pos.1 - 1);
let new_index = self.pos_to_index(&new_pos);
if self.board[new_index].can_go_to(&self.board[index]) {
vec.push(new_pos);
}
}
if pos.1 + 1 < self.board_size.1 {
let new_pos = (pos.0, pos.1 + 1);
let new_index = self.pos_to_index(&new_pos);
if self.board[new_index].can_go_to(&self.board[index]) {
vec.push(new_pos);
}
}
vec
}
fn _set_distances(&mut self) {
let mut queue = VecDeque::new();
queue.push_back(self.end_pos);
while let Some(cur_pos) = queue.pop_front() {
let adjacent = self._get_adjacent_from(&cur_pos);
let cur_index = self.pos_to_index(&cur_pos);
let cur_dist = self.distances.get(cur_index).unwrap().unwrap();
adjacent
.iter()
.filter(|pos| {
let index = self.pos_to_index(pos);
match self.distances.get(index).unwrap() {
None => {
self.distances[index] = Some(cur_dist + 1);
true
}
Some(check_dist) if *check_dist > cur_dist + 1 => {
self.distances[index] = Some(cur_dist + 1);
true
}
_ => false,
}
})
.for_each(|check_pos| {
queue.push_back(*check_pos);
});
}
}
pub fn get_distance_to_end(&self, pos: &(usize, usize)) -> Option<u64> {
*self.distances.get(self.pos_to_index(pos)).unwrap()
}
pub fn get_all_at_height(&self, height: u8) -> Vec<Square> {
self.board
.iter()
.filter(|x| x.height == height)
.copied()
.collect()
}
}
fn main() -> Result<(), BoxError> {
let file = File::open("./test.txt")?;
let game: Game = file.into();
let start = game.start_pos;
let s = game
.get_distance_to_end(&start)
.expect("No end to this game");
println!("Part 1: {s}");
let shortest = game
.get_all_at_height(0)
.iter()
.filter_map(|x| game.get_distance_to_end(&x.pos))
.min()
.expect("problem");
println!("Part 2: {shortest}");
Ok(())
}

41
2022/day12/test.txt Normal file
View File

@@ -0,0 +1,41 @@
abccccccccccccccccaaccccccccccccccccccccaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
abcccccccccccccaaaaaccccccccccccccccccccaaaaaaaaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaa
abccccccccccccccaaaaaccccccccccccccaaaaacccaaaaaacccccaaccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaa
abccccccccccccccaaaaacccccccccaacccaaaaacccaaaaaaaccccaaaacaacaaccccccccccccccccccccccccaaaccccaaaccccccccccccaaaa
abcccccccccccccaaaaacccccccaaaaaccaaaaaacccaaaaaaaacaaaaaacaaaaaccccccccccccccccccccccccaaacccaaaaccccccccccccaaac
abccccccaacaaaccccaaccccccccaaaaacaaaaaaccaaaacaaaacaaaaaccaaaaaaccccccccccccccccccccccccaaaaaaaacccccccccccccaacc
abccccccaaaaaaccccccccccccccaaaaacaaaaaaccaaaaccaaaacaaaaacaaaaaacccccccccccccccccccccccaaaaaaaaaccccccccccccccccc
abccccccaaaaaacccccccccccccaaaaaccccaaccccaacccccaaccaacaacaaaaaccccccccccccccccccccccccccaaakkkkllllcccaaaccccccc
abccccccaaaaaaacccccccccccccccaaccccaacccccccccccccccccccccccaaaccccccaaaacccccccccjjjjkkkkkkkkkkllllccccaacaccccc
abcccccaaaaaaaacccccaaccccccccccccccaaaaaaccccccccccccccccccaaccccccccaaaaccccccccjjjjjkkkkkkkkkppllllcccaaaaacccc
abcccccaaaaaaaaccaaaacccccccccccccccaaaaaccccccccccccccccaacaaccccccccaaaacccccccjjjjjjjkkkkkppppppplllccaaaaacccc
abccccccccaaaccccaaaaaacccccccccccaaaaaaaccccccccccccccccaaaaacccccccccaacccccccjjjjoooooooppppppppplllcccaaaccccc
abccccccccaaccccccaaaaaccccaacccccaaaaaaaaccccaaacccccccccaaaaaaacccccccccccccccjjjooooooooppppuuppppllcccaaaccccc
abccccccaacccccccaaaaacccccaaaccaaaaaaaaaaccaaaaaaccccccaaaaaaaaaacaaaccccccccccjjjoooouuuoopuuuuupppllcccaaaccccc
abacccccaaccccccccccaacccccaaaaaaaccaaaaaaccaaaaaaccccccaaaaaccaaaaaaaccccaaccccjjoootuuuuuuuuuuuuvpqlllcccccccccc
abaccaaaaaaaacccccccccccccccaaaaaaccaacccccccaaaaacccccccacaaaccaaaaaaccaaaacaccjjooottuuuuuuuxyuvvqqljjccddcccccc
abcccaaaaaaaaccccccccccccaaaaaaaaacaacaaccccaaaaaccccccccccaaaaaaaaaacccaaaaaacciijootttxxxuuxyyyvvqqjjjjdddcccccc
abcccccaaaaccccaaacccccccaaaaaaaaacaaaaaccccaaaaaccccccccccccaaaaaaaaacccaaaaccciiinntttxxxxxxyyvvqqqqjjjddddccccc
abccccaaaaaccccaaaaacccccaaaaaaaaaaaaaaaaccccccccccccccccccccaaaaaaaaaaccaaaaccciiinntttxxxxxxyyvvvqqqqjjjdddccccc
abccccaaaaaaccaaaaaccccccccaaaaaaaaaaaaaacccccccccccccccccccccccaaacaaacaacaaccciiinnnttxxxxxyyyvvvvqqqqjjjdddcccc
SbccccaaccaaccaaaaacccccccccaaaaaaaaaaaaacccccccccccccccccccccccaaacccccccccccciiinnntttxxxEzzyyyyvvvqqqjjjdddcccc
abcccccccccccccaaaaacccccccaaaaaaaaacaaaccccccccccccccccccccccccaaccccccccccccciiinnnttxxxxyyyyyyyyvvvqqqjjjdddccc
abcccccccccccccaaccccccccccaaaaaaaaccccccccccccccccccccccccccccccccccccccccccciiinnntttxxyyyyyyyyyvvvvqqqjjjdddccc
abccccccccccccccccccccccccaaaaaaaacccccccccccccccccccccccccccccccccccccccccccciiinntttxxxwwwyyywwvvvvrqqjjjjdddccc
abcccccccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccccccccccciinnntttxwwwwwyyywwvvvrrrqkkkeddcccc
abcccccccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccccccccccchhnnntttsswwswwyywwrrrrrrkkkkeeecccc
abcccccccccccccccccccccccccccaaaaaacccccccccccccccccccaccccccccccccaaacccccccchhhnmmssssssswwwwwwrrrkkkkkeeeeecccc
abcccccccccccccccccccccccccccccaaacccccccccccccccccccaaccccccccccaaaaaacccccaahhhmmmmmsssssswwwwrrrkkkkkeeeeeccccc
abaacccccccccccccaccccccccccccccccccccccccccccccccaaaaacaacccccccaaaaaacaaaaaahhhhmmmmmmmmssswwwrrkkkkeeeeeacccccc
abacccccccccccccaaaaaaaaccccccccaaacccccccaaccccccaaaaaaaacccccccaaaaaacaaaaaaahhhhmmmmmmmmsssrrrrkkkeeeeeaacccccc
abaaaccccaaccccccaaaaaacccccccccaaacccaacaaaccccccccaaaacccccccccaaaaacccaaaaaaahhhhhhhmmmmlsssrrllkfeeeeaaaaacccc
abaaaccaaaaccccccaaaaaacccccccccaaaaaaaaaaaaaacccccaaaaacccccccccaaaaacccaaaaaaachhhhhgggmllsssrrllkffeaaaaaaacccc
abaacccaaaaaacccaaaaaaaacccccaaaaaaaaaaaaaaaaacccccaacaaacccccccccccccccaaaaaacccccchggggglllllllllfffaaaaaaaacccc
abaaccccaaaacccaaaaaaaaaaccccaaaaaaaaacaaaaaaaccaccaccaaacccccccccccccccaaaaaacccccccccgggglllllllffffaaaaaacccccc
abcccccaaaaacccaaaaaaaaaacccccaaaaaaaccaaaaacccaaaccccccccccccccccccccccccccaacccccccccagggglllllffffccccaaacccccc
abcccccaacaaccccccaaaaacaccaacccaaaaaaaaaaaaaccaaacccccccccccccccccccccccccccccccccccccaagggggffffffcccccccccccccc
abcccccccccccaaaaaaaaacccccaaccaaaaaaaccaaaaacaaaaccccccccccccccccccccccccccccccccccccaaaacgggfffffccccccccccccccc
abcccccccccccaaaaacaacccaaaaaaaaaaccaacccaaaaaaaacccaaccccccccccccccccccccccccccccccccccccccggfffccccccccccccaaaca
abccccccccccaaaaaaccccccaaaaaaaaacccccccccaaaaaaaaaaaacccccccccccccaaaccccccccccccccccccccccaaaccccccccccccccaaaaa
abccccccccccaaaaaaccccccccaaaacccccccccccccaaaaaaaaaaaaccccccccccccaaaaccccccccccccccccccccccaaaccccccccccccccaaaa
abcccccccccccaaaaacccccccaaaaaaccccccccccaaaaaaaaaaaaaaccccccccccccaaaaccccccccccccccccccccccccccccccccccccccaaaaa

12
2022/day13/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "day13"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true
log.workspace = true

449
2022/day13/input.txt Normal file
View File

@@ -0,0 +1,449 @@
[[5],[1,[[0]]],[],[3,[[9,1],[3,4,10],8,3],6]]
[[],[[6,8],4]]
[[[[4,4,6,0],[10,4,9]],0,[3,9],[[2,2,4],7,4,2,8],[0,8,7,[9]]],[[[],5,7]],[[10,[9,6],7],[[5,7],4,7],5,[]]]
[[0,5]]
[[7],[],[[1]]]
[[[[9],[1]]],[2],[[[2,10,9],3,0,6],3],[[9,[9,4,8,9]],3],[9,[9,[0,6,10],3],[[8,10,0,7],[2,3,4,2],[2,2,3]],[7,6,10]]]
[[[[],[1,3,4],8,5,1],1,[],3,1],[[5]],[]]
[[[3,9,8,7,[0,6,8]]],[[[0,5]],3]]
[[[[],[4,8]],[],[[],9,8]],[0],[[2,[1,3,0],5,4],0,10,1]]
[[[[3,10,6,9]],5],[0,2]]
[[[3],6],[[1,[6,9,3,4]],[],[6],2],[2,[[],[5],4,[3,9,9]]]]
[[[[6,10,5],6,0,[2,6],0],8,[2,9,[1,0,5]]],[[7,[0,0,9,0],[0,6,2,6,6],[]],1,[[4],[5,2],[2,4,2],5],[[9]],2],[10,[[10],6,9],10],[7]]
[[3,[[5,7,0,1,4],10,2,[2,10,7,0]],[[]]]]
[]
[[6,[],[6,[6,3]]],[10],[5,3]]
[[],[[],0],[[4,[0,2,0],4,1],[[5,2,1,4]],[10,2,[5,5,5]],[[10],[6,6,3,0],8,7]]]
[[[],[[],5],[[10,9,5],[8,2,5],8,1,4],3],[[[7,8,9,0],3,[5],[4,9]],[[3],9,[4,0],[0,9,0]],[1,10,9],[1,[7,8],9],4]]
[[7,[9,[3,1],[10,6,2,3,1]]],[[2,10],6],[[[3,9,10],[6,9]],6,5,[8,[0,3],0,7,1],[]]]
[[8,[[1,0,4,6,9],2]]]
[[3,[9,[],[5]]]]
[[[],[],[],4,[[10,9,7],[10,1],[4,8,4,0,7],7]],[]]
[[6,0,[],3]]
[[3,3,[[1,9,8],4,[1,4,1],[],8],[[6,1],[2,2,10,3,4],[0,2,4,6,4],[10,7,2],[1]]],[[0],[[6,3,3,2,5]],3,10,[10]]]
[[[[1,9],7,[]],[8,[6,10],[8,2],[2,5]],[[2],0,5,4],5,[3,5,6,3]],[],[],[]]
[[],[8,[6],0],[7,10,9,[1]],[],[7,[[]],5,2]]
[[[]],[[[1,3,7,10,9],[2,3,3,1,5],[7,10,0,9,3],[]],[4,0,5],[[],[],9,[0]],[[3],[10],6]],[]]
[[10,[10,7,[0,10,10]],8,8],[6],[0,2,[[],4],[[2,9,6,8,5],6]]]
[[[[],[],5,3]],[],[[[7,10],[2,2,3,9,7]],[5],9,3],[[2],[5,[4,9,8,3,6],1],0,[[8,6],3,[8,10],4],2]]
[[7,[6],[[1]],[4,[8,0,1,4,8],0,[5],9]],[],[5,[[10,10,10,5,4]],[0,[6,7,4],[2,5,6],[1]],8]]
[[1,4,8]]
[[1,0,[[10,5,1,9,2],[0,6,6,6],[9,5,8,8]],4,[3,4]],[],[],[[7],4,[[3,2],[5,5,3,3,6]],8],[]]
[[9,3,[]],[[0,[8,9,4,4],[7,4,3,7]],6],[3,9,1,0],[[]]]
[[4,[3,[10,10,10,8],[4,4,0],8,[9,7,9,4]],1,3],[3,10,0,5,[[9,6],2,3,[1,7,10],4]],[[[10,10,7,10,1],10],9],[5,6,4]]
[[[5,4,9,10],2,[[5],[9,9,9,7],[],[7,10,2],[6]],[10,3,[4]],[[],[8],1,[],[]]],[[[8,2,5,7],9,5,[2,8,2]],7],[7,[[],[10,1,0]]]]
[[6,8,[0,7,9,3,4],[8]]]
[[[6,[2],[10,1,2,8,6],2,6]],[[10,10],[[0,3,1,6,0],10]],[],[8,9,7,2],[[6,9,4,2,0],7,0]]
[[[]],[[3,6,9,[2],[7]],[[0,9,2,7],[3],2,0,[9,5,1,8]],6],[[],3,[[],4],9,6]]
[[4],[1,9,4,10],[],[[],4,5],[[],6,4]]
[[],[[5]],[[4,[10,9,3],[1,6,0],[8]],[[7],[1,3,4,6],[5],[1,7,8,8,1]],[1,[],[],8],[],[[5,2],2,[3,6,1,2],6]]]
[[[[4]],3]]
[[7,5,[],[8],[[3],[8],[2,6,8],[10,4,7]]]]
[[1,4,4]]
[[[8,[10,2,8,3,4]],8],[],[[3],[],5],[7,[[7]],[[0,7,6],[4],5],[[4],[10,4],7,6]],[5,8]]
[[0]]
[[],[[2,[9],0],6],[6,[]]]
[[7,8,[8,3],1,10],[10,[]],[],[[[2,5,7,9],[2,3,7,0,3],[3]],[]]]
[[[4],6,4,1]]
[[5,3],[2,[6,9],[[5,0,2,8]],1,[10,10]],[10],[[[],10,[10],6,6],5,[[0],9]]]
[[2,[0],5,[6,5,[2],7]]]
[[[8,8,[8,2,1,4,5]],6,[[10],6,[],[]],[[9,6]],5],[7,2,[9]],[3,8,1],[2,[5,4,4,10],10,2],[]]
[[6,3],[[[6],0,1]],[9,10],[10,10,5]]
[[[[1,8,5,10]],7,[]],[[]],[[[1,1,5,6],[1],[8,9,10,10],[4,1,10]],[6,[6,9,4,3,8],[4,1,0]],[[]],[[10,2,4,8,6],1,8,7]]]
[[]]
[[[[5,6,3,7,4],[1,1,8,0],[0,8],[2,2,10,9,6]],[[1,7,6,10,9],[3,3,3],6],[[2,7],6,[3,8]]],[2,3,9,5],[],[9,[[],[],[1,5,4],7]]]
[[[[1,7,1,3,4]],3,1,[4,5,8,[2,10,7,7],9]],[6,[1,[],4,4,9]],[[0,[1,6,0,4],5],2,4],[[[4,10,0,10,9],2,7,0,[4,4,10,10]],4,2,[]]]
[[10,3,2,5,[[9,6],[9],4]],[[[8,9,10],[9,8,7,3,7],[3,7,3],7,3],[8,4],7],[],[[[7,4,10],[9],[7,9,8,1],4,5],[9,2,[2],[10,5]],8,1],[[],3]]
[[],[4,10,5,2,[7,[6,2,1,3,3]]],[3,[[3],1],6]]
[[[0,9]],[5,[9,[1,5,2,7,5],10,[4],1]],[],[2,[2,2,[9,4]],[[0,7,2]],5,1],[]]
[[[[8,4,1,8,5]],[4,1,[],[2,6,5]],5,3],[[5,[2],10,[8],[10]],2,0,[[6,5],[7,2,8,6],[7],8],[10,[4,6],[]]],[[10],9],[],[10,[[],[0,0,4],[9,6,7,7,10],2]]]
[[[4,10,7,[5,10],8],0,[],[[1,0],[5],9],[]],[0,[6,[1,0],[],[9,5,9]],[[6,10,4,7,7],10,[10,6,1,1,10],7]]]
[[8,[0,8,7],2],[1,[[5,10]],[[3,4,0,9,0],6,0]],[[[0,7,9,0,4],6,[],[9],[7]],[5]],[[],[[5,10],[0,9,6,6,6],[1,6,3]],8,7]]
[[[[],[]]],[[1,10],5]]
[[1],[],[[[4,9,1,4,1],2,[1,4,8,7,7]],2,[],[],8]]
[[[10],9,[],[6,[],[1],9,[4,6,6,6]],8],[2,[[9,7]],[5,7,2,[2,7,5,6,8],[6,1,0,1]],1,5],[[4],7,0]]
[[9,[[1,5,1],6,2,5,[5,0,3,6,5]]],[4,3,2,[4,[3,1,8,0,9],[6,0,8,7,6],5,10],[]],[[[8,9,5,1,7],9,[9],0,7],10,[[9,10,1,8,7],6,1],1]]
[[[[2,1,10],6]],[],[6,[[6,7],10],[9,10,[],9,4]],[],[[[7,8],5,[6,10,6,8]],[],[[],[8,8,9],8,8,1]]]
[[[[9,0,5,2],2,6],10,[2,4],[],7],[10,[[],5,4,9],[2],[[4,5],[3,2,8,2],[10],[3]],6],[2,1,[]],[]]
[[[1,[0,8,7,6],8,8,4],10],[1,[[6,4,1],[],3,[1,5,5],4],2],[5,2],[],[[5,[8],[6,10,3,9],[1]],[4],[[9,9],[10,2,10,6,5]]]]
[[5,5,6],[[[2,3],9,8]],[[[2,6,9],9,[4],[],2]],[6,[[5,6,6],[10,1,3,5],[1,1,7,9]],[6],[[0,10,0,6],[5,2,4,4,4],[5],8],[]],[1,[0,0,[3,7,8,6],2],8,[5,[10,8,2,3,7],[],[]]]]
[[],[[1,[4,1]],[3,2,[5,2,7]],[5,[7,5,1,5],7],[10,[9,5,1],[2],[3,1,9,7,6],10]],[2,[[5]],0],[8],[[4],[],10]]
[[8,0,8,7],[5,[4],3,6,[]],[],[]]
[[9,1],[7]]
[[7,6],[],[[2,[1,1,9,1,1],[5]]]]
[[7]]
[[[3]],[[2,[0,6]],3,[7,[9,3,2,2,1],[]],[5,9]],[6,[[4,4]],10,[[],[0,0],7,[9,9,4],4]],[[[7,2],[7,9,1,10,9],9]],[[[3,3,9,3,10]],[],[[],3,6]]]
[[[7,2,9],1],[[3],4,8],[[8,8,5],[10],[],1,[10]],[10,2,[8],[[4,5,4,3,9],[]],[7,5]],[3,6,10,[[],4,1,[]]]]
[[7,0,[]]]
[[],[],[2,8],[2,0,2,6,8]]
[[[8,[7,3,10,0]],[],8,[[9,7,5,9],[2],8,7],[]],[9,[[7]],0,0,[4]],[10]]
[[2,5,7],[[1,10,[],[9,6,5,8],3],[],9]]
[[[8,[10,6],3]],[2],[6,[2,[9],3,[2,1,3,0,0],[0,10,3]]],[[9,[4,4]],[]]]
[[5,10,[[0,6,6],[],0,[3,10,3],[0,0,8,2,9]]],[[[1,2,10,1],3,[8],[8,1],6],4,7],[6,0,[7,[9,0,10,6,7]],9,10],[[5,7]]]
[[9],[[[3,7,6,0,2],[10,1,7,9,4],[0,1,5,3,10],4,6],[9],[[],[1,1],7,[3]]],[9,[]],[[],[[9,7,10,1],[9,8,9]],[[4,6],[3],5],[8,4,6,2,[9,10]]],[[8],[[2,6,0,0]],8,9,4]]
[[[5,[5,6,3],10],[1,[0,9,3,5],[],[6,8,5,1,8]],[[6,3,4],3]]]
[[[10,6],3,2,9]]
[[[8,9,[2],4]],[],[9,[[9,0,4,4,3],[],[5,8],8]],[[6,10,10],[2,6],6,4],[[5,10,[3,9,1]],[[1]]]]
[[],[6,0,4,[[2,5,5],[10,9,2],1,[7,1,3,8]]],[6,4]]
[[5],[[[4],[3],[2,8,0],9],9,10,0],[[[]],0,[],8]]
[[],[4,[[5,3,7,3],3],6],[10,[[],[7,4],[],[6,0,4],[5,4,8]],1,[7,[3,0],5,[0]],[[1,7,5,4,0]]]]
[[[[],[0,1,5]],10,4,[0,[2,7]],6],[5],[[[9,4,0,0],8,[8,2,8],[1,2]],6,1,0],[[]],[[6,[]],[7,[8,8],[]]]]
[[[],2]]
[[3],[7,[],[0,3,8,[8]]]]
[[],[[4,0,[]]],[[[2],[4,8],10,[4,10]],[[0,1,6,6],1,4,9,[0]],7,[9,8,[8,10,5,3,3],[8],[5,2,0]],[[3,5,0],[10,8,10,5]]],[[2,2,2,4]],[]]
[[[[4,3,10],1,7,[]],8,2],[[[4,4],8,6],0,4],[[3,5,[7,9,8,10,9],9],5,[[],10,[3],2,[6,8,6,7]]]]
[[[9,8,[6,0,3,7,7]],0],[1,3],[4],[[[],8,8,6,[3]],2]]
[[[[5],10],[7,[0,1,9,6,6],[6],10,[1]],[[9,8,6],[3,9]]],[[[]],[[10,2],[9,9,0],[2,8,9,7],[9,1]],7,[[7],[8],[9,2],4,0]],[]]
[[10,5,0,[4],10],[1]]
[[],[6,3,[[10,7,10,3,4],3],0],[]]
[[[]],[1,6,6,10,10],[[1,[7],[9]]],[],[[[4,8,6],[6]],6,[[8],2,[5],[8,1,9],0]]]
[[],[3,3,10,8],[3,[[7,1,9]]],[9,[[6,6,7,0],1,1,[]],[4,[9,4,9,7,9]],[6]],[7]]
[[],[8,6]]
[[6,3],[[[10]]],[[[3,8,8,9],[9,0],[10,6]],[[6,5,0],5,[3,8,8],[8,3,1]],7]]
[[[[1,0],9,[6,0,8],4,10],[]],[10,10,3,[10],[[2],1,[3,3,0,2,0],2]],[[10,10,[6,9],[0,9,7,9,5],4]],[6]]
[[[8,7,[0,5,1,7,8],6]],[0,1,5,[]],[6,[[8,10,8,8,9],8,[8,6],[3,9,1,7],[]],[[10,7]],[9,5,[0,10,5,3],6,[3,10]]],[8,[9,[],9,[2],[5,4]],[[10,7,2,2],[8,1],[10,3]],9]]
[[[],[7],[[7,1],[],9,9,10],[],[5,[]]],[1,5],[],[]]
[[[7,[7,1],[],[9],[5,5,10]],[[0,0,1,5,1],8,[6,10,8,1,10],10,[3,4,0]],[8,1,2,1],[],[]],[[[9,8,0],[7,7,10,10,9],[7,7],[1,5,2,4]],[],9,[[3,7,8,10],[10,3],[5,6,7,6,6],[4],3],5],[[[4,2,5,2],[],[3,5,5,7,9],3,[3]],[],[1,0,[9,10,8,1],[9]],2]]
[[4,[],8],[],[3,4,5],[[],[6,[1,7,6,9]]]]
[[[[],[]],[[3],4,9,[6,8,10,10,2],6],2,[[4,3,9,6,5],8,[10,3,3,3,9],3],[8,5,[],6]],[],[[],[6],0,[[7],[5,7],7]],[0,[[7,8,0,9,4],6,9,[]],[9,[7],[0,1,1],9]]]
[5,6,0,4]
[5,6,0,4,2]
[[0,[10],3,9,[]],[[10,2,[],1,[10,3]],[5,0,3],8,[9,[0],[10,8],6,7]]]
[[[7]],[9],[0,[]]]
[[[[1],7,2],[[5,6,7,1,3],[],6,1,[0,3,9,0,9]],[[2,4,4,4],8]],[[],[],[8,[10,4,6],[0,10],[3,1,2,4,3]]],[[[3,10],6,[7,5,8,9,1],8,[1]],[],7,[[]]],[[],[],[],8],[[1,[6],[8,7,4],3],2]]
[[10,7,[10,0,[7,5,6,0],3,[0]],0],[[],8,[[5,3,3,10],[8,9,4,1],[10,8,8,3,2],[10,0,10],[0,7,2]],[10,[5,5,10,1,4]]]]
[[3,2,[],8],[[9,[9,7],[1,3,2]],7]]
[[],[[10],6,1,[]],[[[7]]]]
[[0,2,[10,[1,8,6],[],[8]],2],[1],[],[[[]],[[9,7,4]],10,0,10]]
[[[[3,6,5,1,10],[6]]],[[[3],7,4,9,7],[],2,6],[5,4,[[9,8,4,1],0,5,[10,6,2,6,7],3],[7,[1,9,7,10],[10,4],1,[4,10,2]],0]]
[[],[4,0,5,[[],[8,9,6,9,4]]],[4,[7,[8,2,4,10,8],1],2,4,8],[[],7,[9,5,[5,3,4,2],[],5]],[7,5]]
[[2,[[6,6,2,4],[],[1],9]]]
[[],[[[4,9,2,8,8],[6,4,3,9,0]],[8,5,5],0,[8,[10,5,5,2],[7,0,1,3,5],[2,2,4,5,10]]],[5,6,[2,10,1,[4]],10]]
[[8,6,[7,5],[[3,1],9]],[[10,8],[],[]],[[7,[5,8,10,1],8,3],[[2,5]],[[9,9,10,5],[2,1]]],[],[]]
[[[1,4,5,[9],[7,0]],2,1,[[5,2],[5,6],[]]]]
[[[9]],[0,2,[0,[9]]]]
[[2,4],[[[4],[],7,2],2],[3,[[4,6,7,10],1,[10,3,8,3],3],10,[7,2,[1],7]],[7,1],[3,5,8,[10],10]]
[[[]],[8,5,[1,[2,6],4,8,[5,3,7,3]]],[[7,[8,4],[]]],[0],[[8,[10,1,6,3,3],[10,9]],[2,[],9,10],2,5]]
[[8,[1,10,[4,10,10],[1,7,0,1,3],8],[[8],[9,0],10,9,1],[[9,5,1,3],[7,9,2,6,5]],4]]
[[],[9,[0,[8,2,8,1,3],[]]],[[[2,8,3,10,2]],9,[5]],[],[[]]]
[[3,1,0]]
[[[0,[6,8],[2],[]],7,[[0,1,5,0]],[[1,2,3,8]],[5,5,8]],[5],[[[6,3,5],0,[2,1,5,10],[5,10,6,5,2]]],[[[1,2,0,5],[]],7,4,[6,8,2],3]]
[[[],3,9,0],[[4,8,[5,10],[9,6,6],0]]]
[[3,[[1,2,3],0],[[4,9,2]]],[],[],[7,4],[]]
[[[4],10],[7,[[],[5,1,2],7],5,[9,3]],[8,[],[]],[10,6,5,3,2]]
[[],[[],[[2,1,9],[1,5,4,6],[4,4],8,[]]],[],[7,[0,[9,8],2,[],8],9]]
[[4,2,7],[],[6,3,[[]],[],[4,5,[9,9,1],[4,3,9]]],[[[9,1],[7,7],3,[4,6,3,0]],[4,1],[]],[2]]
[[],[],[[[6,6,5],[8,4],10,[2,6,9,2,10]],0,[[2,7,0,2,6],0,[]],2],[[[]]]]
[[2,[10],[[0,6],[9],[8,7]],[]],[[5]],[5],[],[]]
[[[7,[8,9]]],[8]]
[[5,10],[],[]]
[[[10,7],[[1],8,[6,8,2,8],8],[5,[]],0,[]],[]]
[[[9,5,9,[10,7],8],[3,8,[1,10,10],3]],[[0,7,[7,7,9,6],5,8],9],[],[7,[],0]]
[[],[],[5,[],[[5,7],3,8,[0]],8],[1],[[5,[5]],[[0,0,8],8,[4],[9,10],2],[4,3],[[]],[10]]]
[[0,10],[[[2]],5,3]]
[[[3,3,[10],[2,4,4,6],[6]],3],[[8,[4]]],[4,[5,[7,4,5,4,1],5],[[],[3,9,5],8],[],[10,[4,10,1],4]],[[8,5],4],[2,[3]]]
[[1,[[],0,3,[],[6,10]],[9,[10,6,5,8],[0,10,2],4],[4,[],0,4,3],[7,[2,4,8]]]]
[[5,8],[[[3,4,5],[3],9,[1,7,0,3,10]],2]]
[[[],4],[[[8,8,3,3,10],[6,8],[2,0,7],[],[9,3,3,1]],[],[[10,1,3]]],[[[5,0],8,[4,9,3,0]],4],[[],[[0,2,8,2,0],8,5],[[2],8,[],[0,9],[0,0,5]],[2,2,[4]]],[[[6,6,5,4],[8,6,0],4,8]]]
[[1],[10,9,[[1],0,[10,0,3,2,6]],3,10],[[[3,9,3,8,4],[6,2,8,0]],2,[[2,4,10,6,5]],1]]
[[9,[0,[7,0,2],[],6],[[2,7],0,[10,3,10],4,4]],[[[],[7,6,1],5],6,4],[2],[6,7,10],[8,[[3,6,1,6],0,5,5],9]]
[[[7,8,[10,3,3]],[[9,7,4]]],[[0,7,[]],6],[2],[],[4]]
[[[[1,1],[5],[7,9,8],[7,4,10,0,4]],[[4],[4,9,5],5]]]
[[[],[1,9,[7],10,[4]],[[],[0,2],5,[0],4],8,8]]
[[[10,[5,5,8,2],[],[]],[0,5,[0,6,6,1,1],[8],0],[]],[5,5,1],[[3,[6,2,0],[],8],[[9,3,4,5,6]],[[10,4,9],[9,1],2,[0],10],[[3],[5,10],[10,0,7],[5,8],10]],[[[],[8],[10,6,0]],2,10,2,[3,[6,5,4],1]],[5,[[6],[5,4,0],[7,0]],[[],7,[8,3,4,8],[1,3],3]]]
[[8,0,2],[[[4,7,10,6,3],[7,7]],2,1,8,7]]
[[0,[],[[2],[]],[[3,3,5],1,1]],[[[1,3,9],[3,7,8,4],1],[7],3,2,3]]
[[10,0,3,[6,9],[3]],[[[6,2,1,7],[7,10,1,8,4]],[[],[],[7,10],9,4]],[[[]]],[10,3,[],7,3],[[],[[2,8]],[0,3,8,1,5],[[4,1,7,7]],0]]
[[6,[4,5],[[7,0,9,10],0,5,1,9],9,[6,[10,10,8]]]]
[[[2,0,3,[7,2,10,8],0],5,[[0],[3],[7,4,10]]],[1],[[8,1,[7,10,10,8]]],[[7,[],2,2]]]
[[],[9,[1,2,[7,0,9]]],[[9,10,[4,3,10],0,[2]],2,6,[10,0,8],5],[[10,[],3,[]],4,[],3,[10,0,8,9]],[[9,7,0,[9],4]]]
[[[[9,2,1,10,0]],6,[[3],2],[8,9]],[6,10,3],[[[]],1]]
[[],[5,10,[[],1,[],10,[6,1]]],[[10,5],[[]],0,10],[9],[[5]]]
[[[6,[0,2,1,4,5]],3],[[3],[[1],[9,3,1,1,4],[10,7,4]],7,2],[],[8,[[3]]]]
[[[[8],9],[2],0],[[[8],9,8,2],4],[9,3,[0,4,7],2,4],[]]
[[],[3,[1,5,2,[0,8,5,2,4],5],[[10,0,0,1,2]]],[[[],5],[10,[9],9,8],[9,4],[[2,6],[6,5,7,8]]],[],[5]]
[[1],[9]]
[[],[[10,[],[1,7]],8,10],[8,[[9,7,9,10],6],3,1]]
[[[8],[[10,4],[2,0,1,5,9],1,9,[5,0,1]]],[9,[8,2,8,[3,1]],2],[9,3,[7,10,[1,9],[]],[0,6,10]]]
[[1,1,2,8,[[1]]],[[[8],10,[0,9,3,6]],10,[0,6,[1,2,1,1]]],[],[[[8,9,1],0,2,9,[2,5,8]],[[0,0,4],[]],[[10,10,0,5,7],[],[6,5,9,6],5]]]
[[[[2,5,2,10,2],[3],6],[],6],[4,0,8]]
[[0,[9,0,2]],[[[7,0,7,1,4]],[],3,[[4],4]],[7]]
[[[],[[0,6,1,3,8],[8,0,0,9,1]],[10,5,[7],1],[[8],[],4,1,[4,5,8,9,3]],9],[[],[5,[9],[],[7,1,7,6,3]],6,[[2,8,4,9,10],4,[1],10],4],[[0],[],[4,9,5,[],[2,0,1]],[0,8],[[1,6,2,0,2],10,[2,1,3,4],3]]]
[[[4,6,[8,9,9,3]],[4,[],[],[1,6,4]]],[10,[[0],[6],[3,5],4],7,4],[2,2,[[],[6,9,7,7],2],8],[3,[],[9],2],[6,10,[0,[0,6,10,8,9],2],[7,[]]]]
[[[1,[9,3,6,2],7,1],[2,[5,0,2,2],10,[2]]],[],[[8,[1],3,1]]]
[[[]],[[7,[0,9,10,9]],[[10,8,7],9,2,10],[8,4,10,[3,9]]],[[[0,6,5,5,8],8],[[7,7],[9,2,3],7,3],[5,9,3]]]
[[[6,[10],[2],[0,5,6,10,7]],[5,[1,3,1],[1,4,0],8],1,0],[[[0,0,10,4,8]],7,3,[[5,0,0,9]],[1,[5,0],8,5,[]]],[],[9,[[1,7,7,0,10],4]]]
[[1,0],[2,[5],4,[[1,9,4,6,9]],7],[[],10,5]]
[[2,6,10,0],[[[0],[3,9,9,0]],[[6],8,10,[9,10]]],[1,[0,2,2],[],1,[[],[8,2,0],4,6,2]],[[6,[2,0],[],1,7],[2,[],[7,3,3,3,8]],[4,9,0,9],[5,10,[]]],[5]]
[[9],[9,[],10,3,7],[[[9,6,6,2,8],[]],2,[9,9,[8,8,10,4,4],0],[9,[0]],9],[[4,[4,7,8,6],10],[]]]
[[],[],[[[0,2],[8,1,7],[7,5],10],5,6,[],2],[[[2],6]],[5]]
[[[[1,9,9]]],[6,8],[[3],[2,8]],[[0],5,8,4]]
[[[],[6,2,9,10],7,[[7,7,6],[6,0,9],10]],[[[0]],[9,10,9,[5,5,8,0,2]],10]]
[[[5,[4,9,1,7,1],[4,1],0,[3,7]],4],[[10]],[2,[3],[[4,9,10],[],5],[8,6,9,6]]]
[[0,8,[6,[7,4,10,9],0],[[4,3,5,10],10,[7,0,5],5,2]],[[2,[8,4,6,5],[8]],[[9,1,10,1,6],8,7,[3,7],3],[[2,5,2],7,[],2]],[],[4,[8,10],[2,6,8],[[9,4,1,4],10,6],[1]],[[6],2]]
[[8]]
[[0,0]]
[[[],[[4,9,2,6],[],[10,1,7,0,1]],7],[3],[],[[[],[3,2],[4,8]]]]
[[],[],[[0,[7,9,5,8],6],8,[0],1,4]]
[[9,7,4],[[]]]
[[[[7,4,0],[5],[0,8,0],[0,8,9,6],[3,4,8,10]]],[[[0,9,9,4],8]],[[4],[[6,5,10],[10,5,2,9,4]],8]]
[[[6],[7,7,6,[8,0]],4],[]]
[[],[4,[[7,3,7,6],[10,1,0,5,0],8],7,[4,[8],2,[]],[[]]],[[[4,2,0],[4,1,8,6,1],10],7],[9,[1,[2,9,3,0,0]],5],[9]]
[[[[],0,2,3,1],[[],8,6,2,[]],4],[4,[],2,[4,[]]],[]]
[[6,9],[]]
[[[10]],[[[2,8],9,[6,5],7,2],6,6],[[3,[5]]],[],[5,[[]]]]
[[[],0,5,9],[10,0,4,0],[6],[],[]]
[[],[[1]],[7],[1,1,[[7,4,2],6,[8,4,9]],[[10,7,4,0,0],9,2]]]
[[8,10],[3,[0,[5],[6],[1,10,6,2]],10,1],[],[[],[6,7],6],[]]
[[7,6,[],5],[],[],[7]]
[[],[9],[[5,1,4,[10,3,4,5,0],5],[1,5,6],[[4,2],2,6,[9,9]],6],[[],3,[8,2,5,[10,10,9,5],[2,9]],[[1,5,8,7,3]],[]]]
[[3,7,2,[[4,0,2,1,9]]],[3,1,1],[10,[],[1,5,4,[5],1]],[],[10,2,3,6]]
[[[8,10,[1,0,1,9]],[[1,1,3,2,7],1,[4,4,6,6]],7,4,2],[3,[[4,8,5,9]],[4,[2,6,4,1,2],9],6,7]]
[[[[6],0,[7,6,4,3],[8,1,1,2,5]],8,7,[9,10,7]],[9]]
[[[],5],[[],[],8,7]]
[[[],8],[3,2,4]]
[[],[1,6,5,[[0,5],[0],[]],3]]
[[2,[[2,8,8,7,4],[6,9,4],[],[2,10,9],1],5],[[],[[9],2,7]],[[1,0,[1,6,3,6,5],[10,2],6]],[[[6],[2,7,6,3],6,2,7],10,[[],[8]],2,[3,[4,1,1],3,[7,0],3]],[[[0,10,5]],5,10]]
[[[[],6,9],8,[8,2,4],3],[],[9,[],4,7,3],[[2,5,4,9,[8,5,6,1,6]],[[],10,5,10]]]
[[3,[[2,1,7,7,9],[0,3,4,10],[0,1,1,7,8],10],1,[3]],[9,4,[[],9,[],[7],[0]]],[1,3,6,3,4],[[3,3],5]]
[[6,3],[[]]]
[[[],6,[[9],[1,7,0,8,4],[9,3,0,8],[4],1],8,7],[7,[2,2,2,[5,7,8,4]]],[]]
[[[],6]]
[[[[6,7],9,[8,7,9],5],10,3],[[6,[9],9,6]],[],[],[10,[1],[[9,1,10,0]]]]
[[[[3,5,5,0],3,8,9]]]
[[[9,[10,4,9]]]]
[[5,0,9,6],[9,[[3],10,9,3],[8]],[],[[[9,4,2,8],[8,10,2],4,[],0],[2,[]]],[[],6,[5,5,[9]],4]]
[[],[],[]]
[[[4,5,[]]],[[[],[0,9],6,3],3],[10,[4],5,8],[[4,5,2,10,5]]]
[[[0],3,[4,[10,1,1,4],8,[8,1],[4,0,5,4]],[[5],8,[9]]],[[0,[],[2,7]]]]
[[[4,[8],[2,3,5],[10,4,9,9,3]]],[9,[0,1,[6,0,2]],6,[[],4,7,0,[]]]]
[[[2,7,[],[10,1,4,7,9],0],[],7],[10,[0,0,9,[9,5,3,6,10],[]]],[9,7,[9,10]]]
[[[0,10,[1],3,[4,1,7,6,9]],2,3,6,[[1],[0,2,3],[9]]],[[[],[1]],2],[10,7,[5]],[]]
[[],[9,6,4,4,4],[[[0,4,7,7,10],3],0,[[6,3,5],[]],[[8,6,10,7,0],[8,3,7],0,3],[[4,9,8,4,4],3,[4],[7,5,1,9],2]],[[8,[10,2,8],[10,4,0,0],0,[7]],7,[[7,3,8,2],10,10,[2],[9,10,10,5,5]]]]
[[[],10,[8,[1,2,5,1],[2,7,6,7,5]],[9,[9,3,9],[4,8],5,6],1],[[],[[7,8,6,4],[8,8,7,5,3],6,4,[8,1,7]],1],[5,4,[]]]
[[],[7],[[2]],[],[[]]]
[[5,8]]
[[7,[],7,[]],[]]
[[[1,8]],[0,6],[9,6,[[]]],[5,[[0]]]]
[[[[5,6,1]],5],[],[2,[],9,[[]],[7,10,[7,9],[]]],[3]]
[[1,10,3],[7,9],[[[1],[6,9]],0],[4,[[8,4],[8,0,3,0,10],[0,6,2,8,10],4,3],3,7]]
[[[5,[8,4],[6,0,9],0],3,[[8],[10,9,6],[],[10,3,2,0,0]],7,2],[1,9,4,[9,[7,6],8,[3,1],4]],[4,[6],[[2,7,3],[9,6,0],[8,1],[7,4]],[[1,4,0,2,8],7,2,4]],[[[6,3,1,9,8]]],[]]
[[[[10,8,1,5]],[[3,3,5]],5,4],[2,[[],[6,7,2,7,7],5,10],4]]
[[[[0],[],[]],5,9,[6]],[],[]]
[[8,10,[10,8]],[],[[[4,0,6,3,2]]],[[[5,9],[5,8],2],0],[8]]
[[[[4,6,8],6,2,5],6,7],[],[[[8,3,5],4,[9,10,8,1]],[[8,5,4],6],[[8,10,6,8],2,[0],[]]]]
[[[[3,5],[9,5,9,1],10,[0,2,6,9,9],3],10,7,3,6],[],[[0,[9],10,0],9,[],[[4,8],[6,5,2]],2]]
[[[[0,0],[6],10],10,[4,3],9,2]]
[[[[],0,[8,3],[1,9,0,1,6]],9]]
[[8,2,[[4],0],1],[[]],[0,0,9]]
[[],[6,[]],[3,[[0,0,6,0],[],[5,4,8],[7,0,10,3,0],0],[[],[10,3],6,1],2]]
[[],[[[9],[8,0,10,0,9],[2,7,3],[3,2]],5,[]],[10,[8,[6,3,0,8,0],9]],[3]]
[[3,[0],[10,6,[9,5,4],[1,6,5,10]],9,9],[],[[10],9,[[],10,[10,4,6,0],[6,8],[7,7]],3,10],[[1,7,[5,9,9],[],9],6,7]]
[[6,2],[[[],4,6]]]
[[10,6,[],[[3,8,7,7,7],[7],[5],7],4],[6,[],[0,5,1,4,2],8]]
[[],[4]]
[[[[3,3]],1,[0,8,[6,10,7,5,8],[1,1,1,10]],5],[[[5,10]],[7,[7,2]],[10,7,8,[4],6],6,[[4]]],[[5,[0],0,[10,5,10],1],[10,2,9,[5,7,10,0],[10,4,10,6,1]],9]]
[[9,[[7,9,9],[5,8,9,8],2,[6]]],[],[1,1,[2,3]]]
[0,3,7,3,4]
[0,3,7,3]
[[4,2,9,[5,6]],[],[],[]]
[[4,4],[9]]
[[[],[[6,0,7],0],[4,0,2,3]],[[6,5,[10,0,9,9]],[]],[],[[0,0,[8,9,9,0],[6]],1]]
[[10,6,5]]
[[3],[]]
[[6,7,[3,[4,4],5,0]],[9,10],[[[8,6,5,0,1],6,1]],[[],[0,10],10,[10,[1,8,1],[]],[[8],[7,2,10,6,2],6]],[[[1,4,8,8,0],5],6,[],2,[[2,5],3]]]
[[[],2,7],[[],[3,[8,7],[3,6,10],8,[9,4]],1,3,6]]
[[0],[[6,[7]],[[6],[8],9,7,[9]],[[]],[[0],[7,0],[],5,1],[4,[9],[10]]],[[8,[],2,[8,1,7,4]],[[],[],[10,4,7,6],2,2]],[4],[[[0,3,7],5],[[9,0,10,7,5],7]]]
[[],[[[],[0,8,3]],[1,[9,0,5,10,7],9,1,5],[[1,1,2],[5],1,8,0]],[4]]
[[2,[[6],4,1,8],7,0],[[0,[1,5],2],[[4,9,5,9,1],4,2,0]],[[[]]],[10,7,[[],2]],[[4,[6,8,8,7]],[[3,1,9,1,7],7,5,10,[3,5]],[2]]]
[[3,[[0,0,6,2,4],[],8]],[[8,10,9,7,[8,3,5,6,4]],[[8,10,8,2],[4,6,5,0],1,4,[4,6,9,0,6]],[[7,6],[1,2,4],3,8,1],[6,[8,9],[]]],[1,1]]
[[9,[[2,2,3]],4,[1,0]],[7,10,4,[[9,5,2,3],[7,3],[5,6,2,8],[8,4,10],8],0]]
[[[8,[1,9],2,[2,9,3,8,7],[1,7,8,4,2]]],[4]]
[[0,4],[],[4,[],[[]],[[8,5,7,9]]],[[2],4,[5,6,[0,8,8,1],[7,4,9],8]],[1,[2,[9,5]],[0,[8]]]]
[[6,[[6],3,2,[10,6,7,6],0],10],[10],[[3],6,7],[[[10,1],[3,3,1,5,2]],7,[0,[2,10]],[1,[1],1,5,7],[]],[7,[],[6,[3,2,0,8],[7],[0,9,9,4,9]],[[2],[1,8,6,6],[1,6,8,5,1],[0]],[5,6,[2,7,10],[4,2,4],[10,9]]]]
[[[3,[1,5,3,5],6,[0,0]],[0],4],[8,[[9],8,8,4],[],1],[[5,[0,8,6,6,5],0,[9,10,1]]]]
[[[],10,[[0,6,10,0],3,[2,1],[7,4,2,8],[6,1]]],[7,8,8,6],[[9,2,6,4,1],[[7,3,8,9,0],7,3],[[],[0,10,5,9]],[[4,1,0,2]]],[[6]]]
[[9,0]]
[[4],[5]]
[[[[3,8,10],10],9,[7,[0,0,9,5,1],4],[]],[1,[1,5,0,[10,5,9,0],0],8,1,[[7],1,4]]]
[[5,[[2],[2,10,1,10,1]],[],3],[],[[10],0],[6,5,1],[[[10,5,4,10],[],[8,6,0,5],3,2]]]
[[],[6],[[3,1,[4,6]],[[8,9,7],[3,0],5],0,9],[[2,[7,9,2],[2]],[[6,8,4,10],8,3,1,[5,7,8]],6,[4]],[[6,[5]],[[1,7,9],10,[3,9,0,4,3],[7,7,8,3]],4,[0,9,2,2],9]]
[[[2,[],[8,10,7],[0,1]],7,[8],[[8,3,3]]]]
[[3],[6,[[3,6],3,[4,4,6,2]],4,7,[]],[[10,4,5,[2,8,4,5],4],[1],[[4,5,2,0,6]],[[],8,5],[6,[4,9,10],1,4]],[[7],[3,[9,10,2,1],5,3],10]]
[[8,2,[[0,5],5,10]]]
[[1,[],[7,7,4,2,[4]],8],[[],5,4,[[2,6,2],[8],0],[]],[5,8,8,9],[[[8,8,3,5],[7,10],2,[0,1,6],8],[0,[],3,[8,7,10,2]],[8,8,[4,1],2,[8,4,9]]]]
[[[5,[10,10],[9,6],[],9],1]]
[[],[7,[]],[],[9]]
[[2,7],[3,[[10],[6,3,8,4,1],5],1]]
[[],[[],1,3],[6,1,[5,[2,2,8],[3,5,5,7],6,[6,7,9,10]]],[[5,[],[5,3,0,0],7],[]]]
[[[8,[1,8,5],8,[7]],1,10,7],[[0,6,2]],[[[],9,1,[1,4,3]],9],[0,9,[[3],[9,2,2,6,4]],[[]],[0,3,[1]]],[[[2,10,7],2],[[8,7,10,5,1],10,1,7],4]]
[[],[[[4,3,6,8,9],9],10,6],[4,3,[[2,1,4],[10,5,9,6]],0,[[8],10,7,1]]]
[[1],[[[4,2,10,3,5],9,[],0,[9]]],[[3,5,[],[6,9,4]],[7,5,[7,7,5,8,8]],[]],[[1,[1,7,2,1,2],3,[0,4,9,8,2]]],[]]
[[[5,[1,0,1,9,0],[8,6,2,7],5],[7,3,4],[[10,2,6,9],[10,9],[1],[3,5,6],7],[[]]],[2,[],10,7]]
[[[10],[4,[4,10,1],3],[[0,8,10,6],[],9,3],[[7],[9,2,5],[],4,[]],7],[[[2,7,10,10,9]],[[10],1]],[8,[],[[2,7,2,4],2,[0],0],2]]
[[3,[8,[0,8,0,8,1],[1,5,5,0,8]],0,8,[[6,7,7],[0,0,9,10,10],2,5,6]],[4,[8],[8,[],9,[4,9,7,10,8],9],[2,6,2,[3,7]],7],[[0],[7,[4,5],[1,5,0]],7,[[9,0],[1,1],2,6]],[6,[3,[9],[2,5,3,5],1],[]],[]]
[[[[10,8]],[[7],[7,1,3],[9,6,3,6,8]],[[6],[0,4],3,8,[10,1,9]],4],[[[7],[4,7,5,0,3]],8,10]]
[[3,1,[]],[[1,[2,10,9,10],[],[2,7,10]]],[[[],10,8,8]],[5,8,[6]],[]]
[[[[],0,[3,2,2,2],10]],[[5,0]]]
[[9,[],4,[[2,0,0,8],[7,4]]],[[1],2],[[]],[[[],7],4,0]]
[[[8,3,[1,4,4,4],[9,1,4,7,0]],[2,[6,1,6,5,3],10,2,8],[0,9,[10,3,1,4]]],[[4,5],0,4,5,[[3,4,4,3],[1,0,10],9,5]],[],[4]]
[[[[2,9,1],[],4,7]],[]]
[[[0,3,3,[],[10,0,8,2]],1]]
[[[[4,1,3,9,0],[7,0,3],3],[],[9],[[7],7,[6,1,4,6,5]],[0]],[10],[7,4,2,8,6]]
[[[[0,9,3,7,1],2],[[10,2,2,4,6],5],10,0,5],[4,[]],[[[],2],[],[[6,6],8,4,[10,8]],[[5,1,2,4],4,[9],7]],[[[9],7,2,[6,0,9,8],[4,1]],5,[],[5,8,[5,2,7,6,5]],[[2]]]]
[[],[2,[7,0,[0],[0,1,2,1,6]],2,6]]
[[[[5,0,9,4,6],8,2],1,0,[[9,4,3],[10,5,0,1],[]],0],[],[[[],9,4],[[2,8,3],6,0,[7,7,5]],[1,[10],8,[]]],[4,2,[4,5,[6,3,10,1,6],[10,3]]],[[[1,2],[0,1],7]]]
[[5,9],[6,[9],[1,[9,8,9,6],9,6,[1,5,7,6]],7,[3,[4,7,2],6,[5,9,4,10],[]]]]
[[[[0,7,7,1,8],[2,1,9]],0,2,6,3],[5,[2,[10],6]]]
[[7,[],[[1],[4,6]],7,[9,[1],9]]]
[[],[],[6,4,8],[0,[]],[[7,[]],5]]
[[],[[4,2],0,[2]],[[[3,9],[1,0,1],6],7,[[2,4,9,5],9,[4,8,6],10,[2,2,8,4]]],[0,4]]

119
2022/day13/src/main.rs Normal file
View File

@@ -0,0 +1,119 @@
#![warn(clippy::all, clippy::pedantic)]
use std::{
cmp::Ordering::{Equal, Greater, Less},
fs,
};
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{self, newline},
multi::{separated_list0, separated_list1},
sequence::{delimited, separated_pair},
Parser,
};
#[derive(Debug, Eq)]
pub enum Checker {
Num(u32),
Array(Vec<Checker>),
}
impl Checker {
/// # Errors
///
/// returns an `nom::err::Error<&str>` if there is problems parsing
pub fn parse(input: &str) -> nom::IResult<&str, Self, nom::error::Error<&str>> {
alt((
complete::u32.map(Self::Num),
delimited(tag("["), separated_list0(tag(","), Self::parse), tag("]")).map(Self::Array),
))(input)
}
}
impl PartialEq for Checker {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Num(l0), Self::Num(r0)) => l0 == r0,
(Self::Array(l0), Self::Array(r0)) => l0 == r0,
(Self::Num(l0), Self::Array(r0)) => &vec![Self::Num(*l0)] == r0,
(Self::Array(l0), Self::Num(r0)) => l0 == &vec![Self::Num(*r0)],
}
}
}
impl PartialOrd for Checker {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Checker {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {
(Self::Array(a), Self::Array(b)) => a.cmp(b),
(Self::Num(a), Self::Num(b)) => a.cmp(b),
(Self::Array(a), Self::Num(b)) => a.cmp(&vec![Self::Num(*b)]),
(Self::Num(a), Self::Array(b)) => vec![Self::Num(*a)].cmp(b),
}
}
}
pub struct Together {
pub left: Checker,
pub right: Checker,
}
impl Together {
/// # Errors
///
/// returns an `nom::err::Error<&str>` if there is problems parsing
pub fn parse(input: &str) -> nom::IResult<&str, Self, nom::error::Error<&str>> {
separated_pair(Checker::parse, newline, Checker::parse)(input)
.map(|(input, (left, right))| (input, Self { left, right }))
}
}
/// # Errors
///
/// returns an `nom::err::Error<&str>` if there is problems parsing
pub fn parse_data(input: &str) -> nom::IResult<&str, Vec<Together>, nom::error::Error<&str>> {
separated_list1(tag("\n\n"), Together::parse)(input)
}
fn main() {
let file = fs::read_to_string("./input.txt").unwrap();
let (_, all) = parse_data(&file).unwrap();
let part1 = all
.iter()
.enumerate()
.filter_map(|(i, Together { left, right })| match left.cmp(right) {
Less => Some(i + 1),
Equal => panic!(),
Greater => None,
})
.sum::<usize>();
let two = Checker::Array(vec![Checker::Array(vec![Checker::Num(2)])]);
let six = Checker::Array(vec![Checker::Array(vec![Checker::Num(6)])]);
let mut p2 = all
.iter()
.flat_map(|Together { left, right }| [left, right])
.chain([&two, &six])
.collect::<Vec<_>>();
p2.sort();
let i2 = p2
.iter()
.enumerate()
.find_map(|(i, b)| if *b == &two { Some(i + 1) } else { None })
.unwrap();
let i6 = p2
.iter()
.enumerate()
.find_map(|(i, b)| if *b == &six { Some(i + 1) } else { None })
.unwrap();
let part2 = i2 * i6;
println!("Part 1: {part1}");
println!("Part 2: {part2}");
}

11
2022/day14/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "day14"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true

149
2022/day14/input.txt Normal file
View File

@@ -0,0 +1,149 @@
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
446,83 -> 450,83
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
466,59 -> 470,59
420,134 -> 420,135 -> 429,135 -> 429,134
449,79 -> 449,80 -> 463,80
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
469,61 -> 473,61
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
472,63 -> 476,63
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
431,94 -> 435,94
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
428,102 -> 432,102
437,94 -> 441,94
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
452,87 -> 456,87
425,99 -> 429,99
475,57 -> 479,57
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
469,57 -> 473,57
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
449,85 -> 453,85
420,134 -> 420,135 -> 429,135 -> 429,134
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
440,87 -> 444,87
478,32 -> 478,33 -> 490,33 -> 490,32
443,94 -> 447,94
431,105 -> 435,105
481,61 -> 485,61
460,63 -> 464,63
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
440,96 -> 444,96
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
434,92 -> 438,92
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
434,96 -> 438,96
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
475,61 -> 479,61
422,102 -> 426,102
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
420,134 -> 420,135 -> 429,135 -> 429,134
478,63 -> 482,63
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
425,105 -> 429,105
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
471,38 -> 471,39 -> 480,39
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
446,87 -> 450,87
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
440,92 -> 444,92
449,79 -> 449,80 -> 463,80
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
429,138 -> 429,140 -> 428,140 -> 428,146 -> 436,146 -> 436,140 -> 433,140 -> 433,138
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
446,96 -> 450,96
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
471,38 -> 471,39 -> 480,39
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
456,66 -> 456,70 -> 455,70 -> 455,77 -> 466,77 -> 466,70 -> 462,70 -> 462,66
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
466,63 -> 470,63
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
443,85 -> 447,85
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
478,32 -> 478,33 -> 490,33 -> 490,32
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
472,55 -> 476,55
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
419,105 -> 423,105
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
437,90 -> 441,90
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
478,32 -> 478,33 -> 490,33 -> 490,32
472,59 -> 476,59
463,61 -> 467,61
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13
428,96 -> 432,96
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
421,131 -> 421,127 -> 421,131 -> 423,131 -> 423,124 -> 423,131 -> 425,131 -> 425,130 -> 425,131 -> 427,131 -> 427,128 -> 427,131
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
478,59 -> 482,59
410,118 -> 410,108 -> 410,118 -> 412,118 -> 412,114 -> 412,118 -> 414,118 -> 414,115 -> 414,118 -> 416,118 -> 416,108 -> 416,118 -> 418,118 -> 418,112 -> 418,118 -> 420,118 -> 420,116 -> 420,118 -> 422,118 -> 422,116 -> 422,118 -> 424,118 -> 424,108 -> 424,118
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
494,23 -> 494,26 -> 486,26 -> 486,29 -> 502,29 -> 502,26 -> 496,26 -> 496,23
419,159 -> 419,156 -> 419,159 -> 421,159 -> 421,158 -> 421,159 -> 423,159 -> 423,152 -> 423,159 -> 425,159 -> 425,155 -> 425,159 -> 427,159 -> 427,156 -> 427,159 -> 429,159 -> 429,153 -> 429,159 -> 431,159 -> 431,155 -> 431,159 -> 433,159 -> 433,153 -> 433,159
475,52 -> 475,50 -> 475,52 -> 477,52 -> 477,46 -> 477,52 -> 479,52 -> 479,49 -> 479,52 -> 481,52 -> 481,49 -> 481,52 -> 483,52 -> 483,42 -> 483,52 -> 485,52 -> 485,47 -> 485,52
484,63 -> 488,63
497,13 -> 497,16 -> 496,16 -> 496,20 -> 508,20 -> 508,16 -> 503,16 -> 503,13

147
2022/day14/src/main.rs Normal file
View File

@@ -0,0 +1,147 @@
#![warn(clippy::all, clippy::pedantic)]
use std::{collections::HashMap, fmt::Display, fs};
use nom::{
bytes::complete::tag,
character::complete::{self, newline},
multi::separated_list0,
sequence::separated_pair,
};
#[derive(Debug, Clone, Copy)]
enum Square {
Air,
Wall,
Sand,
}
impl Square {
pub fn is_sand(self) -> bool {
matches!(self, Self::Sand)
}
pub fn is_rock(self) -> bool {
matches!(self, Self::Wall)
}
}
impl Default for Square {
fn default() -> Self {
Self::Air
}
}
impl Display for Square {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = match self {
Self::Air => '.',
Self::Wall => '#',
Self::Sand => 'o',
};
write!(f, "{display}")
}
}
fn rock_wall(input: &str) -> nom::IResult<&str, HashMap<(usize, usize), Square>> {
let (input, pts) = separated_list0(
tag(" -> "),
separated_pair(complete::u64, tag(","), complete::u64),
)(input)?;
let mut rocks = HashMap::new();
pts.iter()
.enumerate()
.take(pts.len() - 1)
.flat_map(|(i, start)| {
let start = (
usize::try_from(start.0).unwrap(),
usize::try_from(start.1).unwrap(),
);
let (ex, ey) = pts.get(i + 1).unwrap();
let end = (usize::try_from(*ex).unwrap(), usize::try_from(*ey).unwrap());
if start.0 == end.0 {
let (s, e) = if start.1 < end.1 {
(start.1, end.1)
} else {
(end.1, start.1)
};
(s..=e).map(|y| (start.0, y)).collect::<Vec<_>>()
} else if start.1 == end.1 {
let (s, e) = if start.0 < end.0 {
(start.0, end.0)
} else {
(end.0, start.0)
};
(s..=e).map(|x| (x, start.1)).collect::<Vec<_>>()
} else {
panic!("wee wooo");
}
})
.for_each(|pt| {
rocks.insert(pt, Square::Wall);
});
Ok((input, rocks))
}
fn parse_input(input: &str) -> nom::IResult<&str, HashMap<(usize, usize), Square>> {
let (input, walls) = separated_list0(newline, rock_wall)(input)?;
let walls = walls
.iter()
.flatten()
.map(|(pt, s)| (*pt, *s))
.collect::<HashMap<(usize, usize), Square>>();
Ok((input, walls))
}
fn main() {
let file = fs::read_to_string("./input.txt").unwrap();
let (_, mut board) = parse_input(&file).unwrap();
//insert rocks
//get lowest rock
let lowest = board
.iter()
.filter_map(|((_, y), typ)| if typ.is_rock() { Some(y) } else { None })
.max()
.unwrap()
.to_owned();
while board.get(&(500_usize, 0_usize)).is_none() {
let mut square = (500_usize, 0_usize);
//drop - find the place to insert
while square.1 < lowest {
let checks = [
(square.0, square.1 + 1),
(square.0 - 1, square.1 + 1),
(square.0 + 1, square.1 + 1),
];
square = if let Some(new) = checks.iter().find(|pos| board.get(pos).is_none()) {
*new
} else {
break;
};
}
if square.1 >= lowest {
break;
}
board.insert(square, Square::Sand);
}
let part1 = board.values().filter(|x| x.is_sand()).count();
println!("Part 1: {part1}");
while board.get(&(500_usize, 0_usize)).is_none() {
let mut square = (500_usize, 0_usize);
//drop - find the place to insert
while square.1 < lowest + 1 {
let checks = [
(square.0, square.1 + 1),
(square.0 - 1, square.1 + 1),
(square.0 + 1, square.1 + 1),
];
square = if let Some(new) = checks.iter().find(|pos| board.get(pos).is_none()) {
*new
} else {
break;
};
}
board.insert(square, Square::Sand);
}
let part2 = board.values().filter(|x| x.is_sand()).count();
println!("Part 2: {part2}");
}

12
2022/day15/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "day15"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true
itertools.workspace = true

24
2022/day15/input.txt Normal file
View File

@@ -0,0 +1,24 @@
Sensor at x=3291456, y=3143280: closest beacon is at x=3008934, y=2768339
Sensor at x=3807352, y=3409566: closest beacon is at x=3730410, y=3774311
Sensor at x=1953670, y=1674873: closest beacon is at x=2528182, y=2000000
Sensor at x=2820269, y=2810878: closest beacon is at x=2796608, y=2942369
Sensor at x=3773264, y=3992829: closest beacon is at x=3730410, y=3774311
Sensor at x=2913793, y=2629579: closest beacon is at x=3008934, y=2768339
Sensor at x=1224826, y=2484735: closest beacon is at x=2528182, y=2000000
Sensor at x=1866102, y=3047750: closest beacon is at x=1809319, y=3712572
Sensor at x=3123635, y=118421: closest beacon is at x=1453587, y=-207584
Sensor at x=2530789, y=2254773: closest beacon is at x=2528182, y=2000000
Sensor at x=230755, y=3415342: closest beacon is at x=1809319, y=3712572
Sensor at x=846048, y=51145: closest beacon is at x=1453587, y=-207584
Sensor at x=3505756, y=3999126: closest beacon is at x=3730410, y=3774311
Sensor at x=2506301, y=3745758: closest beacon is at x=1809319, y=3712572
Sensor at x=1389843, y=957209: closest beacon is at x=1453587, y=-207584
Sensor at x=3226352, y=3670258: closest beacon is at x=3730410, y=3774311
Sensor at x=3902053, y=3680654: closest beacon is at x=3730410, y=3774311
Sensor at x=2573020, y=3217129: closest beacon is at x=2796608, y=2942369
Sensor at x=3976945, y=3871511: closest beacon is at x=3730410, y=3774311
Sensor at x=107050, y=209321: closest beacon is at x=1453587, y=-207584
Sensor at x=3931251, y=1787536: closest beacon is at x=2528182, y=2000000
Sensor at x=1637093, y=3976664: closest beacon is at x=1809319, y=3712572
Sensor at x=2881987, y=1923522: closest beacon is at x=2528182, y=2000000
Sensor at x=3059723, y=2540501: closest beacon is at x=3008934, y=2768339

266
2022/day15/src/main.rs Normal file
View File

@@ -0,0 +1,266 @@
#![warn(clippy::all, clippy::pedantic)]
use std::{collections::BTreeMap, fmt::Display, fs, ops::RangeInclusive};
use itertools::Itertools;
use nom::{
bytes::complete::tag,
character::complete::{self, newline},
multi::separated_list1,
};
trait Pos {
fn get_pos(self) -> (i64, i64);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sensor {
pub x: i64,
pub y: i64,
pub strength: i64,
}
impl Sensor {
pub fn x_covereage_at_y(&self, y: i64) -> Option<RangeInclusive<i64>> {
let dist = (y - self.y).abs();
let n = self.strength - dist;
if n < 0 {
return None;
}
Some((self.x - n)..=(self.x + n))
}
pub fn coverage(&self) -> Vec<(i64, RangeInclusive<i64>)> {
((self.y - self.strength)..=(self.y + self.strength))
.map(|y| {
let Some(x_s) = self.x_covereage_at_y(y) else {
panic!()
};
(y, x_s)
})
.collect()
}
}
impl Pos for Sensor {
fn get_pos(self) -> (i64, i64) {
(self.x, self.y)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Beacon {
pub x: i64,
pub y: i64,
}
impl Pos for Beacon {
fn get_pos(self) -> (i64, i64) {
(self.x, self.y)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Square {
Covered,
Beacon(Beacon),
Sensor(Sensor),
}
impl Square {
pub fn is_covered(&self) -> bool {
matches!(self, Self::Covered)
}
}
impl Display for Square {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Covered => '#',
Self::Beacon(_) => 'B',
Self::Sensor(_) => 'S',
}
)
}
}
impl From<&Sensor> for Square {
fn from(value: &Sensor) -> Self {
Self::Sensor(*value)
}
}
impl From<Sensor> for Square {
fn from(value: Sensor) -> Self {
Self::Sensor(value)
}
}
impl From<&Beacon> for Square {
fn from(value: &Beacon) -> Self {
Self::Beacon(*value)
}
}
impl From<Beacon> for Square {
fn from(value: Beacon) -> Self {
Self::Beacon(value)
}
}
fn parse_reading(input: &str) -> nom::IResult<&str, (Sensor, Beacon)> {
let (input, _) = tag("Sensor at x=")(input)?;
let (input, x_sensor) = complete::i64(input)?;
let (input, _) = tag(", y=")(input)?;
let (input, y_sensor) = complete::i64(input)?;
let (input, _) = tag(": closest beacon is at x=")(input)?;
let (input, x_beacon) = complete::i64(input)?;
let (input, _) = tag(", y=")(input)?;
let (input, y_beacon) = complete::i64(input)?;
let dist = (x_sensor - x_beacon).abs() + (y_sensor - y_beacon).abs();
Ok((
input,
(
Sensor {
x: x_sensor,
y: y_sensor,
strength: dist,
},
Beacon {
x: x_beacon,
y: y_beacon,
},
),
))
}
fn parse_output(input: &str) -> nom::IResult<&str, Vec<Square>> {
let (input, readings) = separated_list1(newline, parse_reading)(input)?;
let output = readings
.iter()
.flat_map(|(sensor, beacon)| vec![sensor.into(), beacon.into()])
.unique()
.collect();
Ok((input, output))
}
fn coverage_count_at_y(board: &[Square], y: i64) -> Vec<i64> {
board
.iter()
.filter_map(|square| match square {
Square::Sensor(sensor) => Some(sensor),
_ => None,
})
.filter_map(|sensor| sensor.x_covereage_at_y(y))
.flatten()
.unique()
.collect()
}
pub fn part1(board: &[Square], y: i64) -> String {
let pos_covered_on_y = coverage_count_at_y(board, y).len();
let obs_on_y = board
.iter()
.filter_map(|square| match square {
Square::Beacon(beacon) => {
if beacon.y == y {
Some(beacon.x)
} else {
None
}
}
Square::Sensor(sensor) => {
if sensor.y == y {
Some(sensor.x)
} else {
None
}
}
Square::Covered => None,
})
.unique()
.count();
(pos_covered_on_y - obs_on_y).to_string()
}
pub fn part2(board: &[Square], lower: i64, upper: i64) -> String {
let bb = board
.iter()
.filter_map(|square| match square {
Square::Sensor(x) => Some(x),
_ => None,
})
.flat_map(|square| {
square
.coverage()
.into_iter()
.filter(|(y, _)| y >= &lower && y <= &upper)
.map(|(y, x_s)| (y, *x_s.start().max(&lower)..=*x_s.end().min(&upper)))
})
.fold(BTreeMap::new(), |mut acc, (y, x_s)| {
acc.entry(y)
.and_modify(|x_range: &mut Vec<RangeInclusive<i64>>| x_range.push(x_s.clone()))
.or_insert(vec![x_s]);
acc
});
let (x, y) = bb
.into_iter()
.find_map(|(y, mut x_s)| {
x_s.sort_by(|a, b| a.start().cmp(b.start()));
x_s.iter()
.fold(
(lower..=lower, None),
|mut acc: (RangeInclusive<i64>, Option<i64>), x_range: &RangeInclusive<i64>| {
if acc.1.is_some() {
return acc;
}
if acc.0.end() + 1 >= *x_range.start() {
acc.0 = *acc.0.start()..=*acc.0.end().max(x_range.end());
} else {
acc.1 = Some(acc.0.end() + 1);
}
acc
},
)
.1
.map(|x| (x, y))
})
.unwrap();
(4_000_000 * x + y).to_string()
}
fn main() {
let file = fs::read_to_string("./input.txt").unwrap();
let (_, board) = parse_output(&file).unwrap();
let y = 2_000_000;
println!("Part 1: {}", part1(&board, y));
println!("Part 2: {}", part2(&board, 0, 4_000_000));
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3";
#[test]
fn part1_works() {
let (_, board) = parse_output(INPUT).unwrap();
assert_eq!(part1(&board, 10), "26");
}
#[test]
fn part2_works() {
let (_, board) = parse_output(INPUT).unwrap();
assert_eq!(part2(&board, 0, 20), "56000011");
}
}

12
2022/day16/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "day16"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true
itertools.workspace = true

57
2022/day16/input.txt Normal file
View File

@@ -0,0 +1,57 @@
Valve RU has flow rate=0; tunnels lead to valves YH, ID
Valve QK has flow rate=24; tunnels lead to valves PQ, PP
Valve RP has flow rate=11; tunnels lead to valves RM, BA, RI, EM
Valve BX has flow rate=0; tunnels lead to valves ZX, VK
Valve JL has flow rate=0; tunnels lead to valves ID, LC
Valve DC has flow rate=25; tunnel leads to valve ST
Valve HX has flow rate=0; tunnels lead to valves DH, FE
Valve KJ has flow rate=0; tunnels lead to valves ZK, XN
Valve EM has flow rate=0; tunnels lead to valves AW, RP
Valve XN has flow rate=7; tunnels lead to valves LH, KJ, KU, AO
Valve DH has flow rate=9; tunnels lead to valves SY, CC, QL, LH, HX
Valve LH has flow rate=0; tunnels lead to valves XN, DH
Valve PP has flow rate=0; tunnels lead to valves QK, TA
Valve AO has flow rate=0; tunnels lead to valves AA, XN
Valve SY has flow rate=0; tunnels lead to valves DH, AA
Valve MZ has flow rate=0; tunnels lead to valves JT, PF
Valve AA has flow rate=0; tunnels lead to valves JN, UN, WG, SY, AO
Valve RM has flow rate=0; tunnels lead to valves XL, RP
Valve BA has flow rate=0; tunnels lead to valves RP, YP
Valve AD has flow rate=12; tunnels lead to valves LK, ZX, AW
Valve ZN has flow rate=0; tunnels lead to valves EQ, HL
Valve EX has flow rate=18; tunnel leads to valve RB
Valve CR has flow rate=0; tunnels lead to valves TA, ST
Valve WG has flow rate=0; tunnels lead to valves AA, TA
Valve UN has flow rate=0; tunnels lead to valves WK, AA
Valve VE has flow rate=0; tunnels lead to valves JA, KW
Valve JA has flow rate=19; tunnels lead to valves PQ, VE
Valve AW has flow rate=0; tunnels lead to valves AD, EM
Valve XL has flow rate=0; tunnels lead to valves RM, PF
Valve OD has flow rate=0; tunnels lead to valves VK, RI
Valve FE has flow rate=0; tunnels lead to valves JT, HX
Valve PQ has flow rate=0; tunnels lead to valves JA, QK
Valve RB has flow rate=0; tunnels lead to valves CC, EX
Valve JT has flow rate=3; tunnels lead to valves RF, MZ, ZK, FE, DD
Valve YP has flow rate=0; tunnels lead to valves ID, BA
Valve ID has flow rate=14; tunnels lead to valves JL, RU, YP
Valve YH has flow rate=0; tunnels lead to valves RU, VK
Valve TA has flow rate=21; tunnels lead to valves WG, KU, PP, RF, CR
Valve LK has flow rate=0; tunnels lead to valves PF, AD
Valve DD has flow rate=0; tunnels lead to valves JN, JT
Valve HL has flow rate=0; tunnels lead to valves ZN, DW
Valve VK has flow rate=22; tunnels lead to valves OD, KW, BX, YH
Valve RF has flow rate=0; tunnels lead to valves JT, TA
Valve CC has flow rate=0; tunnels lead to valves RB, DH
Valve KW has flow rate=0; tunnels lead to valves VE, VK
Valve PF has flow rate=10; tunnels lead to valves WK, MZ, QL, XL, LK
Valve ZX has flow rate=0; tunnels lead to valves AD, BX
Valve JN has flow rate=0; tunnels lead to valves DD, AA
Valve ST has flow rate=0; tunnels lead to valves CR, DC
Valve WK has flow rate=0; tunnels lead to valves PF, UN
Valve DW has flow rate=13; tunnels lead to valves LC, HL
Valve ZK has flow rate=0; tunnels lead to valves KJ, JT
Valve QL has flow rate=0; tunnels lead to valves DH, PF
Valve RI has flow rate=0; tunnels lead to valves OD, RP
Valve EQ has flow rate=23; tunnel leads to valve ZN
Valve LC has flow rate=0; tunnels lead to valves JL, DW
Valve KU has flow rate=0; tunnels lead to valves XN, TA

249
2022/day16/src/main.rs Normal file
View File

@@ -0,0 +1,249 @@
#![warn(clippy::all, clippy::pedantic)]
use std::{
collections::{HashMap, VecDeque},
fs,
};
use itertools::Itertools;
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{self, newline},
error::Error,
multi::{separated_list0, separated_list1},
sequence::preceded,
Parser,
};
#[derive(Debug, Clone, Default)]
struct Valve {
pub label: String,
pub release: usize,
pub connected_to: Vec<String>,
}
impl<'a> Parser<&'a str, Self, Error<&'a str>> for Valve {
fn parse(&mut self, input: &'a str) -> nom::IResult<&'a str, Self, Error<&'a str>> {
let (input, label) =
preceded(tag("Valve "), complete::alpha1.map(ToOwned::to_owned))(input)?;
let (input, release) =
preceded(tag(" has flow rate="), complete::u128.map(|s| s as usize))(input)?;
let (input, connected_to) = preceded(
alt((
tag("; tunnels lead to valves "),
tag("; tunnel leads to valve "),
)),
separated_list1(tag(", "), complete::alpha1.map(ToOwned::to_owned)),
)(input)?;
self.label = label;
self.release = release;
self.connected_to = connected_to.clone();
Ok((input, self.clone()))
}
}
//* nom parser to take string input and turn it in to a hashmaps of valves */
fn parse_input(input: &str) -> nom::IResult<&str, HashMap<String, Valve>> {
let (input, valves) = separated_list0(newline, Valve::default())(input)?;
Ok((
input,
valves
.iter()
.map(|x| (x.label.clone(), x.clone()))
.collect(),
))
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
struct SimpleValve {
pub _label: String,
pub release: usize,
pub connected_to: Vec<(usize, usize)>,
}
//* takes a map of valves represented by strings and removes the unnexasrray steps and will create a complete graph of the connected */
fn convert_to_distance(board: &HashMap<String, Valve>) -> Vec<SimpleValve> {
let care_about = board
.iter()
.filter(|(pos, valve)| valve.release != 0 || *pos == "AA")
.collect::<Vec<_>>();
let mut distances: HashMap<(&str, &str), usize> = HashMap::new();
//get the distance for each possible connection
for (from_pos, from_valve) in &care_about {
let mut queue = VecDeque::from([(0_usize, *from_valve)]);
while let Some((dist, check)) = queue.pop_front() {
let dist = dist + 1;
for v in check.connected_to.iter().filter_map(|i| board.get(i)) {
if let Some(d) = distances.get(&(from_pos, &v.label)) {
if dist >= *d {
continue;
}
}
distances.insert((from_pos, &v.label), dist);
queue.push_back((dist, v));
}
}
}
//makeing distances immutable
let distances = distances;
let holder = care_about
.iter()
.sorted_by(|(a, _), (b, _)| (*a).cmp(*b))
.map(|(a, _)| (*a).clone())
.enumerate()
.map(|(i, a)| (a, i))
.collect::<HashMap<_, _>>();
care_about
.iter()
.map(|(pos, valve)| {
let connected_to = care_about
.iter()
.filter(|(key, _)| "AA" != *key && **pos != **key)
.map(|(to_pos, _)| {
(
*holder.get(*to_pos).unwrap(),
*distances.get(&(pos, *to_pos)).unwrap(),
)
})
.collect();
(
(*pos).clone(),
SimpleValve {
_label: valve.label.clone(),
release: valve.release,
connected_to,
},
)
})
.sorted_by(|(a, _), (b, _)| a.cmp(b))
.map(|(_, a)| a)
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct StructKey(
/*step*/ usize,
/*valve*/ usize,
/*pathmask*/ Pathmask,
);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Pathmask {
pub mask: usize,
}
impl From<usize> for Pathmask {
fn from(mask: usize) -> Self {
Self { mask }
}
}
impl From<Pathmask> for usize {
fn from(mask: Pathmask) -> Self {
mask.mask
}
}
fn recurse(
board: &Vec<SimpleValve>,
vavle_index: usize,
path: Pathmask,
steps: usize,
score: usize,
cache: &mut HashMap<StructKey, usize>,
) -> usize {
//has this been cached?
if let Some(sc) = cache.get(&StructKey(steps, vavle_index, path)) {
println!("choo");
return *sc;
}
let valve = board.get(vavle_index).unwrap();
let new_path =
Pathmask::from(std::convert::Into::<usize>::into(path) | (1_usize << vavle_index));
let Some(v) = valve
.connected_to
.iter()
.filter(|x| std::convert::Into::<usize>::into(path) & (1 << x.0) == 0 && steps > x.1 + 1)
.map(|(next, new_dist)| {
let new_steps = steps - 1 - new_dist;
let score = score + board.get(*next).unwrap().release * new_steps;
recurse(board, *next, new_path, new_steps, score, cache)
})
.fold(None, |acc: Option<usize>, new_spot: usize| {
if acc.is_none() || new_spot > acc.unwrap() {
Some(new_spot)
} else {
acc
}
})
else {
return score;
};
cache.insert(StructKey(steps, score, path), v);
v
}
fn part1(input: &str) -> String {
//explore graph
let board = convert_to_distance(&parse_input(input).unwrap().1);
let cache = &mut HashMap::new();
let r = recurse(&board, 0, 0.into(), 30, 0, cache);
r.to_string()
}
fn part2(input: &str) -> String {
let board = convert_to_distance(&parse_input(input).unwrap().1);
let cache = &mut HashMap::new();
let top_mask: usize = (1 << board.len()) - 1;
(0..=((top_mask + 1) / 2))
.map(|i| {
recurse(&board, 0, Pathmask::from(i), 26, 0, cache)
+ recurse(&board, 0, Pathmask::from(top_mask ^ i), 26, 0, cache)
})
.max()
.unwrap()
.to_string()
}
//if open don't close
fn main() {
let file = fs::read_to_string("input.txt").unwrap();
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II";
#[test]
fn part1_works() {
assert_eq!(part1(INPUT), "1651")
}
#[test]
fn part2_works() {
assert_eq!(part2(INPUT), "1707")
}
#[test]
fn testssss() {
let board = convert_to_distance(&parse_input(INPUT).unwrap().1);
let top_mask: usize = (1 << board.len()) - 1;
(0..=((top_mask + 1) / 2)).for_each(|i| {
let other = i ^ top_mask;
let test = i | other;
assert_eq!(test, top_mask, "{i}");
});
}
}

12
2022/day17/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "day17"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools.workspace = true
nom.workspace = true

59
2022/day17/src/main.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::default;
use nom::{branch::alt, bytes::complete::tag, error::Error, Parser};
#[derive(Debug, Clone, Copy)]
enum Shape {
Bar,
Plus,
El,
Staff,
Square,
}
impl Shape {
pub fn iter() -> ShapeGenerator {
ShapeGenerator::new()
}
}
struct ShapeGenerator {
last_shape: Shape,
}
impl ShapeGenerator {
fn new() -> Self {
Self {
last_shape: Shape::Square,
}
}
}
impl Iterator for ShapeGenerator {
type Item = Shape;
fn next(&mut self) -> Option<Self::Item> {
self.last_shape = match self.last_shape {
Shape::Bar => Shape::Plus,
Shape::Plus => Shape::El,
Shape::El => Shape::Staff,
Shape::Staff => Shape::Square,
Shape::Square => Shape::Bar,
};
Some(self.last_shape)
}
}
enum Jet {
Left,
Right,
}
impl<'a> Parser<&'a str, Self, Error<&'a str>> for Jet {
fn parse(&mut self, input: &'a str) -> nom::IResult<&'a str, Self, Error<&'a str>> {
alt((tag("<").map(|_| Self::Left), tag(">").map(|_| Self::Right)))(input)
}
}
fn main() {
let shapes = Shape::iter().take(10).collect::<Vec<_>>();
println!("{shapes:#?}");
}

7
2022/day2/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day2"
version = "0.1.0"

9
2022/day2/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "day2"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2500
2022/day2/input Normal file

File diff suppressed because it is too large Load Diff

148
2022/day2/src/main.rs Normal file
View File

@@ -0,0 +1,148 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
#[derive(Debug)]
struct HoHoError {}
#[derive(PartialEq)]
enum Choice {
Rock = 1,
Paper,
Scissors,
}
impl std::str::FromStr for Choice {
type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"A" | "X" => Ok(Choice::Rock),
"B" | "Y" => Ok(Choice::Paper),
"C" | "Z" => Ok(Choice::Scissors),
_ => Err(HoHoError {}),
}
}
}
impl Choice {
fn cmp(&self, opponent: &Self) -> i32 {
if self == opponent {
return 3;
}
if self.beats() == *opponent {
return 6;
}
0
}
fn beats(&self) -> Choice {
match self {
Choice::Rock => Choice::Scissors,
Choice::Paper => Choice::Rock,
Choice::Scissors => Choice::Paper,
}
}
fn loses(&self) -> Choice {
match self {
Choice::Rock => Choice::Paper,
Choice::Paper => Choice::Scissors,
Choice::Scissors => Choice::Rock,
}
}
}
struct Game1 {
pub opponent: Choice,
pub you: Choice,
}
impl Game1 {
fn score(self) -> i32 {
let outcome = self.you.cmp(&self.opponent);
(self.you as i32) + outcome
}
}
impl std::str::FromStr for Game1 {
type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let str_split = s.split(' ').collect::<Vec<&str>>();
let opponent: Choice = str_split[0].parse()?;
let you: Choice = str_split[1].parse()?;
Ok(Self { opponent, you })
}
}
struct Game2 {
pub opponent: Choice,
pub you: Choice,
}
impl std::str::FromStr for Game2 {
type Err = HoHoError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let str_split = s.split(' ').collect::<Vec<&str>>();
let opponent: Choice = str_split[0].parse()?;
// game1
//let you: Choice = str_split[1].parse()?;
let you = match *str_split.get(1).expect("msg") {
"X" => opponent.beats(),
"Y" => str_split[0].parse()?,
"Z" => opponent.loses(),
_ => return Err(HoHoError {}),
};
Ok(Self { opponent, you })
}
}
impl Game2 {
fn outcome(&self) -> i32 {
self.you.cmp(&self.opponent)
}
fn score(self) -> i32 {
let outcome = self.outcome();
(self.you as i32) + outcome
}
}
fn part1(input: &str) -> String {
input
.lines()
.map(|line| line.parse::<Game1>().unwrap().score())
.sum::<i32>()
.to_string()
}
fn part2(input: &str) -> String {
input
.lines()
.map(|line| line.parse::<Game2>().unwrap().score())
.sum::<i32>()
.to_string()
}
fn main() -> std::io::Result<()> {
//read in file
let file = fs::read_to_string("input")?;
println!("Part2: {}", part1(&file));
println!("Part2: {}", part2(&file));
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "A Y
B X
C Z";
#[test]
fn part1_works() {
debug_assert_eq!(part1(INPUT), "15");
}
#[test]
fn part2_works() {
debug_assert_eq!(part2(INPUT), "12");
}
}

7
2022/day3/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day3"
version = "0.1.0"

10
2022/day3/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "day3"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

300
2022/day3/input Normal file
View File

@@ -0,0 +1,300 @@
dWlhclDHdFvDCCDfFq
mGdZBZBwRGjZMFgvTvgtvv
jwwJrzdzGdSbGGnNlzWczHzPHPhn
cczcbMBszhzzDBTBPPPGjtvtlt
LqJLfpwdLnvQLRGQjGtj
gSgnSJJCGSGpGSrwgfhchmmmHzcrHDmbrmMm
bVjstCsSstCLCrbSLnMpdMndcLddcqcpHR
wPZJQJwtBfJZmgBwPTcpTdcnfHMppcGMdG
gmFJzwPJJtszvNhCNC
DmjZDMZWDqGRqqRpHmmRLTTNTPTfCQJQQLJHTClc
FtzfvrfFwVgtzztgBLJNcNlTcTVNNQLN
vgsdbzzrwtqWfWRpZDdZ
rJhqRhLHhdcQqdHqfQGfPGstgGPlWttM
DzCpDDmnNCmBZBZnVBmZzBGPfsbglfNPwgPGPMWsWWft
BZFnlmpBpBzDzVZmhFHFrrrchhRqTdrc
DWCCWFNqdGFdPVcb
HllttQsTRlJlsblrHlhdmPLVcVcTccndLvPLmL
HSlstHgJltghhRrzNBNDzSwMjNZwNb
dzGSHCWSsGVVSdHVHHWWVVDCgJDpQqLTTRJpgmTLRmJTTpTR
BvNjMPZMBtBBMvvNMNttlhLQqCJpLmhTRQqQJgRJLQQg
llNncBlMCwwMnwPZrGsVHzcfFGdHGFGs
JfZhphMMQmFzDTDjSdrQjQ
sqHCbCwBVtbqbCqtrWdjzlSJTlrTSWBn
bHcwbGCGRssNscwtHNbwvmRFvpFFJFvLZMmPLFfv
qBCrzznVmDCmMMDNgrgcrvHHcgbQcW
TTTsdJRTRhhlsgbvbdCFdbWvbQ
JhGGlfRlJsnCMDMqjmfV
rqLLvDLtStDLQhQDQrQhhNdsmWdmmjjnssPnTMnTzfTsWT
ZRFFpgCgppcBcnjTsjTMTfPFMP
GCZpGlwJwBgGHcJhSSHLDDrNqrMtNq
FsPFqsDNZFNnZrcBmWfWWQ
lSnRlRTvgrWtctTmft
SMbGbbvnGlnSDwGqLwNCqNVD
dPQDcBwJJDgDTPgGgQTBVjSsmLhLTrLmjSLpjSLh
MvvZRHtMtbCNvCNCNtNvbRfBSMSrjmLpjnjmVhrVSLsnnmSh
qHZtbBZfRztbHbCzNRHPQgJPJgGgglFPPFqdQD
MsBsVDspRPfPlhMl
zWnCFzHbSCwqNmPSjmGlsmfN
FnHnFbFzsHHCCgzCzbBptVppgvvVgVrpBrJt
pTLntptZjQLfVDjQTDlVJCSWNCPSCCsSNmFlNslm
BBHbqGHHqgwSWSCCWwZWWZ
qRzbhhbzzrHdRRHhRHvzZjfrVTrppVQttDfcQTfp
DHsdHPHHsHMsRmhMZZQBtljgZGtC
NFnCbFznLVJbVrjhQthjGBLZBjGL
wNrfFJJbCpNnfbdwDSDHsvsHmsmH
jLZRjnMMjJhJnvtQbdHfHZbvHQ
mBzwptCWlcFCwsHSTpQfQHQfrpTb
mNltzmsCNmFzGwCBllGRgRgqjgLMnGqjnPjV
ZqqcqmVVtbcBMFfFMcQfgphJ
HLWLDvWjjLwTWzzvGLThQQshQllgJDJgfbffJQ
vHzLvrwwzGzTRGzzLLRPRwRdSVdnBBdSbBSVStbNmnrmZS
TWVVvPSgwWSqcRgRwbRRcqshsfFzzzChTGNHzHhhhCsG
njZrjMLlpmDmGfSGtrNttzCF
ZQBmZdDBZRRPRSBgcb
TpntvdpnZDptnbnTDGtSFSlFmzCzzmSFRjqlZj
MWrNcWRMlgqzMjzq
NPwsPNrBNcVHNNcJHBNBcJwwttDvGVnDptVRtbnTtGvVGtGG
tsbbvvSfnqvzQLLBjfMLdd
gJRmRNmJNchgmmrFJhFgWJQMwBnjwrMBVQwQTBdLdVQj
GcGpGGRGJgqstvpbnCqb
rsHcrbZHBTTtLtNSwwHLLJ
mFqhWVsjsVCjQlNJGMwMlMMGMh
QggzffQRCfgVFWzzCQffqfZpZDcRvTTBTvvZnsdnddcn
bPFMFFBpMlFfMZMpHGNSrNctJcPSSchJchPt
zCgwnmgzQDnQgdWWQRgqSrqLSqSgssLNJhgJ
rQCQQTrRRmDBFfbHBFHZbT
fzfPQsGrrMMjtHtBHs
SwNNDqwhWpVTwbDGGDmwSVhZRZHdbCtgBjCRjMtbBHtRMd
vVTGvVGNvPPvQvfncJ
cwzMJbclHDPqfJQPfq
rrqjjTBrqqBjRCgTjrRjNrsGPDhDFGCfWGfPFfFPGWfD
BZTTSTZgjbSwVptvpq
PPPPJpvpJsJwPHHPsJdTNZRZZZjTFFmRRRNjZd
qbWVfChDCDnVVDGfnSFNNjRLmNfBNLQjLjmBRm
DhFDhbnWCDhGcbJPPwrsrMwrvlvc
lFSDTwHTSwlTNwFFlwNcFFpjLZvZqvnqLPnnWbgngbbncqbZ
rzQfMzRGrRGJCffBMGdGsJZWWnqWVqdPPgNvvVZWPWWn
RJrttBtNQCsNzTpShhHFDwFlth
QNzQFjNFrQPNbmPpqTTDGswWmB
ggHRcSlcCVCSzMVqDGwgqTWpsmqwqG
cltzCZtLClHRRtMZZLQjfNvtrJfhvrddvNNd
mcfWHffBFnQRQlTFdv
wssSLVbbzDVbzbggzSzNshNMnvnljRdvQRlMBjvQdnCj
bhDzVSSJDDJhDLBwtbDzzbbWppfpcmWGmprqqGtqprcHPp
rpVFrZpgHWSZrFPqhzwcqPwmcVBD
vMTnQJjQLCbljvvQzTMbTjPNdBLwwDhmhNNqPwmmhhBh
jvGjjQJnrspGHgFz
sjssjtZlcphZHwWvcrHTwWJH
qDdzzrFNNNDGdFDzzVBVVvfWJPfgPmgWPgvwVH
nqMQGDrnLGnqqLNqjtCZZjsMhZhCMbtl
JJJsLFmzsrFlSpzPscjgHhnRnmvcjqRvvj
fCMQbCbTjjqTGhjc
bfdbWdCddfBbtCfbfbqVWQQpPlBrJLJJSSLwppFssFzLzp
pdbbzlffWtJbgQwhcphQcCCg
vHvLFvVLvSfFRLnRFRNHjPjggcssQcjjsnwhsPCC
vGVGGFFVHLTvDRHDmBbTzfZWMdJZlfMm
wjCbjQgjTQhNNzgWQCWrDFMZmZDZDCrrMDpLpL
czGSPznnRGGJGGlVVRVBGGlBmDDcMDFDDZLqfffZFrrZqFpZ
JPGlvBSJHVGnVsjvQjjzwdwjzd
MFlWQHDTpnpsFNNQllWFWlhzjGgrgDzGGhGGjvmZDZrh
PtTPcTLbBCVPTRVcvhjmmhVhSZGGgvZg
JLPfCwPbTbBPJCfblplMpqWsMpMwWHQn
QbHVBBzWtzHBNtBwQSgqhqSbFgRLjhmqqj
ZnCnMcdsDnJTncggFJwRmSwgRFmL
sTMZpGDvsZcMpcvTCPHwzrfzrpzHpWBrWz
PMdJWwJWHFWJnNzbDlfbCfMvbl
rZgttrFptFFcBtccbbCDvgfbGCGGgGDz
QsZBmsrFscrVrjQJjJQRJWWLwq
GwNNJwwRThwrWfhh
SmQqmzsjHssQzCbvsmSSzsQTjWpFTTfFfThchhFTBBfppB
mmbHbmtmCzzQZzQRdZJhNMdMlRMglh
rrsPbncQvvgnnrTdGDVcCdpZHHZp
RwwwhjLLqtJFwjzwtwmwwGpDZVdGDVdZBZDFHdHZCp
zhzwLhhfffLtjNChgWbbrbnMvPrMrrfv
tQMtQtTSBFtSmQSttMggMtbtnTnPrZvrnzNNTGZvrZZdLdnL
HqhwDpDcwlHqpVrrFzvFGpZrrzrP
RwlhhjwRCjVfjDbMtFWBWJMgWjWm
WpWVlWzsGlBJpspNclNlhhhmgzrdtzQMQttzMmtt
RfnPRLTPDHRdPbwvvntnSrFgmvnmtm
CwqRDCCwqCwbCTqJcJBNcZqdVcBlpJ
tpfnNBsGGNRppRCgfgRRCRQJGMPPWdwMJdWFFwjVzGPJ
chLSchLTbLqvqcZLlvvLqbmldMzFFWdzVSFWFMzQVJwjQjdM
rLqqcjDrcvhRsDfHDtNNnN
DjZjvTTDqrtTZZSMcdRdmRJrcJNc
HWgPGVhFPgnSVtnJcdMs
FPLfBwCWGPfCwfLHCCDBZlbDpBjvqTtDzzDj
FJNqNFgNFssqGGqBsTlMVcgVrCwLwlhcrw
fRZzDmDZvvDdZbtdpDZmbrThrLMCvcChwwlSTrvMSr
zmfZmtZmpDmbfWRDDZdqFljqnNQjGWnsjFqGsG
rMdMWddmJmvdSdmWfWMddpVRqRFVHRRqMRRPQMRqRq
tDGtGGhLjLLZNLjjNgNthGtqHTVqRVVpPDRpHHVRQVRPPR
ZlLtzNjgsZZlssLgtjNpfWfJvlJrrvCJfBmBfn
hqpWvFJsJFNHhqMWNhWvWRmmDcDMLcwZnjcwnjRnjn
LdrlgCCrSSTrTnwRjmwRQZwdwG
PVlfClrLlLlfggtBPzHHhHFbWzJNbvqJFNqF
TbbQtnDtbGGjGlGsGHpJJmFWFJJrBWWFlWrS
CZzzNzzhddNchhMhhRVjpCBwBrCvJvpmSCvrwJ
fhZZhddRjRgZzMZRzPjPTsLGQtHLTHTbDPQTtqPT
nHnWsQNQQWTWQshwjBJJJmHwFBwm
VZZpfbffZVvbSbGfBhSwmtmmJlmjJFJJ
bpphpMfMvMzDbMGZgQNrrngzdTsNqWdd
VPNddVTPPmdnVcPVZcdTmcDbQTFjMpjtFzbMtFjzsFTssT
lJCllWCrgvRlgwlJfRRvSzjSjQpbzMHpbwMQpszM
fRhGBBJJCgrNLsNPNVVhNq
pLrVDgbNbjVplpsltHBqWSqhSQcHDttH
MCdCwCGTmnTmmmvTTCwCqNhHQhRWcwHWBRHSqSQH
TNTFFPfffTvFTJvTPCPTFfGdzVjspVLZglJbsbZpVblrzjlb
cdPzFrldgcdCrnlznPzrBNRssLLBbVNVZsLHRHdm
wTQQwvvtqwqcGvTZmVHBbVLLBbRV
GWJSGfJWcjQwhQQWjqJhhGfgpMnMzDnpMlPpMnDlMrzl
pMhqTTsSpdBPpNBshsdMMTQFvFlQtQWCRQlCllVFqVqG
dfcbnmrnjzRFvGQQGvfv
mDrjLLLcJjLhpZSSJMdpph
NGZNwqFqZhhcFSCfRzwdzRfCzVRw
QTTmBTsWQWJPPCvzvpHPzdvVFR
bsmWTBbQDbmbLQQMsWWQchgqLqhGGGGZLhSFjhqS
HgmGlgsvBBDgBGCdHHvHwCGwhZJWhTjSdhTSFFFhJtSJTJhT
RQfVrfQNszMQfpMzpNnfLbtjhtSbWJWFWtFFtFJtFSZq
PNzLfnLnBCPHgsgC
mTZGgCdNSNmCQLLpPnDhRlGhpV
vWJHWFsfHMWBBFbBsjfjHrFfLRRLPPnpLthttRVPLSnhSPbS
fzMrBjWfBrzsZCZmSTgQzcNN
mgmCZCMgmnZmZgBZpgpJfbQfwSQPDTdfdwSDfwhn
sHhcrWLcFlzHcHRNNFvNFcFPwDDTWdddDdqWbSTWDPTTQS
RNsslsRrNcRNvNRFFNvVsghZBJtVCCtCtGghjhGBGG
gchrcRRdnRwPPnvQ
CVCCSrDjFHjVDbBLFGGBSvwnwNMnMsPPNsNPvwPQVM
lHLCTHGDCbbjFTTzdWlpcqfgcrdzZg
fRDPsDsqqJttttJSzPDgJWQCbQQbGMWCCnGGPVGVQQ
rTTBvZhrvBnWWDWCbZWW
cwLlTLpjTwBFLLhgfRRfmRqRDmRdjq
sprGGPTrJTsGPzszqGzNtTtpfbQddQSQSDFDFvvbZvwFbbfN
LWWCMVmwMmgWFQfFDDvZDgdF
mlMmRVCWVMmmHRjVCmjHWRhMzpTtlrlPzrtzwlsGPrpwtrJp
tsfwwfjfdfrtrClfvwvvLnTHNmvLHcNccRNcvNWH
qQSqZqFQRBzghDFncHgmccHNmWcNmM
SQJphFJzRDSsdpVlllrCrw
vGQqLQFvBvLvdNnvjnvNDc
TRJwmWmZWlCCmzznbNhhbDhRgj
CCTtDTlmDTWTmDmZZlVLLsFfstfFFLsBLQfF
cfWflMmWWlWfPWBhBlQtLmmvrrrvCLjvRTjLLwwr
gSgbsbgHdsjzHbqbdVDLZLvTZwLTvSrZrFvZLw
sdbJqDNdjJNdsJBpBWpJlMcfcB
FHlMHPqDLlPctgHSnttCSC
zhrmBrTwJTjBmQcSQvQqbtwGvg
jBjmBmJjjjRZTBzhhrBJLDdZqfpDMdfWWlDDLMlV
zPVdbsBzZdwqJGhrLTvNNJqH
tmmCgCPCDDnptHDjNvGvhrDvLv
pRWRlpSpPllClnpbQVQwFFVSQFVBZz
nDrCvmvMnMSmsCvblBzzCZplbJlTbZ
FNRtFWRfcGqFGQbzlZTQqQTBbd
GRwFfNtwFRNFGMvBsnnwMMMBjn
LVTBjjlJCDrnJzJNQR
GsGGsggGpfhgpchgdqzbMzzhzQRnnMRrNzzR
PwWFqFGpwWpdWgfsGggdmjCVHPHlCCCVZNCjVmVj
qVTsCWwbCsPlCVfcbvfPDgLzbzDDhrzRrjgZghgr
ntmHmNpSQNGtntNttmSdSdBdjrrDLQZQLLhRrFFFRDTFZhDF
mtMtBNTSNBpNJStMGSdHppNcVWPsWvqVcsVJfwwqlqWqlc
vvWzLvvdpZDvhTpcrLcTTLpdwSPnCfJwCMnQSMwSnCGJrnwr
ttHVmVNNsHBBRsHbMMwwnjnjBfjJwCMP
tsVllgNVqbRlfplldDvDWT
mLjLsQqLQqsBRvvlRBLRlT
bhgtDDhCtmptmTTS
nfmdbggwGWrfsPzfWq
JpWDcSGJpGzsHPSSlbbd
wVRqVZwwRwPDwbDddH
VtVVVLthLVtVgfQLRTNtqDcCcJBmmWMWWprpFrcBJWNp
dhhhDtmLdttdPlslGlRFjfzBBpzzRpGJ
QMrVMwbVrrbvVVCrvcnqQQrrSMBJfpjFSzfjJFMFRWSpjjFG
cwvbHHbCqVchRDHgDsPTdt
CgVNCtDsDtJGZZGqMMGhDq
cLRnSHgWcRdLHWSSRLjQdlHBTTPcPwwhzqzTMBPTwhPPwP
HnnnglnWWgdRjlmQNsNFmJCFJFvsJsNN
hfccLbjhfSRbfDZjFRJzrlvlwwlnnFrWwzqr
TCsPLNtQdpdQQVtVNvJNJWlzJzwlrvJl
pHtPsPtPtCQfbRHHDHhMLh
nWRWgLtWnfTcZNNsscfd
JMGzMVJwMVTvzVQFGHMMmPdddsPsCjldlHPcScNPsP
vGrMQQmmvTQzMJpghWRWgpbbBqLbLR
WSbhFbPTpRfTfPdhpfbhSbfPQLzlQlzlHvtQsvlltlsgHdgQ
pZcGJDZNGcVrJwrDrrnvtNvlNzsgtgzvvsgt
qmJZZJcBqwrMJcVbWfSPPWpmpjmSCF
jHVjjCcpNrDgjsfB
ndqllRvJQtqlQQTRWllFNDrsMZBfDBLvNMNDfsbZ
qTFnWJqdWRdqWRlnTRnQGnTmwwSHCzpcGNmHNcPVcHGmCz
pZCpBhDfvgBVZQGMMVZVlq
sLsLTTSssjPnTNbFGRGFPVHqMMRF
TsNSccnjLdcsLjdmjWvWvBhfmvWpCGhGhD
VWFFFPMpPVSMbTppHTnHTbRH
DtvfNdBNddDNSLjsvDTTHnzzHwrTrwsrwqbT
dgBLNffdgjjtfBQSvgNjNDlvMcQJmJJmZCMmVVVZFPFcPMGG
VWsQLHMVVSNRShWLhNSNLjbbbddbpDZDddcbZdDRztpd
FPlhhgPvThGFJndnnCCnJzzdCp
qrTfmllGvfvGqwNMMMsffsQsNh
NsmFqNlmnQRbCFsmJgSffpPcbvSfrVvpgS
LhZGDZhhwtDHMwDdHGhDjDpTzzPdzgTvcPvpSSpPrdrv
jgHMjBLhwtZMHMHmJNNJNFlBqlBJNn
bznSQggscgMcSTTfJbSQzQFwClMhmCmthClvMwFLwhZL
BRWBPBVVPjPNVHpVqlqrvtwFqmmLqltZmL
WDVddNHNvRgccgsDsgbT
sNgnQLtLLLPPnsPpqdqjBclpGWjcWjBG
rVCChSZhVrrwqVDVHSHmMjldGfJGfHddJGJlfGjGJj
CVZmDvZCmmhFVVrCgTNbbnQFgbsqNqNQ
WmMmSSfJNRRPfJRMRMtllCgdStgbgttgCdDd
QGBrvzwBczlgqCtDbvDq
QQpBGFrLQjQzGVVRNjPmNNWMbW
gGljnJhnJtllpNVCHWcccdTdjdmB
bLfSQDSMSHmBnwCB
LFMDrbFfFQZQRzLZnbgtlsRGtltpgNhgPpPG
cRThZZchCThtgTRhZTRtjWFjWNwqCjGmwFjqqffC
DPDPGzPMHDbrpqjfwrjqmjmp
JVHHDdVdVbvGMdnVdQVdDbHcRZllhRtgStRLThRSTcBTvc
lQWPSBrrPZGgPglGssDfHnWsfDFHHvHh
TDCqpttptJNLtwNpbwTqzqHshsvsMMFnmHMNfssmvmvf
JCjqVpDtrBjQjrlj
wFGWGpFLvCczNSWWsz
tlfgtftjlbtHHlDBsBzmQQnsQDQsCn
gjfrsVqVgPlfqhvLdvdwZhGq
pHpZHBSvRvRCBBZCTMngRnWndnRmWcgg
jsfrfrjJFDwDDMMggMCGWGcfmG
qbCszCjtCjQsQrtZVBHBHvBvqLZvlp
scFzsPScNgNPNgQzpttlCBCwpLrMLCrDdljLwq
TjfGZZjVwMZwMLwr
vnGbWTvTmFRjQFQPsb
bVLrzqrzJVgJbbtVrWJVgppcBCzBvdzwBCCBHDcBvc
hflPQnMQmQSRlQMPNRTHwwHHHqpHpdfwdBCp
hFhZMhqSNMNbrZgWWGWJjZ
NJsgNjJlMHQrwnRgSRPwrP
tqpQtTFpFvbGpzTTWSrnSbrhwChCnRfrCf
GtcvGqQpttzcqdFzWppDsZMJjBBsBJcBNmBjMsLJ
lGfZGZhFfhdSWqmFFWSS
wDRDPLcDnjtWbSmqrSCSLC
MPwmtVnVMjztznHPgQhQfJfvvHHGggQZ
llTspLllCHmLHHndldqHdlLQQPSBQczZSFDDQZSNGcGG
jMhwvVrRjbRhFBZNGPcGNN
wVtrrtRwrfrwftjVjwWvMrRpsqsnsHsBglslCmTsdWdHTd
vPvmTGgDPRvGpDPGPqGHQnWJQJMBBzJBlBQWlHWl
bfbwNsmwFdLjbfrrLsSfLNQtMllznBzJQZMQtMlZZnnF
frssSscssNfScCjfSCwjsDmRDpGmDRDvvvVcvRDvRp
LtlPZPjBTbWsWJVJVzdT
nnprqhrqmzfrSrphqfCChVVGVDJWgSHHWgWsRDVHWd
nrmppNqhcCrfMchcMCncqbzPZvlvlwbBNjPjtNjZjL
FPWsFdSspVbbbtWVvl
CCHnnfHHvCwtVMhzlzDllC
LrGnjGfgfvcwfgrLrBjrBLgwdBTSRBFsRZdRsSqFFSFSSPPp
whGCLqsrjgGhhGFqrCCFGCGzTRTZJcNnzlLTnznNHcnzTH
ddvVmbfvdvVbDVQdvvdSzpNcnJzlzSRHNJpnJcSc
BPdvfQdWtPDDPfDvDQVVPmbhssCGGMqgFCFMqGMWgMjrRw
PSLbGmWPSPLQbMTPWGFWltthdDdrmBDHhdDdczzDRh
VfCngVfgsZwCftrZdhcZrdNDzz
CjVJJJqnJwQhWPPLQlGj
ntnnQmTQTQGVWGNGNNlClG
jDffjMSvqjHzHHzwNVwNVcCddPVNdD
ZszJsrrZMjsHqqvZJLRQCbTRQbJmThbt
BgLHgFDsJNWgQgflWd
mnVVcCHnCGRcVnZSjmlthftMQddlfhQctNfW
qbSGqmHSTFprvpvTTL
dvdTMvvpdLpTcSLvdLLMmhfFBftwCNhRwRNjtCTRCf
lshQWgsgrHHqlFfRqFjRFfFwCB
rsgHQbJbrsGHHlgQHgJrlHrPZdhdpMZGDSDpdPLcZhdvhZ

90
2022/day3/src/main.rs Normal file
View File

@@ -0,0 +1,90 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
fn part1(input: &str) -> String {
input
.lines()
.map(|line| {
let (comp1, comp2) = line.split_at(line.len() / 2);
let duplicate = comp2.chars().find(|c| comp1.contains(*c)).unwrap();
match duplicate {
n @ 'a'..='z' => (n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
})
.sum::<i32>()
.to_string()
}
fn part2(input: &str) -> String {
input
.lines()
.fold(Vec::new(), |mut acc: Vec<Vec<String>>, line| {
if acc.is_empty() || acc.last().unwrap().len() == 3 {
acc.push(Vec::new());
}
acc.last_mut().unwrap().push(line.to_owned());
acc
})
.iter()
.map(|group| {
let [g1, g2, g3] = group.as_slice() else {
panic!("not get here")
};
match g1
.chars()
.fold(Vec::new(), |mut combo: Vec<char>, ch| {
if g2.contains(ch) {
combo.push(ch);
}
combo
})
.iter()
.find(|c| g3.contains(**c))
.unwrap()
{
n @ 'a'..='z' => (*n as i32) - ('a' as i32) + 1_i32,
n @ 'A'..='Z' => (*n as i32) - ('A' as i32) + 27_i32,
_ => 0,
}
})
.sum::<i32>()
.to_string()
}
fn main() -> std::io::Result<()> {
//Read in file
let file = fs::read_to_string("input")?;
// fold the lines into groups of three
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
// find common letter in the groups
// find common letters in the first 2 then find the common in the third
// sum the common letters
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw";
#[test]
fn part1_works() {
assert_eq!(part1(&INPUT), "157");
}
#[test]
fn part2_works() {
assert_eq!(part2(&INPUT), "70");
}
}

7
2022/day4/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day4"
version = "0.1.0"

11
2022/day4/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "day4"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true

1000
2022/day4/input Normal file

File diff suppressed because it is too large Load Diff

79
2022/day4/src/main.rs Normal file
View File

@@ -0,0 +1,79 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
use nom::{
bytes::complete::tag,
character::complete::{self, newline},
multi::separated_list0,
sequence::separated_pair,
};
fn parse_range(input: &str) -> nom::IResult<&str, (i32, i32)> {
separated_pair(complete::i32, tag("-"), complete::i32)(input)
}
fn parse_line(input: &str) -> nom::IResult<&str, ((i32, i32), (i32, i32))> {
separated_pair(parse_range, tag(","), parse_range)(input)
}
fn process_input(input: &str) -> nom::IResult<&str, Vec<((i32, i32), (i32, i32))>> {
separated_list0(newline, parse_line)(input)
}
fn part1(input: &str) -> String {
do_part(input, |(a, b)| {
i32::from((a.0 <= b.0 && a.1 >= b.1) || (a.0 >= b.0 && a.1 <= b.1))
})
}
fn part2(input: &str) -> String {
do_part(input, |(a, b)| {
i32::from(
(a.0 >= b.0 && a.0 <= b.1)
|| (a.1 >= b.0 && a.1 <= b.1)
|| (b.0 >= a.0 && b.0 <= a.1)
|| (b.1 >= a.0 && b.1 <= a.1),
)
})
}
fn do_part(input: &str, f: impl Fn((&(i32, i32), &(i32, i32))) -> i32) -> String {
let (_, ranges) = process_input(input).unwrap();
ranges
.iter()
.map(|(a, b)| f((a, b)))
.fold(0, |mut acc: i32, x: i32| {
acc += x;
acc
})
.to_string()
}
fn main() -> std::io::Result<()> {
//Read in file
let file = fs::read_to_string("input")?;
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = "2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8";
#[test]
fn part1_works() {
assert_eq!(part1(INPUT), "2")
}
#[test]
fn part2_works() {
assert_eq!(part2(INPUT), "4")
}
}

7
2022/day5/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day5"
version = "0.1.0"

11
2022/day5/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "day5"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom.workspace = true

512
2022/day5/input Normal file
View File

@@ -0,0 +1,512 @@
[Q] [B] [H]
[F] [W] [D] [Q] [S]
[D] [C] [N] [S] [G] [F]
[R] [D] [L] [C] [N] [Q] [R]
[V] [W] [L] [M] [P] [S] [M] [M]
[J] [B] [F] [P] [B] [B] [P] [F] [F]
[B] [V] [G] [J] [N] [D] [B] [L] [V]
[D] [P] [R] [W] [H] [R] [Z] [W] [S]
1 2 3 4 5 6 7 8 9
move 1 from 4 to 1
move 2 from 4 to 8
move 5 from 9 to 6
move 1 from 1 to 3
move 5 from 8 to 3
move 1 from 1 to 5
move 4 from 3 to 6
move 14 from 6 to 2
move 5 from 4 to 5
move 7 from 7 to 2
move 24 from 2 to 3
move 13 from 3 to 2
move 1 from 7 to 9
move 1 from 9 to 5
move 7 from 2 to 6
move 3 from 1 to 7
move 3 from 6 to 3
move 2 from 7 to 1
move 1 from 7 to 5
move 2 from 2 to 6
move 2 from 1 to 4
move 9 from 5 to 1
move 1 from 6 to 3
move 4 from 5 to 4
move 1 from 2 to 7
move 4 from 6 to 2
move 7 from 2 to 3
move 2 from 2 to 6
move 2 from 2 to 3
move 2 from 5 to 4
move 1 from 7 to 3
move 4 from 6 to 7
move 19 from 3 to 6
move 3 from 7 to 4
move 1 from 7 to 8
move 1 from 8 to 1
move 2 from 1 to 3
move 10 from 3 to 2
move 3 from 3 to 8
move 1 from 3 to 9
move 1 from 9 to 6
move 11 from 6 to 8
move 2 from 3 to 8
move 6 from 4 to 3
move 3 from 4 to 1
move 7 from 2 to 8
move 1 from 3 to 6
move 6 from 8 to 5
move 1 from 4 to 6
move 9 from 6 to 9
move 6 from 3 to 8
move 1 from 3 to 5
move 10 from 1 to 3
move 11 from 8 to 7
move 1 from 3 to 5
move 1 from 1 to 8
move 5 from 9 to 2
move 1 from 6 to 3
move 5 from 3 to 6
move 1 from 3 to 5
move 4 from 6 to 4
move 1 from 5 to 9
move 6 from 2 to 4
move 2 from 2 to 9
move 5 from 5 to 1
move 2 from 1 to 7
move 10 from 8 to 3
move 1 from 8 to 6
move 3 from 6 to 3
move 6 from 4 to 2
move 8 from 3 to 8
move 3 from 4 to 8
move 4 from 2 to 1
move 3 from 5 to 3
move 4 from 7 to 6
move 2 from 9 to 3
move 1 from 2 to 9
move 1 from 2 to 3
move 2 from 4 to 8
move 1 from 7 to 9
move 5 from 7 to 8
move 2 from 7 to 3
move 14 from 3 to 2
move 3 from 9 to 5
move 1 from 3 to 1
move 1 from 7 to 4
move 3 from 9 to 8
move 7 from 8 to 9
move 7 from 2 to 5
move 2 from 3 to 7
move 2 from 7 to 6
move 16 from 8 to 9
move 4 from 6 to 5
move 1 from 2 to 5
move 21 from 9 to 5
move 3 from 9 to 3
move 6 from 1 to 4
move 1 from 1 to 9
move 1 from 1 to 4
move 2 from 6 to 3
move 3 from 4 to 6
move 3 from 4 to 8
move 1 from 9 to 4
move 2 from 4 to 6
move 4 from 3 to 6
move 1 from 3 to 4
move 1 from 4 to 9
move 1 from 9 to 8
move 1 from 8 to 6
move 6 from 2 to 1
move 2 from 8 to 4
move 6 from 1 to 8
move 23 from 5 to 9
move 1 from 4 to 7
move 1 from 7 to 1
move 22 from 9 to 7
move 4 from 8 to 7
move 1 from 5 to 2
move 1 from 1 to 9
move 2 from 8 to 4
move 6 from 6 to 3
move 2 from 9 to 5
move 18 from 7 to 4
move 18 from 4 to 5
move 1 from 2 to 7
move 1 from 8 to 4
move 6 from 7 to 2
move 5 from 4 to 5
move 1 from 3 to 1
move 1 from 7 to 2
move 4 from 3 to 4
move 1 from 3 to 4
move 1 from 1 to 7
move 1 from 5 to 8
move 3 from 4 to 3
move 3 from 3 to 8
move 2 from 8 to 3
move 2 from 4 to 8
move 2 from 7 to 5
move 1 from 7 to 9
move 2 from 3 to 1
move 1 from 9 to 7
move 4 from 2 to 3
move 1 from 8 to 9
move 2 from 1 to 8
move 2 from 2 to 4
move 1 from 9 to 1
move 4 from 6 to 8
move 1 from 2 to 7
move 1 from 4 to 7
move 4 from 8 to 2
move 1 from 4 to 3
move 1 from 1 to 9
move 4 from 8 to 1
move 2 from 2 to 1
move 3 from 3 to 9
move 2 from 7 to 1
move 32 from 5 to 1
move 1 from 8 to 7
move 6 from 5 to 1
move 2 from 7 to 6
move 1 from 9 to 5
move 1 from 3 to 2
move 1 from 5 to 9
move 2 from 6 to 1
move 1 from 3 to 7
move 1 from 9 to 8
move 36 from 1 to 4
move 1 from 8 to 9
move 5 from 4 to 9
move 6 from 9 to 3
move 2 from 2 to 9
move 3 from 1 to 9
move 1 from 3 to 2
move 30 from 4 to 8
move 1 from 7 to 5
move 1 from 3 to 5
move 3 from 3 to 4
move 2 from 8 to 5
move 3 from 9 to 8
move 3 from 9 to 3
move 19 from 8 to 6
move 2 from 3 to 5
move 3 from 4 to 3
move 1 from 4 to 7
move 8 from 1 to 8
move 1 from 3 to 2
move 1 from 7 to 6
move 4 from 5 to 3
move 1 from 1 to 7
move 2 from 5 to 4
move 1 from 9 to 4
move 12 from 6 to 2
move 1 from 7 to 8
move 6 from 2 to 9
move 3 from 6 to 7
move 2 from 7 to 5
move 6 from 2 to 3
move 8 from 3 to 5
move 5 from 6 to 8
move 5 from 3 to 6
move 1 from 9 to 4
move 1 from 9 to 8
move 5 from 5 to 9
move 3 from 4 to 6
move 1 from 4 to 9
move 1 from 7 to 5
move 1 from 3 to 5
move 8 from 9 to 2
move 3 from 9 to 6
move 27 from 8 to 2
move 10 from 6 to 9
move 1 from 6 to 4
move 1 from 4 to 9
move 2 from 5 to 6
move 5 from 5 to 3
move 2 from 6 to 9
move 5 from 3 to 2
move 12 from 9 to 3
move 5 from 3 to 1
move 3 from 1 to 5
move 1 from 9 to 8
move 1 from 5 to 2
move 1 from 2 to 1
move 1 from 1 to 6
move 1 from 5 to 3
move 34 from 2 to 4
move 8 from 3 to 9
move 1 from 6 to 1
move 1 from 8 to 5
move 4 from 2 to 8
move 3 from 8 to 7
move 1 from 7 to 2
move 7 from 9 to 8
move 1 from 9 to 6
move 2 from 5 to 1
move 1 from 6 to 9
move 1 from 9 to 5
move 2 from 2 to 5
move 5 from 8 to 6
move 2 from 8 to 5
move 1 from 1 to 3
move 12 from 4 to 6
move 2 from 7 to 1
move 4 from 1 to 6
move 3 from 2 to 3
move 1 from 8 to 5
move 1 from 2 to 6
move 1 from 1 to 9
move 1 from 9 to 5
move 16 from 4 to 1
move 4 from 3 to 1
move 8 from 1 to 8
move 1 from 4 to 1
move 6 from 5 to 8
move 1 from 5 to 7
move 12 from 6 to 9
move 7 from 1 to 5
move 2 from 1 to 7
move 1 from 7 to 1
move 9 from 9 to 6
move 15 from 6 to 2
move 2 from 9 to 7
move 4 from 4 to 5
move 2 from 2 to 9
move 3 from 7 to 5
move 2 from 1 to 3
move 1 from 7 to 1
move 10 from 2 to 3
move 6 from 8 to 6
move 3 from 9 to 2
move 14 from 5 to 6
move 1 from 8 to 4
move 5 from 8 to 2
move 2 from 2 to 3
move 24 from 6 to 1
move 3 from 1 to 2
move 9 from 2 to 9
move 1 from 4 to 3
move 1 from 4 to 2
move 1 from 8 to 4
move 23 from 1 to 4
move 3 from 2 to 4
move 2 from 1 to 2
move 1 from 8 to 4
move 3 from 3 to 5
move 3 from 3 to 4
move 3 from 5 to 8
move 3 from 2 to 7
move 2 from 3 to 8
move 15 from 4 to 3
move 2 from 4 to 1
move 19 from 3 to 9
move 1 from 7 to 2
move 1 from 2 to 5
move 1 from 5 to 4
move 1 from 7 to 6
move 1 from 7 to 4
move 3 from 8 to 3
move 1 from 8 to 4
move 5 from 3 to 8
move 1 from 3 to 6
move 22 from 9 to 2
move 17 from 2 to 6
move 3 from 9 to 3
move 9 from 4 to 9
move 6 from 4 to 9
move 5 from 2 to 6
move 1 from 4 to 2
move 1 from 4 to 9
move 1 from 1 to 6
move 19 from 9 to 2
move 4 from 8 to 7
move 1 from 1 to 5
move 1 from 5 to 3
move 1 from 8 to 1
move 1 from 8 to 2
move 4 from 3 to 7
move 12 from 6 to 1
move 3 from 7 to 3
move 7 from 2 to 7
move 9 from 2 to 6
move 4 from 2 to 6
move 13 from 1 to 4
move 8 from 6 to 4
move 16 from 4 to 8
move 12 from 7 to 6
move 3 from 8 to 3
move 1 from 1 to 2
move 4 from 3 to 8
move 5 from 8 to 9
move 27 from 6 to 8
move 2 from 3 to 7
move 2 from 2 to 8
move 2 from 7 to 5
move 1 from 5 to 9
move 1 from 5 to 1
move 1 from 6 to 9
move 2 from 6 to 2
move 2 from 2 to 6
move 2 from 9 to 2
move 3 from 4 to 3
move 1 from 1 to 9
move 5 from 9 to 8
move 1 from 9 to 5
move 2 from 2 to 6
move 2 from 4 to 6
move 1 from 3 to 7
move 1 from 5 to 6
move 1 from 6 to 7
move 6 from 6 to 8
move 2 from 7 to 5
move 2 from 3 to 2
move 34 from 8 to 1
move 1 from 5 to 6
move 1 from 5 to 3
move 1 from 6 to 1
move 32 from 1 to 8
move 23 from 8 to 4
move 1 from 2 to 1
move 24 from 8 to 4
move 1 from 3 to 6
move 47 from 4 to 6
move 2 from 6 to 1
move 3 from 1 to 5
move 1 from 2 to 1
move 3 from 5 to 7
move 21 from 6 to 2
move 3 from 7 to 8
move 2 from 1 to 6
move 8 from 6 to 4
move 4 from 8 to 9
move 3 from 2 to 8
move 4 from 4 to 2
move 2 from 2 to 5
move 4 from 9 to 8
move 2 from 1 to 5
move 11 from 6 to 1
move 14 from 2 to 6
move 2 from 4 to 3
move 1 from 2 to 9
move 3 from 2 to 9
move 20 from 6 to 5
move 2 from 4 to 2
move 4 from 9 to 1
move 8 from 8 to 9
move 1 from 6 to 9
move 14 from 5 to 2
move 10 from 2 to 7
move 7 from 9 to 6
move 1 from 6 to 8
move 6 from 2 to 6
move 1 from 2 to 5
move 1 from 3 to 5
move 9 from 6 to 3
move 1 from 5 to 2
move 9 from 7 to 3
move 12 from 3 to 2
move 9 from 5 to 9
move 1 from 8 to 6
move 3 from 3 to 5
move 1 from 7 to 6
move 14 from 2 to 6
move 3 from 9 to 7
move 6 from 1 to 2
move 5 from 1 to 8
move 10 from 6 to 9
move 4 from 5 to 6
move 3 from 2 to 4
move 9 from 9 to 7
move 1 from 8 to 7
move 3 from 9 to 6
move 3 from 3 to 7
move 1 from 5 to 1
move 15 from 7 to 1
move 2 from 8 to 5
move 2 from 5 to 4
move 1 from 7 to 4
move 1 from 3 to 1
move 15 from 6 to 7
move 2 from 4 to 9
move 3 from 4 to 7
move 18 from 1 to 6
move 1 from 8 to 9
move 6 from 9 to 7
move 3 from 6 to 8
move 1 from 1 to 2
move 2 from 9 to 5
move 2 from 2 to 9
move 16 from 6 to 3
move 15 from 3 to 7
move 2 from 8 to 4
move 1 from 3 to 7
move 3 from 4 to 9
move 2 from 1 to 9
move 26 from 7 to 4
move 1 from 2 to 1
move 7 from 9 to 8
move 1 from 2 to 5
move 2 from 5 to 2
move 8 from 7 to 5
move 1 from 7 to 3
move 1 from 3 to 9
move 2 from 2 to 7
move 1 from 6 to 4
move 4 from 8 to 9
move 1 from 1 to 3
move 1 from 5 to 6
move 2 from 5 to 7
move 17 from 4 to 9
move 6 from 4 to 9
move 1 from 3 to 4
move 6 from 7 to 9
move 3 from 5 to 6
move 2 from 7 to 9
move 4 from 8 to 9
move 4 from 6 to 4
move 8 from 4 to 6
move 1 from 8 to 4
move 3 from 5 to 2
move 2 from 4 to 3
move 1 from 7 to 9
move 2 from 3 to 5
move 4 from 6 to 9
move 1 from 6 to 1
move 36 from 9 to 4
move 2 from 5 to 3
move 3 from 2 to 1
move 3 from 1 to 4
move 14 from 4 to 1
move 1 from 8 to 5
move 4 from 1 to 3
move 5 from 9 to 5
move 2 from 5 to 8
move 1 from 8 to 9
move 4 from 9 to 6
move 3 from 5 to 8
move 1 from 5 to 6
move 2 from 1 to 6
move 2 from 9 to 7
move 6 from 6 to 4
move 1 from 1 to 3
move 29 from 4 to 6
move 7 from 3 to 4
move 1 from 8 to 9
move 3 from 1 to 6
move 4 from 1 to 4
move 1 from 8 to 4
move 4 from 4 to 3
move 15 from 6 to 8
move 9 from 4 to 9
move 1 from 7 to 9
move 8 from 8 to 3
move 3 from 6 to 7
move 1 from 1 to 2
move 4 from 7 to 6
move 7 from 8 to 5
move 1 from 8 to 4
move 2 from 5 to 7
move 1 from 2 to 4
move 5 from 6 to 1
move 4 from 3 to 2

177
2022/day5/src/main.rs Normal file
View File

@@ -0,0 +1,177 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{self, newline};
use nom::error::Error;
use nom::multi::separated_list1;
use nom::sequence::{delimited, preceded, separated_pair};
use nom::Parser;
#[derive(Clone, Debug, Default, Copy)]
struct Crate<'a> {
label: &'a str,
}
impl<'a> Parser<&'a str, Self, Error<&'a str>> for Crate<'a> {
fn parse(&mut self, input: &'a str) -> nom::IResult<&'a str, Self, Error<&'a str>> {
let (input, label) = delimited::<&str, _, &str, _, Error<&'a str>, _, _, _>(
tag("["),
complete::alpha1,
tag("]"),
)(input)?;
self.label = label;
Ok((input, *self))
}
}
#[derive(Clone, Debug, Default, Copy)]
struct GameMove {
quantity: usize,
from: usize,
to: usize,
}
impl<'a> Parser<&'a str, Self, Error<&'a str>> for GameMove {
fn parse(&mut self, input: &'a str) -> nom::IResult<&'a str, Self, Error<&'a str>> {
let (input, quantity) = preceded(tag("move "), complete::u8.map(|x| x as usize))(input)?;
let (input, from) = preceded(tag(" from "), complete::u8.map(|x| x as usize))(input)?;
let (input, to) = preceded(tag(" to "), complete::u8.map(|x| x as usize))(input)?;
self.quantity = quantity;
self.from = from;
self.to = to;
Ok((input, *self))
}
}
#[derive(Debug, Default)]
struct GameBoard<'a> {
_labels: Vec<String>,
board: Vec<Vec<Crate<'a>>>,
}
impl<'a> GameBoard<'a> {
pub fn game1_move(&mut self, m: &GameMove) {
let v = &mut Vec::new();
let work = self.board.get_mut(m.from - 1).unwrap();
for _ in 0..m.quantity {
v.push(work.pop().unwrap());
}
let work = self.board.get_mut(m.to - 1).unwrap();
for _ in 0..m.quantity {
work.append(v);
}
}
pub fn game2_move(&mut self, m: &GameMove) {
let v = &mut Vec::new();
let work = self.board.get_mut(m.from - 1).unwrap();
for _ in 0..m.quantity {
v.push(work.pop().unwrap());
}
v.reverse();
let work = self.board.get_mut(m.to - 1).unwrap();
for _ in 0..m.quantity {
work.append(v);
}
}
fn get_tops(&self) -> String {
self.board
.iter()
.map(|x| x.last().unwrap().label.clone())
.fold(String::new(), |acc, x| acc + x)
}
}
impl<'a> Parser<&'a str, Self, Error<&'a str>> for GameBoard<'a> {
fn parse(&mut self, input: &'a str) -> nom::IResult<&'a str, Self, Error<&'a str>> {
let (input, crates) = separated_list1(
newline,
separated_list1(
tag(" "),
alt((tag(" ").map(|_| None), Crate::default().map(Some))),
),
)(input)?;
let (input, _) = newline(input)?;
let (input, labels) = separated_list1(
tag(" "),
delimited(tag(" "), complete::u8.map(|x| x.to_string()), tag(" ")),
)(input)?;
let (input, _) = newline(input)?;
//self._labels = labels;
let mut board = vec![Vec::new(); crates[0].len()];
for cols in crates {
for (col, c) in cols.iter().enumerate() {
if c.is_none() {
continue;
}
board[col].push(c.unwrap());
}
}
board.iter_mut().for_each(|col| col.reverse());
self.board = board;
let b = GameBoard {
_labels: labels,
board: self.board.clone(),
};
Ok((input, b))
}
}
fn parse_input(input: &str) -> nom::IResult<&str, (GameBoard, Vec<GameMove>)> {
separated_pair(
GameBoard::default(),
newline,
separated_list1(newline, GameMove::default()),
)(input)
}
fn part1(input: &str) -> String {
let (_, (mut board, moves)) = parse_input(input).unwrap();
for m in moves {
board.game1_move(&m);
}
board.get_tops()
}
fn part2(input: &str) -> String {
let (_, (mut board, moves)) = parse_input(input).unwrap();
for m in moves {
board.game2_move(&m);
}
board.get_tops()
}
fn main() {
//Read in file
let file = fs::read_to_string("input").unwrap();
//read in the parts of the file
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &str = " [D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2";
#[test]
fn part1_works() {
assert_eq!(part1(INPUT), "CMZ");
}
#[test]
fn part2_works() {
assert_eq!(part2(INPUT), "MCD");
}
}

7
2022/day6/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day6"
version = "0.1.0"

11
2022/day6/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "day6"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools.workspace = true

1
2022/day6/input Normal file
View File

@@ -0,0 +1 @@
mgwwjddqzqdqsstctjjsdjsdsrsfsmfsfwwltwlwhwnhhlffzddgffwlffbsfshfshhgvvdrrltlzlnzznrrnrsnnhgnnfjnnvpnnbjjnwwrcwrrhlhvlhhmzmqzqrqtqmqpmpwwmssgsrgrgtgmtgmtgtdtvdvmvsvsbvsbvbtthmmftmmdnmddcrcvcrrfjfhhfjhffjllcpllmcctjtrttwmtwmwffrlrqlqzzpddsqdqqgjqgjgngwnncjnnvsnswwbzbtzzflzzqsqbsbvbmbnnjpnpnnpfpmpmnpmmjljtltssqnsqslstswtwswwjddvmmzlzqlzqzqjjlttmtrtbtmtgmtmsttrctrrsqrqvvrzrcrhhlnhllbfbtthrhdhllmwlmlgglgsgmgsmszzprpwpfprfftffpssjzjgzjzddqfqmmwqwvwlvlqqtbtwwrwttmsmppbmmpcmctcnnhssnjncnlcnctcjjrzrwrfwfcwffczztrtsrtstlsssljssmvssjzssrqqrcqqwlqwlwffsflssrrzhzzhrzzdgdppspwplpqptttvddggzszccrrnzzwwdwjddrvvwggpvgpvvhdhqddffrnngcncjcjlcchrrftrrjccrcrqqgcglcgcscmmlzmmtcmcffwfcfrcrggdmggdvvnrvnnphnngzzpdpgpspqqgrrnffmfpmffmgfmmjmzztlljlggljjcnnrqnqpnqndnffnwwbpwpjwjjlslmsmtmtjttsvsggrmmdpmmcjjswsqqwfwwrwffczfzggqvggdlldhllsdsfdsdhhmmzmjjmpjpddsccqrrjhjlhjjcnnpwnnffjwwcsszrrnmnsmnnjbnndwnnnhnwwjtwtlwtwqqbnqnbnqqfjfdjdbbwbqwqpqggbcbhhtrtqrrddpdwdlwdddzvzwvvdfdpdcdvdtdpttwwdzdzmdmqmzmnzmnmhmwmjwwshhcqcpcvvzgzdggnjnnhwnhhswwvccqrqlqggcngnmggmffblbglltlstshhrjjlvlppsqslljtjtgglvltvlvmllhrhdrrmqrmqmjjdcjjppqwwllvsvsrszslsvvghvvhmmfbfvfpfmmvdvppwggtrrjvvsbbzffbmffpqqqhnhncclzczwcwpwssrprfrsrbsbnbvnnwzwqqpsqsspmssztssstrtcczsznzvvpvttnssdjdhjddngdgvvmsszbzsbsmbbgsbgsgmmhwhghrhjhphshchgglmlvlhlbhlldwdggdsscvcbcssfbbvggvwwtstltrrwttjdtdvttlsttfhfmhhcbclbcbffqqslshlldhdqhhjwwlffrbrdbrrgcrrfmffbhhlslrslrslrlsrsnsnvvqfqnnfdfmfmttmcmcrrcmmmjttjvtvvjbjqjnnbtbnblbtlblplgltlqltlztzvvtdvvtpvvwdwfflbflfrrhbrbbmjmcjmccztzwwjzwwzwdzdnnwcclbllqgghjhlhthwrdglrmcpbmtrnrdtvjrpmzqmljzzrtpzsrhnjrsdmpnsgdhvqchcfqjqdncjqfnscwjqvszpzzfhpjljmvsqnjzmrsgsbzlvrddtdmwbwwgprlvdfflrpztdzrhtmlzrrtdmpmcprqzzwlnmfjvsrltfjgcnnfllnzmbjcbthvbffczsspmczrpgpdjmvrvfmprfmnqdcnfwwvgdrwvrbtlqmhrrjvtrmmgrlprtnzdlszgbtbwztdrmpmlfblshzcnsczlblgwzrpnlccwhmcqhssmpznbdnnqgzzmjprjttdjhmjbmgqvzblsjwmplzsthrswhsdbvtqgrfzmbpqtpqgqdqcvzlgjrtvrhvzgmcmrwdmfpdvjddsmmsnvrdgnsbsdzcbprbqchqcgnwmfsrmqtrcdhdtzztbvmpblftwqlmlmmjcjhhjlgnnhljnncvbnjhgbjrltlwscswgvqmcnssbcdrtbgnhgmpmvjwtrbrbrdbdqfrncvhdstwztwcpbjrjwzmdlwvlvmsrhghjwjnjstbcqjqtjrgcvhzjdhdgbgdlhvjmztwvhgzzggwwhhhzvtrldchztmwfjvnqnvhnwpfvzzvnlvsccmvsngzgtnttssmdmhwzlhtpnfhczsdfnrstbwvwpqmslcvpvhfzttzhsgzpbhqdtswshljpncznjhzmgvvbcllmzprhrvwljwcjpcdqmwbzvsdcgtmwnrhswsgqhwpwhbjpnhnpjvgsqcjltzrqvqfflcdcvpwnznvtqbfbtlpmtdgbbwdwncqsqnbtgfdzzqzzvjnwmzdmlgstmnjwznjqghglvmwjzlqrnddcqhgndlhlbmqdhrqgrjqztnhpzssnwmrqclmwpgbvfrvgqqvtthznsqwgndjrprbgrhcvhpzbfhdmgnhsrqjvjstbtmnltsbjfzczvjqnhtldqclsflbhvvlzjwrqqgbgpwqwpfjctqpzdqwcfstmwbzgrgrtzngljjnvtggrqcbgjwtqsdgwmfjqppnzgfsfdmlctztbhnntnntdlvrsdvnllvmpggjzspqfhzwrttwzpqrnqjhmpjnmrzrpnqzshcqgctbtflqflcrzpmnphgbbghhwzplljwngbtffwmrwggdztvtfgwldlswqvjptvbfvnbpglhgrdgcfmvrslqldmwjqvjpvwgpjddvglllvpqwvbchqsmjrncgvgmqbsbcwfbsbpqcqzjfpcdzszgmvqgqjlflpfzbsrhsrzrdbpssrjbcfhvztftlzqpsglpwhbscgwdlbgghzsbwznnbgnnsgjghmmpmmrmqmdhnflgvgprqfcbpzbcpjscvnpfrmtvzsbflmffvcfsvdsggzdqtppcjzphcqwrqtrczqmwcdmdqndzmhdpnfqsbndnvjlzrsjzmpcrfgjwccsdtzvslccwhlvzjwjgvwpsnsggmqgsjfbwmjstsgnqmtjhljvfnflnngdrqvscwlqqdsglhghczhjdvgrjcqblmncdbjvsbwgptgpvvzhcjgjnvttrgzrjnqlvfbrmpzdcbbnnrqptpzpssznbsrstdphbgdrsnrhcjwwgsncdzvqfnmnvqcmcgdgjdbqjzdrvvbvhjdfcqndmqwscmsvppclzrhgbldqtwctbdhpbbwfvwpcpsvddmrhqbhlrrmrblnmqqqbwvcwwbwprlmhtdncmjhmjgphmrrhcdrqgmcrzwsznqzpngbtsvjgglrddhjflbrhvqwmmhmqzhphwnvqwzczdvqjsnlhfqbcgddtwgnlcgbfqmzfpqmnbpvfhdhjlnwtrlmggtbfnfvmqrzjvjjvffctsrwgfcpghhnzqmwtlsfhjrvqpwqhngrhpswslsvtgnbvbmwsfwmpntfsfpshrjzvghhpvnlbmnrhltfpmqdwzfhztvhlmbnmhnbvdzbbtczvwbvwtvjghhjjrtgbrqrhmbgvssstdwztdmdsqtctghjhsnpslqttdlvndmjfnmdzwrblfjqcwptfttvlcgsvwcbmfzbdlmrtchgqlfspwznbzfjthjtfwshqgfsfdsmzsmpptzschlzjshvfwtmpszvrvlggbrgpcnqwndhjjprztdfddblhfljbvttfvhchhdfsftrhccrbncmhwpcpwfqthngcqptmvsmpcswdrdlcbqvvhwmcqqwbzlblrgfcrrndwdvlvnpjvwchzjzmgrqhzzmgqqdsdflpclpdtlhvhcthzjfbvjvzsnbvwfsnglvbnwnbgrqwpbgclhjhztttbjwvmlmmgmzncbwswncqhmcfjfnwnpbrmchhpgwngrfwgdfdqmblwlghdjvdhjftdblrtcvvgbvpmbjhfwgpmghqbqrcpgfvhtvqtlbjdblggcpjzlrhpbsqwntfhbhwwszpdlsgbpfqhvrjrhsldcgvqhqmwdfcrcmhrvvwvbrfsrrcvwzhqqvgltlnhwhdrhrdqsvmdzjwgmqdsccwhcgwltfhdfqpsltjccwsttmrc

70
2022/day6/src/main.rs Normal file
View File

@@ -0,0 +1,70 @@
#![warn(clippy::all, clippy::pedantic)]
use std::fs;
use itertools::Itertools;
fn work(input: &str, count: usize) -> String {
let array = input.as_bytes();
for x in count..array.len() {
if array
.iter()
.enumerate()
.filter_map(|(pos, val)| {
if pos >= x - count && pos < x {
Some(val)
} else {
None
}
})
.unique()
.count()
== count
{
return x.to_string();
}
}
panic!("stuff should have been gotten {input}")
}
fn part1(input: &str) -> String {
work(input, 4)
}
fn part2(input: &str) -> String {
work(input, 14)
}
fn main() {
//Read in file
let file = fs::read_to_string("input").unwrap();
println!("Part 1: {}", part1(&file));
println!("Part 2: {}", part2(&file));
}
#[cfg(test)]
mod test {
use super::*;
const INPUT: &[(&str, &str, &str)] = &[
("mjqjpqmgbljsphdztnvjfqwrcgsmlb", "7", "19"),
("bvwbjplbgvbhsrlpgdmjqwftvncz", "5", "23"),
("nppdvjthqldpwncqszvftbrmjlhg", "6", "23"),
("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "10", "29"),
("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "11", "26"),
];
#[test]
fn part1_works() {
INPUT
.iter()
.for_each(|(test, ans1, _)| assert_eq!(part1(*test), *ans1))
}
#[test]
fn part2_works() {
INPUT
.iter()
.for_each(|(test, _, ans2)| assert_eq!(part2(*test), *ans2))
}
}

7
2022/day7/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day7"
version = "0.1.0"

10
2022/day7/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "day7"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1083
2022/day7/input Normal file

File diff suppressed because it is too large Load Diff

220
2022/day7/src/main.rs Normal file
View File

@@ -0,0 +1,220 @@
#![warn(clippy::all, clippy::pedantic)]
use std::cell::RefCell;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::rc::Rc;
trait Sizer {
fn size(&self) -> usize;
}
impl Sizer for Vec<usize> {
fn size(&self) -> usize {
self.iter().sum()
}
}
impl<T: Sizer> Sizer for Vec<T> {
fn size(&self) -> usize {
self.iter().map(Sizer::size).sum()
}
}
#[derive(Clone, Debug)]
struct MyFile {
_name: String,
size: usize,
}
impl Sizer for MyFile {
fn size(&self) -> usize {
self.size
}
}
#[derive(Clone, Debug)]
struct MyDir {
name: String,
objects: Vec<FileSystemTypes>,
parent_dir: Option<Rc<RefCell<MyDir>>>,
}
impl MyDir {
fn move_up(&self) -> Option<Rc<RefCell<MyDir>>> {
self.parent_dir.clone()
}
fn move_down(&self, dir: impl Into<String>) -> Option<Rc<RefCell<MyDir>>> {
let dir = dir.into();
Some(
self.objects
.iter()
.filter_map(|x| match x {
FileSystemTypes::MyDir(y) => Some(y),
FileSystemTypes::MyFile(_) => None,
})
.find(|x| *x.borrow().name == dir)?
.clone(),
)
}
fn touch(&mut self, name: impl Into<String>, size: usize) {
self.objects.push(FileSystemTypes::MyFile(MyFile {
_name: name.into(),
size,
}));
}
//has to be a static method...
fn mkdir(self_: &Rc<RefCell<MyDir>>, name: impl Into<String>) {
self_
.borrow_mut()
.objects
.push(FileSystemTypes::MyDir(Rc::new(RefCell::new(MyDir::new(
name,
Some(self_.clone()),
)))));
//me.clone()
}
fn new(name: impl Into<String>, parent_dir: Option<Rc<RefCell<MyDir>>>) -> Self {
let name: String = name.into();
MyDir {
name,
objects: Vec::new(),
parent_dir,
}
}
}
impl Sizer for MyDir {
fn size(&self) -> usize {
self.objects.size()
}
}
#[derive(Clone, Debug)]
enum FileSystemTypes {
MyFile(MyFile),
MyDir(Rc<RefCell<MyDir>>),
}
impl Sizer for FileSystemTypes {
fn size(&self) -> usize {
match self {
FileSystemTypes::MyFile(file) => file.size(),
FileSystemTypes::MyDir(dir) => dir.borrow().size(),
}
}
}
fn recurse_part1(collector: &mut Vec<usize>, cwd: &MyDir) -> usize {
let cwd_size: usize = cwd
.objects
.iter()
.map(|x| match x {
FileSystemTypes::MyFile(y) => y.size(),
FileSystemTypes::MyDir(dir) => recurse_part1(collector, &dir.borrow()),
})
.sum();
collector.push(cwd_size);
cwd_size
}
fn process_part1(input: &str) -> String {
todo!()
}
fn process_part2(input: &str) -> String {
todo!()
}
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let reader = BufReader::new(file);
//create root file object on heap
let root = Rc::new(RefCell::new(MyDir::new("/", None)));
let mut ls_mode = false;
//set a pointer to the currently on MyDir, in this case start at roo
let mut cursor = root.clone();
// loop through the input files lines
reader.lines().for_each(|line| {
//need to unwrap the line cause lines() returns an Option
let line = line.unwrap();
//if we are listing files we need to get the information from the input
if ls_mode && line.as_bytes()[0] != b'$' {
//do adding to cursor
match line.split_whitespace().collect::<Vec<_>>()[..] {
["dir", name] => MyDir::mkdir(&cursor.clone(), name),
[size, name] => cursor
.borrow_mut()
.touch(name, size.parse::<usize>().unwrap()),
_ => panic!("oops {line}"),
};
// end the for_each
return;
}
ls_mode = false;
//parse all other lines as commands
cursor = match line.split_whitespace().collect::<Vec<_>>()[..] {
["$", "ls"] => {
ls_mode = true;
cursor.clone()
}
["$", "cd", "/"] => root.clone(), //set current directory back to root
["$", "cd", ".."] => cursor.borrow().move_up().unwrap(),
["$", "cd", dir] => cursor.borrow().move_down(dir).unwrap(),
_ => panic!("unknown command {line}"),
}
});
let mut part1 = Vec::new();
let max = recurse_part1(&mut part1, &root.borrow());
let part1_ans: usize = part1.iter().filter(|x| **x <= 100_000).sum();
println!("Part 1: {part1_ans}");
//part 2
let free_space = 70_000_000_usize - max;
let needed_clear_space = 30_000_000_usize - free_space;
let part2 = part1.iter().filter(|x| **x >= needed_clear_space).min();
println!("Part 2: {}", part2.unwrap());
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k";
#[test]
fn part1_works() {
assert_eq!(process_part1(INPUT), "95437");
}
#[test]
fn part2_works() {
assert_eq!(process_part2(INPUT), "24933642");
}
}

25
2022/day8/Cargo.lock generated Normal file
View File

@@ -0,0 +1,25 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day8"
version = "0.1.0"
dependencies = [
"itertools",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]

11
2022/day8/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "day8"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools.workspace = true

99
2022/day8/input Normal file
View File

@@ -0,0 +1,99 @@
131102031022411233124153000402524132243245422402002121234345453535321113450535302430144220243210033
210030011133442111442145434004231353202400601331445042106230540242340014332243105022302133441101011
201302044230110302341330012044051130055046354663404014033243655520410125432425532024143403330032232
210303103204034020151134514120046162664361356330261302226530654326452225134535222440444443410101130
021310332113230035544033314213544254421233042151226601345516101654005025035545035333140023212102212
220140301231432453044535532323066560244052510442522465306425021511260132313424322110345422300411332
103013240142011245425311001542145655516105003352455141424412415222100145355043202120423034202120403
314103304322445325255534650156565303510461206034672755654106425161056213552330050310445352323213402
243430113305332145305400366156126611120357766767667531265661631510215134106224240155532142014202020
344044011305120500223111213354316112154646744635155132374774442641061323024416155344101304242413014
210001340311202411013214565026366577746364242672331436164352252445110000414212215202321411504422400
001030344254033552521244445311464542617442572153467545366653342426676363026234406640015310103000111
123004351234415456501532136126535464236513321263256111263236363424471316560661104630322511135024314
243444301010130503501065440067414335615573435151661324666436573133444251763541254166141130405531401
301333054435342646553265615125271561444757162666247563564724434377461174733226461663122454115301022
313333115034033032461152521554721624436564356545874583322623147632365263427634464233444421123452114
434441324240044110131332722354256132333522657745624745724323263635746316535120231124164652542245541
020013043025261604606066777717675561448878622688884553346728286853164272251311263510330645452212040
225405401530036646635664123763243188844858643328244728344354343287813231363771264361344552214134043
444013205241103641532477643143435563484567276423226466226434857664377176552653732212146514023430325
235444033555452351644577725572733823223422438445886848336238372267477767173715122521014452405344110
022222500504231055646646534633724257646568684735488756288737857786232268232655151633323530463425151
412102254551210100735562467375656382325864659764674335534465738553284326347575542675435335613154320
202304233522424114651647265763556544532358373769578879369385574555386666783363244334360301665155255
400235422666401262236327746826382638573848989589958764697958946664233887757677777164614616200413302
413211350405512664175712772346875822558798479568486643787487763573785465875336133314155235265153544
412112650043523364626154522655787734369585968849884958638795656846722736645822544573361050432101002
040151232015244534675233447267274466883354564933934639355395374945757347634723116764261154205631025
053511566106055552733487866435428673379594867554589579573759867856436754328455243315565403452366550
402441261342655261311663778728277373873487457867467678497684746937764462833722831553364205512206541
401560462601374333165687778774966694644847875679669694796453568554383377242683474675113640166014002
245431640441276372256235534578677594558499466595957658548574676357557473852362834636123725105611001
140325466637452621767678632447835458776865576466546548894776445753494487385328422645157221103664133
253440020224216474125727543697395446885846849486744757588954688447585337348374328857266123303626032
522360463154354425644334277863647479565896696996775849896649575745746557964548536537415577142552050
402351634474624167474857559553949899894799879644496759587555549787974996993634823833353424316316042
414140006077774645384224689955449865767867876647679668857949995687773539976755288762714471242526254
032242255462211412258653836566746764685477947565799869886769647489953985859547537844111624535631156
241646435225236526565228495485878686676559956669957869665758947786478934645637286347261114426145563
512215356337536722737525437473645894558456658976958699776986594778559569864962457825216143344215016
322625244746336257345525895486859454957989669585766859958667775649875866654855238722721513671212305
044600535247217686848675385837958687859856856865869679556868995579875763985654342678317263541605554
430320424173321746345287493633447589689698796959599858768985856685945573454358785585284454656334025
232026013361136674773333583555589876467998965697988698997966777468454666855894285223681327425505166
354332536226232536453676489538955896788985786767799998876886965757444485955456462568453444425456521
016154147271566788586769489946847647867857556667888797775895768988576579978395627453672756147414326
463454162462563733454784956555887864475857986666689767978699985965647598648778424456255355723655466
021001072567511684363734454496747746785559778677966778897776558954997794766634668356686124441524525
234605435156344755776859794859864756797769876888968689787579998559856654953446748662382433262116262
030666223611312438477278595797684446757958596668696976678757986797965899875556377876865147373650335
245046541536764737224694794369857786796969878968689968797958865856864988656773724353268427134461620
265066622261247542566375373496969757789659688887789988798956868878668565665794723743572475263161166
422245453144736877586299945587888888767997586686876996879786788998867747683776448657845662361505315
210351625547722865278485967857788659769765586689699988976775596895984657648869967252632246417436621
141004614422116428487735355858968995455599988699767679796595755979678769336598642342483777532511004
533362561377622426377686383498446767487997987976886899988585956569875694463399574483265564264622314
456163507723454276868637866355647859456998677889778989889969998954684957667483346372863274465245646
030644251421316636263479555487765499846688669798677798668968599994746869757876565775844144255106462
510135336311735337436764935399965949469956999767696876898578576969547984463869253278467243311543030
466524526117724766584368696865578944888566858759789698699778689456856774865884337672615767136523254
254110631463762333865539645936466585858655857657659586556989585456479575455563845762632676471423233
562434653773762262468663566995359545869887797866957588766855659579865834576343438465353323754454240
112066240641477224475587449447989749648974898785555767886979489566644938356955638286634724441631625
330443313353347713822865768695549469777556759786896565758769775757475566378642426537546311412140051
040065350247621726448577745444446666557767548565886798996759654466566755698345662584625446342463260
330536625076531353825376746548345995946649894668956867559446757474398638639624464682343654214032311
530251120326432762683357643388548466489576697998869899549584977659746869733837538723573545213461105
255356015316555756432734833639785737748988987965669458947565666746847669548847254811135737404226043
454354550123557227257454326863468487794448666674495859497588676766356646553276336612337324063553462
144340525160235512164242786385886753536996894579865659848488534457479379684556654637432614663443613
305316054035171372138527636345993984687638579458566486795488343446895753373444445772513544116215313
543055400456632262131666826787874497775946535566897457748984377394483964377555447341647525602315351
412546632444321676125286364645444448636496473746537653585847369856994564666782226566264742530022340
044233444612165527122563533685825959374668734768758953663536843946435452847223172365277563546141540
340030454102343151253211647666557354334348799955649644348678747758347287426221514313112316663165454
011020051334501654522762446268866586389746656669789443696677886848328248582411315534364123146250501
305413453334030464614216442476475733478966457359966737766476988846463467574624373625606065562242335
034215112203016427473135421577622448726677469646375339565639944867546662453345521671403262325021214
132210543140531252452317342463454885388566728556533533877354846446665578244115675240031664244524501
201140051634662646614612354714626525722377445426842485568735384726362363654642742661135663265002510
025011353243541235316415314133578633558245363633567643474723462264656821637416273202656565602515322
121540314222665224011553176734444466578255884322834667368468238484254776573264467333645106223352155
233134314541654641520531771126551528746557387438726348653238283875537147555615754035502132555444301
424510000505066600303343337525732714723283556468834467558232644677477536766533666404266150255104242
041433452552331436330125237255465337374884372735358382327855756643676341445255435062065033314033212
141025211241314363206626056633223121353133645783244374622462256271457773212200625560321421020100213
224101010422251643133220616233621223532466525114754865347141165612135423157400060111225313054434032
004040052225144131342364656025377722754756627621767354111575442445436452322335541156250421012350243
043322120521041516241540153402233524371432721521521276133447761312616332565246332656140342120501432
200232013354533102134562014146621725413251577715376671261144475237357064044430050234040121014304202
011420212054031401033241133121106532344212712314572323662643147467661343340045361122545415103011124
102223410325432324313243446514164004156475643254444557477323147134144354265066260422110450012141414
031411112311321035125554420606413626050373327654774373667767140605036115333551650220414205214132024
210204032200320524351051665115025502044361230251377575706601164652440120455165201034431244211004301
331131223444034015143142430503454436431142412165246111316106164251614223504203533022310134023423343
120300034332102045152234220042512653414152606411234665213124345325420542210314454221024423343223220
200230420442302210441134513143532363040132120013505235035543064245636124512505054404233133430423011
000202411243122420140310501035333204626352325031215630345206242502134153000033354044334042221310101
111301302244231442013332415144033330063530643146335532313306033656410450441505505240142210004121122

161
2022/day8/src/main.rs Normal file
View File

@@ -0,0 +1,161 @@
#![warn(clippy::all, clippy::pedantic)]
use itertools::Itertools;
use std::fs::File;
use std::io::{prelude::*, BufReader};
fn get_board(file: &File) -> Vec<Vec<u8>> {
let reader = BufReader::new(file);
reader
.lines()
.map(|line| {
line.unwrap()
.matches(char::is_numeric)
.map(|num| num.parse().unwrap())
.collect()
})
.collect()
}
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let board = get_board(&file);
let y_len = board.len();
let x_len = board.iter().map(std::vec::Vec::len).max().unwrap();
assert!(board.iter().any(|x| x.len() != x_len), "board isn't square");
let mut visible: Vec<(usize, usize, u8)> = Vec::new();
let mut max_in_row_from_left = vec![0_usize; y_len];
let mut max_in_row_from_right = vec![x_len - 1; y_len];
let mut max_in_col_from_top = vec![0_usize; x_len];
let mut max_in_col_from_bottom = vec![y_len - 1; x_len];
let mut scores: Vec<Vec<usize>> = vec![vec![0; x_len]; y_len];
for y in 0..y_len {
for x in 0..x_len {
// Part 1 stuff
let y_inv = y_len - 1 - y;
let x_inv = x_len - 1 - x;
let tree_from_top_left = board[y][x];
let tree_from_bottom_right = board[y_inv][x_inv];
let max_from_left = board[y][max_in_row_from_left[y]];
let max_from_right = board[y_inv][max_in_row_from_right[y_inv]];
let max_from_top = board[max_in_col_from_top[x]][x];
let max_from_bottom = board[max_in_col_from_bottom[x_inv]][x_inv];
if tree_from_top_left > max_from_left || x == max_in_row_from_left[y] {
visible.push((x, y, board[y][x]));
max_in_row_from_left[y] = x;
}
if tree_from_bottom_right > max_from_right || x_inv == max_in_row_from_right[y_inv] {
visible.push((x_inv, y_inv, board[y_inv][x_inv]));
max_in_row_from_right[y_inv] = x_inv;
}
if tree_from_top_left > max_from_top || y == max_in_col_from_top[x] {
visible.push((x, y, board[y][x]));
max_in_col_from_top[x] = y;
}
if tree_from_bottom_right > max_from_bottom || y_inv == max_in_col_from_bottom[x_inv] {
visible.push((x_inv, y_inv, board[y_inv][x_inv]));
max_in_col_from_bottom[x_inv] = y_inv;
}
//part2 stuff
//search left
let right_part = board[y].iter().skip(x).copied().collect::<Vec<u8>>();
let right_score = if right_part.len() > 1 {
let score = right_part
.iter()
.skip(1)
.copied()
.take_while(|tree| *tree < tree_from_top_left)
.count()
+ 1;
if score + x >= x_len {
score - 1
} else {
score
}
} else {
0
};
// println!("Right: {}, {:?}", right_score, right_part);
let left_part = board[y].iter().rev().skip(x_inv).collect::<Vec<_>>();
let left_score = if left_part.len() > 1 {
let score = left_part
.iter()
.skip(1)
.take_while(|tree| ***tree < tree_from_top_left)
.count()
+ 1;
if score >= x {
score - 1
} else {
score
}
} else {
0
};
// println!("Left: {}, {:?}", left_score, left_part);
let down_part = board.iter().map(|row| row[x]).skip(y).collect::<Vec<_>>();
let down_score = if down_part.len() > 1 {
let score = down_part
.iter()
.skip(1)
.take_while(|tree| **tree < tree_from_top_left)
.count()
+ 1;
if score + y >= y_len {
score - 1
} else {
score
}
} else {
0
};
// println!("Down: {}, {:?}", down_score, down_part);
let up_part = board
.iter()
.map(|row| row[x])
.rev()
.skip(y_inv)
.collect::<Vec<_>>();
let up_score = if up_part.len() > 1 {
let score = up_part
.iter()
.skip(1)
.take_while(|tree| **tree < tree_from_top_left)
.count()
+ 1;
if score >= y {
score - 1
} else {
score
}
} else {
0
};
// println!("Up: {}. {:?}", up_score, up_part);
let tree_score = right_score * left_score * down_score * up_score;
// println!(
// "({}, {})({}) = {} = {} * {} * {} * {}",
// x, y, tree_from_top_left, tree_score, up_score, left_score, down_score, right_score
// );
scores[y][x] = tree_score;
}
}
let part1 = visible.iter().unique().count();
println!("Part 1: {part1}");
let part2 = scores
.iter()
.map(|x| x.iter().max().unwrap())
.max()
.unwrap();
println!("Part 2: {part2}");
Ok(())
}

7
2022/day9/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day9"
version = "0.1.0"

10
2022/day9/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "day9"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2000
2022/day9/input Normal file

File diff suppressed because it is too large Load Diff

69
2022/day9/src/main.rs Normal file
View File

@@ -0,0 +1,69 @@
#![warn(clippy::all, clippy::pedantic)]
use std::collections::HashSet;
use std::fs::File;
use std::io::{prelude::*, BufReader};
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let reader = BufReader::new(file);
let mut snake = [(0_i32, 0_i32); 10];
let mut visited: Vec<HashSet<(i32, i32)>> = vec![HashSet::new(); 10];
//intialize the visited
snake.iter().enumerate().for_each(|(i, x)| {
visited[i].insert(*x);
});
//read in input
reader.lines().for_each(|line| {
let line = line.unwrap();
let (direction, steps) = match line.split(' ').collect::<Vec<&str>>()[..] {
[dir, step] => (dir, step.parse::<usize>().unwrap()),
_ => panic!("failed parseing line {line}"),
};
for _ in 0..steps {
let (mut cur_head_x, mut cur_head_y) = snake[0];
match direction {
"L" => cur_head_x -= 1,
"R" => cur_head_x += 1,
"U" => cur_head_y += 1,
"D" => cur_head_y -= 1,
x => panic!("invalid movement {x}"),
};
let new_head_pos = (cur_head_x, cur_head_y);
snake[0] = new_head_pos;
visited[0].insert(new_head_pos);
for i in 1..snake.len() {
let (cur_head_x, cur_head_y) = snake[i - 1];
let mut new_pos = snake[i];
let (cur_tail_x, cur_tail_y) = new_pos;
let mut x_offset = cur_head_x - cur_tail_x;
let mut y_offset = cur_head_y - cur_tail_y;
if std::cmp::max(x_offset.abs(), y_offset.abs()) > 1 {
if y_offset.abs() >= 2 && x_offset == 0 {
y_offset = y_offset.clamp(-1, 1);
} else if x_offset.abs() > 2 && y_offset == 0 {
x_offset = x_offset.clamp(-1, 1);
} else if x_offset.abs() > 1 || y_offset.abs() > 1 {
x_offset = x_offset.clamp(-1, 1);
y_offset = y_offset.clamp(-1, 1);
} else {
x_offset = 0;
y_offset = 0;
}
new_pos = (cur_tail_x + x_offset, cur_tail_y + y_offset);
}
snake[i] = new_pos;
visited[i].insert(new_pos);
}
}
});
let part1 = visited[1].len();
println!("Part 1: {part1}");
let part2 = visited[9].len();
println!("Part 2: {part2}");
Ok(())
}