day3 day4

This commit is contained in:
Frederik Palmø 2023-05-24 22:05:37 +02:00
parent 4f7afcf17d
commit 1a0d386f14
4 changed files with 1391 additions and 0 deletions

300
input/day3.txt Normal file
View file

@ -0,0 +1,300 @@
vvMQnwwvrwWNfrtZJfppmSfJSmSg
BzGqjlBqBBmztHNFzDHg
llRCPlTPPqBjPhqhlBRBClhqWcTWrWNcMbQbdrdLccccrnvM
wMhwbTWpQjbpWHMQppzTHhjtlCjPSSJCCtlqRlJVFJFt
ggdvvnvDgdDmNcBrrcDntFRFqHJJtSJqvlVSRlJq
fggNNffGmcBrmBfcDzzzpHbsGTpszwwTbp
BPdPPBggrPtrpbtvPBBdgrFmhhQThGGlbbTZnzZQzZfn
ccjWRJVNcTGmnWWFmh
DMNmsMHwRNBrggdPDPdt
TfsfHLQbBtBFQbQsBmPwwlnPGZFwwdwWFZZw
MRpcvJMJVSMrVMpVSvhhnclwgWwDZgWgWgWglwcG
GCzjRJjVjSSrvfNQtLmQNsQbjB
FrSPFjtVvwsqSwcG
hDHdWDngpgZTDgHzzHwNNqlwNvZJlGqcQGsl
wDzLTDHgFffLtRft
CnCJNVqvCBJBNZmfPcPMcFLVcwmd
HgzjHFghSFtrLfwPchPM
QDpjgDSQlHHlDQQRzRzsBRRvWnWvJvZnqWBJNF
mGHcFPFqzPtcfPwDGVVpgLgSlgBl
rCvddTrnsbDLVSDwjSjd
QWhWQThswssMQMMMvhTzPqJzmzftHccJfHFhFm
cPbNpLVFTPbbFrpTLQBzqqmgnnBhgLMM
vvSwWCZCRZCDZtGwzdgWdQmzqgnQddJn
vCltGltCGmRRmCvDjjtHFpbcFfbbfssbpNPpHFpH
WLLQMWZLSPMPWmrwhnjhZZhpHJHljBDB
csbtCfFgCftGljHwHcBnpnJR
tsvgszNtfMwPzWqPrS
NbDZrbrFQQqqQtQqQDtTcBvCLBLswsZhscCGBZ
ljmWRzVRpbndMWmmfdsTsCBsGwTVVVCGCGws
ffRpnllHRMfdWzdnmRNQNNSFQQFN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rvBHgqRRgqHGgfZGfHRS
ggTQgsgwFrTrggbMTvSdmjfCmmQDcmqjDjmc
nLZnRhNZnnNHZhZVStCcDqjcqmjSjH
RWGNnhzBnJJRRWNRBNZNLZhFMTFPvrTrTlsggPwSlFMWTw
RNmnPRnLGcQmzBQpHHjTltjtlfgspbsq
CZvCJwZMMCCMdFVcwJJsgTTHfsTlbfbgbT
SSVFhWCZdSCcWCcWdrvhzmnnnLNGDRDNzzLNGz
jPwfPwNfFpFNQpDjdMcjcrdddDHD
tzsRsGRLzhLhvqvhHMlqqV
LRBnRBGSnBSGsGSGmGtBJCmnNWZpPpTNPMwQMPNJFZTTNwWT
PCrStRPSPvZQcZPvqvfjSRWFFNFJFLZTTJTTVZFFGLFF
DlpBzBntHDzhlpGJVHLwTMFLVLTL
gptBBdgzpsBbpQvvPQPRqrdcCC

1000
input/day4.txt Normal file

File diff suppressed because it is too large Load diff

56
src/bin/day3.rs Normal file
View file

@ -0,0 +1,56 @@
use itertools::Itertools;
use std::collections::HashSet;
fn main() {
let input = std::fs::read_to_string("input/day3.txt").unwrap();
println!("part 1: {}", part1(&input));
println!("part 1: {}", part2(&input));
}
fn part1(input: &str) -> i64 {
input
.lines()
.map(|line| line.split_at(line.len() / 2))
.flat_map(|(first, second)| {
HashSet::<_>::intersection(&first.chars().collect(), &second.chars().collect())
.map(|c| *c as u8)
.collect::<Vec<_>>()
})
.map(|item| match item {
b'a'..=b'z' => item - b'a' + 1,
b'A'..=b'Z' => item - b'A' + 27,
_ => panic!("input was wrong"),
} as i64)
.sum()
}
fn part2(input: &str) -> i64 {
input
.lines()
.chunks(3)
.into_iter()
.filter_map(|group| {
group
.map(|line| line.chars().collect::<HashSet<_>>())
.reduce(|previous, badges| previous.intersection(&badges).copied().collect())
})
.flatten()
.map(|item| match item {
'a'..='z' => item as u8 - b'a' + 1,
'A'..='Z' => item as u8 - b'A' + 27,
_ => panic!("input was wrong"),
} as i64)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn examples() {
let example = "vJrwpWtwJgWrhcsFMMfFFhFp\njqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\nPmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\nttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw";
assert_eq!(part1(example), 157);
assert_eq!(part2(example), 70);
}
}

35
src/bin/day4.rs Normal file
View file

@ -0,0 +1,35 @@
use eyes::{parse, try_parse};
fn main() {
let input = std::fs::read_to_string("input/day4.txt").unwrap();
println!("part 1: {}", part1(&input));
println!("part 1: {}", part2(&input));
}
fn part1(input: &str) -> i64 {
input
.lines()
.map(|line| parse!(line, "{}-{},{}-{}", i64, i64, i64, i64))
.filter(|(f1, f2, s1, s2)| (f1 <= s1 && f2 >= s2) || (s1 <= f1 && s2 >= f2))
.count() as i64
}
fn part2(input: &str) -> i64 {
input
.lines()
.map(|line| parse!(line, "{}-{},{}-{}", i64, i64, i64, i64))
.filter(|(f1, f2, s1, s2)| f2 >= s1 && s2 >= f1)
.count() as i64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn examples() {
let example = "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8";
assert_eq!(part1(example), 2);
assert_eq!(part2(example), 4);
}
}