Compare commits

...

2 Commits

Author SHA1 Message Date
Max
15a59bae60 Day 4 2022-12-04 15:01:31 +01:00
Max
6ba1e39d98 Day 2 and 3 improvements 2022-12-04 15:01:22 +01:00
6 changed files with 1305 additions and 14 deletions

View File

@ -10,12 +10,9 @@ enum Symbol {
impl From<char> for Symbol {
fn from(value: char) -> Self {
match value {
'A' => Self::Rock,
'B' => Self::Paper,
'C' => Self::Scissors,
'X' => Self::Rock,
'Y' => Self::Paper,
'Z' => Self::Scissors,
'A' | 'X' => Self::Rock,
'B' | 'Y' => Self::Paper,
'C' | 'Z' => Self::Scissors,
_ => panic!("invalid input"),
}
}

View File

@ -3,12 +3,12 @@ use std::{collections::HashSet, fs::read_to_string};
use color_eyre::{eyre::*, Result};
use itertools::Itertools;
fn priority(item: &char) -> usize {
fn priority(item: char) -> Result<usize> {
('a'..='z')
.position(|c| *item == c)
.position(|c| item == c)
.map(|i| i + 1)
.or_else(|| ('A'..='Z').position(|c| *item == c).map(|i| i + 27))
.unwrap_or(0)
.or_else(|| ('A'..='Z').position(|c| item == c).map(|i| i + 27))
.ok_or_else(|| eyre!("invalid character '{}'", item))
}
fn initial(input: &str) -> (usize, usize) {
@ -20,7 +20,7 @@ fn initial(input: &str) -> (usize, usize) {
let compartment2 = compartment2.chars().collect::<HashSet<_>>();
compartment1
.intersection(&compartment2)
.map(priority)
.filter_map(|v| priority(*v).ok())
.sum::<usize>()
})
.sum();
@ -32,7 +32,7 @@ fn initial(input: &str) -> (usize, usize) {
group
.map(|line| line.chars().collect::<HashSet<_>>())
.coalesce(|a, b| Ok(a.intersection(&b).copied().collect()))
.map(|set| set.iter().map(priority).sum::<usize>())
.map(|set| set.iter().filter_map(|v| priority(*v).ok()).sum::<usize>())
.sum::<usize>()
})
.sum();

243
day4/Cargo.lock generated Normal file
View File

@ -0,0 +1,243 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "backtrace"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "cc"
version = "1.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color-eyre"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "day4"
version = "0.1.0"
dependencies = [
"color-eyre",
"itertools",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "eyre"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "gimli"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d"
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "miniz_oxide"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]]
name = "thread_local"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
dependencies = [
"once_cell",
]
[[package]]
name = "tracing"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"

View File

@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
color-eyre = "0.6.2"
itertools = "0.10.5"

1000
day4/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,52 @@
fn main() {
println!("Hello, world!");
use std::fs::read_to_string;
use color_eyre::Result;
use itertools::Itertools;
fn initial(input: &str) -> (usize, usize) {
let ranges = input.lines().map(|line| {
line.split_terminator(',')
.map(|range| {
let (lower, upper): (usize, usize) = range
.split_terminator('-')
.map(|number| number.parse::<usize>().unwrap())
.take(2)
.collect_tuple()
.unwrap();
lower..=upper
})
.take(2)
.collect_tuple()
.unwrap()
});
let containing = *ranges
.clone()
.map(|(first, second)| {
first.contains(second.start()) && first.contains(second.end())
|| second.contains(first.start()) && second.contains(first.end())
})
.counts()
.get(&true)
.unwrap_or(&0usize);
let overlaping = *ranges
.map(|(first, second)| {
first.contains(second.start())
|| first.contains(second.end())
|| second.contains(first.start())
|| second.contains(first.end())
})
.counts()
.get(&true)
.unwrap_or(&0usize);
(containing, overlaping)
}
fn main() -> Result<()> {
let input = read_to_string("input.txt")?;
let (containing, overlaping) = initial(&input);
println!(
"{} pairs have containing ranges, {} ranges overlap",
containing, overlaping
);
Ok(())
}