improve performance of 2016 day 1 part 2 and format fixes
This commit is contained in:
parent
ef042f87db
commit
663375d8f8
@ -1,4 +1,4 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
use crate::util::{index::*, parse::ParseExt};
|
use crate::util::{index::*, parse::ParseExt};
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ pub fn part1(input: &str) -> impl std::fmt::Display {
|
|||||||
pub fn part2(input: &str) -> impl std::fmt::Display {
|
pub fn part2(input: &str) -> impl std::fmt::Display {
|
||||||
let mut pos = Ix2::ORIGIN;
|
let mut pos = Ix2::ORIGIN;
|
||||||
let mut dir = Ix2::UP;
|
let mut dir = Ix2::UP;
|
||||||
let mut set = HashSet::new();
|
let mut set = BTreeSet::new();
|
||||||
|
|
||||||
for (d, s) in parse(input) {
|
for (d, s) in parse(input) {
|
||||||
dir = if d == b'R' {
|
dir = if d == b'R' {
|
||||||
|
@ -52,5 +52,8 @@ fn test_part1() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2() {
|
||||||
assert_eq!("2", part2("100 200 300\n101 201 301\n102 202 302").to_string());
|
assert_eq!(
|
||||||
|
"2",
|
||||||
|
part2("100 200 300\n101 201 301\n102 202 302").to_string()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,16 @@ pub fn part2(input: &str) -> impl std::fmt::Display {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() {
|
||||||
assert_eq!("24000", part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string())
|
assert_eq!(
|
||||||
|
"24000",
|
||||||
|
part1("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2() {
|
||||||
assert_eq!("45000", part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string())
|
assert_eq!(
|
||||||
|
"45000",
|
||||||
|
part2("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000").to_string()
|
||||||
|
)
|
||||||
}
|
}
|
@ -1,13 +1,13 @@
|
|||||||
enum Move {
|
enum Move {
|
||||||
Rock = 1,
|
Rock = 1,
|
||||||
Paper = 2,
|
Paper = 2,
|
||||||
Scissors = 3
|
Scissors = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum GameResult {
|
enum GameResult {
|
||||||
Loss = 0,
|
Loss = 0,
|
||||||
Draw = 3,
|
Draw = 3,
|
||||||
Win = 6
|
Win = 6,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Move {
|
impl From<&str> for Move {
|
||||||
@ -16,7 +16,7 @@ impl From<&str> for Move {
|
|||||||
"A" | "X" => Self::Rock,
|
"A" | "X" => Self::Rock,
|
||||||
"B" | "Y" => Self::Paper,
|
"B" | "Y" => Self::Paper,
|
||||||
"C" | "Z" => Self::Scissors,
|
"C" | "Z" => Self::Scissors,
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -27,18 +27,21 @@ impl From<&str> for GameResult {
|
|||||||
"X" => Self::Loss,
|
"X" => Self::Loss,
|
||||||
"Y" => Self::Draw,
|
"Y" => Self::Draw,
|
||||||
"Z" => Self::Win,
|
"Z" => Self::Win,
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calc(input: &str, calc: fn(&str, &str) -> u32) -> u32 {
|
fn calc(input: &str, calc: fn(&str, &str) -> u32) -> u32 {
|
||||||
input.lines().map(|l| {
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
let Some((l, r)) = l.split_once(' ') else {
|
let Some((l, r)) = l.split_once(' ') else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
(calc)(l, r)
|
(calc)(l, r)
|
||||||
}).sum()
|
})
|
||||||
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &str) -> impl std::fmt::Display {
|
pub fn part1(input: &str) -> impl std::fmt::Display {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user