summaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-12-11 22:09:16 -0500
committerJesse Luehrs <doy@tozt.net>2022-12-11 22:19:17 -0500
commit70f6149889511f61f91f525eba5a82e938cb41ec (patch)
tree20ed08e39ee1d0c7ec561721231f36e98a89c1b9 /src/bin
parente2d219b331a878bbb3c9dcef9ea4e218b2e3ee06 (diff)
downloadadvent-of-code-70f6149889511f61f91f525eba5a82e938cb41ec.tar.gz
advent-of-code-70f6149889511f61f91f525eba5a82e938cb41ec.zip
support string solutions
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/2021/day13.rs7
-rw-r--r--src/bin/2022/day10.rs16
-rw-r--r--src/bin/2022/day5.rs18
3 files changed, 17 insertions, 24 deletions
diff --git a/src/bin/2021/day13.rs b/src/bin/2021/day13.rs
index 75e547b..b21071f 100644
--- a/src/bin/2021/day13.rs
+++ b/src/bin/2021/day13.rs
@@ -122,13 +122,12 @@ pub fn part1(
pub fn part2(
(mut paper, folds): (Paper, Vec<(bool, usize)>),
-) -> Result<usize> {
+) -> Result<String> {
for fold in folds {
paper.fold(fold.0, fold.1);
}
- println!("{}", paper);
- Ok(paper.total())
+ Ok(ocr(&paper.to_string()))
}
#[test]
@@ -139,6 +138,6 @@ fn test() {
);
assert_eq!(
part2(parse(parse::data(2021, 13).unwrap()).unwrap()).unwrap(),
- 95
+ "ECFHLHZF"
);
}
diff --git a/src/bin/2022/day10.rs b/src/bin/2022/day10.rs
index 8f7b617..ff9ab11 100644
--- a/src/bin/2022/day10.rs
+++ b/src/bin/2022/day10.rs
@@ -62,22 +62,26 @@ pub fn part1(ops: impl Iterator<Item = Op>) -> Result<i64> {
Ok(total)
}
-pub fn part2(ops: impl Iterator<Item = Op>) -> Result<i64> {
+pub fn part2(ops: impl Iterator<Item = Op>) -> Result<String> {
let mut cpu = Cpu::new();
for op in ops {
cpu.step(op);
}
+ let mut s = String::new();
for (y, row) in cpu.history.chunks(40).enumerate() {
+ if row.len() < 40 {
+ break;
+ }
for (x, pos) in row.iter().enumerate() {
if i64::try_from(x).unwrap().abs_diff(*pos) <= 1 {
- print!("#");
+ s.push('#');
} else {
- print!(".");
+ s.push('.');
}
}
- println!();
+ s.push('\n');
}
- Ok(0)
+ Ok(ocr(&s))
}
#[test]
@@ -88,6 +92,6 @@ fn test() {
);
assert_eq!(
part2(parse(parse::data(2022, 10).unwrap()).unwrap()).unwrap(),
- 0
+ "ZFBFHGUP"
);
}
diff --git a/src/bin/2022/day5.rs b/src/bin/2022/day5.rs
index da1e5a2..b09e2e0 100644
--- a/src/bin/2022/day5.rs
+++ b/src/bin/2022/day5.rs
@@ -86,7 +86,7 @@ pub fn parse(fh: File) -> Result<Crates> {
})
}
-fn part1_str(mut crates: Crates) -> Result<String> {
+pub fn part1(mut crates: Crates) -> Result<String> {
for Instruction { count, from, to } in crates.instructions {
for _ in 0..count {
let c = crates.piles[from - 1].pop().unwrap();
@@ -100,12 +100,7 @@ fn part1_str(mut crates: Crates) -> Result<String> {
.collect())
}
-pub fn part1(crates: Crates) -> Result<i64> {
- println!("{}", part1_str(crates)?);
- Ok(0)
-}
-
-fn part2_str(mut crates: Crates) -> Result<String> {
+pub fn part2(mut crates: Crates) -> Result<String> {
for Instruction { count, from, to } in crates.instructions {
let mut tmp = vec![];
for _ in 0..count {
@@ -123,19 +118,14 @@ fn part2_str(mut crates: Crates) -> Result<String> {
.collect())
}
-pub fn part2(crates: Crates) -> Result<i64> {
- println!("{}", part2_str(crates)?);
- Ok(0)
-}
-
#[test]
fn test() {
assert_eq!(
- part1_str(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(),
+ part1(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(),
"PSNRGBTFT"
);
assert_eq!(
- part2_str(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(),
+ part2(parse(parse::data(2022, 5).unwrap()).unwrap()).unwrap(),
"BNTZFPMMW"
);
}