Make 2020 a single cargo project

This commit is contained in:
kageru 2020-12-08 11:32:59 +01:00
parent f0968f0dff
commit 18ea3c01cb
27 changed files with 34 additions and 118 deletions

View File

@ -1,10 +0,0 @@
[package]
name = "day01"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"

View File

@ -1,15 +0,0 @@
from itertools import combinations
with open('input') as f:
lines = list(map(int, f.readlines()))
def p1(lines):
a, b = filter(lambda t: t[0] + t[1] == 2020, combinations(lines, 2)).__next__()
return a * b
def p2(lines):
a, b, c = filter(lambda t: t[0] + t[1] + t[2] == 2020, combinations(lines, 3)).__next__()
return a * b * c
print(p1(lines))
print(p2(lines))

View File

@ -1,11 +0,0 @@
[package]
name = "day02"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"
text_io = "0.1.8"

View File

@ -1,10 +0,0 @@
[package]
name = "day03"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"

View File

@ -1,10 +0,0 @@
[package]
name = "day05"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"

View File

@ -1,11 +0,0 @@
[package]
name = "day06"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"
reduce = "0.1.3"

View File

@ -1,9 +0,0 @@
[package]
name = "day07"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,9 +0,0 @@
[package]
name = "day08"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,5 +1,5 @@
[package]
name = "day04"
name = "aoc2020"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
@ -7,7 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.9.0"
lazy_static = "1.4.0"
regex = "1.4.2"
text_io = "0.1.8"
# for the day 4 meme solution
serde = { version = "1.0.117", features = ["derive"] }
serde_yaml = "0.8.14"

View File

@ -1,16 +1,10 @@
#!/bin/sh
today=$(date +%d)
mkdir "$today"
cd "$today"
cargo init --name "day$today"
echo 'Initialized cargo project'
cargo add itertools
# this assumes that your puzzle input is already in your clipboard
xsel -b > input
xsel -b > inputs/$today
# add trailing newline if necessary
sed -i -e '$a\' input
sed -i -e '$a\' inputs/$today
echo '
#![feature(test)]
@ -18,7 +12,7 @@ extern crate test;
use std::env;
fn read_input() -> String {
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("input"))).unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day'$today'"))).unwrap()
}
fn parse_input(raw: &str) -> Vec<!> {
@ -38,4 +32,4 @@ mod tests {
const TEST_INPUT: &str = "";
}' > src/main.rs
}' > src/day$today.rs

View File

@ -1,9 +1,10 @@
#![feature(test,bool_to_option)]
#![feature(test, bool_to_option)]
extern crate test;
use itertools::Itertools;
use std::env;
fn read_input() -> Vec<usize> {
std::fs::read_to_string("input")
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day01")))
.unwrap()
.lines()
.filter_map(|l| l.parse().ok())

View File

@ -1,7 +1,7 @@
#![feature(test)]
extern crate test;
use itertools::Itertools;
use std::ops::RangeInclusive;
use std::{env, ops::RangeInclusive};
use text_io::scan;
#[derive(Debug)]
@ -37,12 +37,16 @@ impl PasswordVerification {
let pw = self.password.as_bytes();
let (first, second) = (*self.required_quantity.start(), *self.required_quantity.end());
// 1-based indexing :reeeeeee:
(pw[first-1] == self.required_char as u8) ^ (pw[second-1] == self.required_char as u8)
(pw[first - 1] == self.required_char as u8) ^ (pw[second - 1] == self.required_char as u8)
}
}
fn read_input() -> Vec<PasswordVerification> {
std::fs::read_to_string("input").unwrap().lines().map_into().collect()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day02")))
.unwrap()
.lines()
.map_into()
.collect()
}
fn main() {
@ -71,11 +75,7 @@ mod tests {
#[test]
fn test_part2() {
let pws: Vec<PasswordVerification> = TEST_INPUT.lines().map_into().collect_vec();
let pws = pws
.into_iter()
.filter(|pw| pw.is_valid_part2())
.map(|pw| pw.password)
.collect_vec();
let pws = pws.into_iter().filter(|pw| pw.is_valid_part2()).map(|pw| pw.password).collect_vec();
assert_eq!(pws, vec![String::from("abcde")]);
}
}

View File

@ -2,7 +2,7 @@
#![feature(test)]
extern crate test;
use itertools::Itertools;
use std::iter;
use std::{env, iter};
#[derive(Debug, PartialEq, Copy, Clone)]
enum Tile {
@ -29,7 +29,7 @@ impl From<u8> for Tile {
}
fn read_input() -> String {
std::fs::read_to_string("input").unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day03"))).unwrap()
}
fn parse_input(raw: &str) -> Forest {
@ -50,7 +50,7 @@ fn count_trees(forest: &Forest, step_right: usize, step_down: usize) -> usize {
y + step_down,
Some(x + step_right)
.filter(|&it| it < forest[0].len())
.unwrap_or_else(||(x + step_right) - forest[0].len()),
.unwrap_or_else(|| (x + step_right) - forest[0].len()),
))
})
.map_while(|(y, x)| forest.get(y).map(|r| r[x]))

View File

@ -3,6 +3,7 @@ extern crate test;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::env;
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Passport {
@ -17,7 +18,7 @@ struct Passport {
}
fn read_input() -> String {
std::fs::read_to_string("input").unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day04"))).unwrap()
}
/// When I first saw the input and puzzle, I thought

View File

@ -1,7 +1,7 @@
#![feature(test, map_first_last)]
extern crate test;
use itertools::Itertools;
use std::collections::BTreeSet;
use std::{collections::BTreeSet, env};
const NUM_ROWS: usize = 128;
const NUM_COLS: usize = 8;
@ -50,7 +50,7 @@ fn main() {
}
fn read_input() -> String {
std::fs::read_to_string("input").unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day05"))).unwrap()
}
#[cfg(test)]

View File

@ -1,7 +1,7 @@
#![feature(test)]
extern crate test;
use std::{
collections::HashSet, ops::{BitAnd, BitOr}
collections::HashSet, env, ops::{BitAnd, BitOr}
};
fn main() {
@ -11,7 +11,7 @@ fn main() {
}
fn read_input() -> String {
std::fs::read_to_string("input").unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day06"))).unwrap()
}
fn parse_input(raw: &str) -> Vec<Vec<HashSet<char>>> {

View File

@ -1,7 +1,6 @@
#![feature(test)]
use std::collections::HashSet;
use std::{collections::HashSet, env};
extern crate test;
use std::env;
const COLOR: &str = "shiny gold";
@ -47,7 +46,7 @@ impl From<&str> for Bag {
}
fn read_input() -> String {
std::fs::read_to_string(env::args().nth(1).unwrap_or(String::from("input"))).unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day07"))).unwrap()
}
fn part1<'a, 'b>(bags: &'b [Bag], color: &str, seen: &'a mut HashSet<&'b str>) -> &'a mut HashSet<&'b str> {

View File

@ -10,7 +10,7 @@ enum Command {
}
fn read_input() -> String {
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("input"))).unwrap()
std::fs::read_to_string(env::args().nth(1).filter(|n| n != "--bench").unwrap_or(String::from("inputs/day08"))).unwrap()
}
fn parse_input(raw: &str) -> Vec<Command> {

3
2020/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Please use cargo run dayXX to run any of the days.");
}