Skip to content

Commit 77bf29e

Browse files
committed
Change AoC result to allow a vec of results
- also formatted day 14
1 parent 888e4fa commit 77bf29e

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

libaoc/src/lib.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![feature(str_split_once)]
22

33
use anyhow::{Error, Result};
4-
use std::fmt;
5-
use std::string::ToString;
4+
use std::{cmp::max, fmt, string::ToString};
65

76
mod vm;
87
pub use vm::*;
@@ -40,16 +39,20 @@ pub fn get_solution(solutions: &'static [Solution], day: usize) -> Result<&Solut
4039

4140
/// generic result container for each day
4241
pub struct AocResult {
43-
pub part1: String,
44-
pub part2: String,
42+
results: Vec<(&'static str, String)>,
4543
}
4644

4745
impl AocResult {
4846
/// construct result from any valid types
4947
pub fn new<T: ToString, R: ToString>(part1: T, part2: R) -> Self {
5048
AocResult {
51-
part1: part1.to_string(),
52-
part2: part2.to_string(),
49+
results: vec![("Part 1", part1.to_string()), ("Part 2", part2.to_string())],
50+
}
51+
}
52+
53+
pub fn from(results: &[(&'static str, String)]) -> Self {
54+
AocResult {
55+
results: results.to_vec()
5356
}
5457
}
5558
}
@@ -95,14 +98,35 @@ impl fmt::Display for FloatTime {
9598

9699
impl fmt::Display for AocResult {
97100
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
98-
write!(f, "{}, {}", self.part1, self.part2)
101+
for (i, result) in self.results.iter().enumerate() {
102+
if i > 0 {
103+
write!(f, ", ")?;
104+
}
105+
write!(f, "{}", result.1)?;
106+
}
107+
Ok(())
99108
}
100109
}
101110

102111
impl fmt::Debug for AocResult {
103112
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
104113
writeln!(f, "Results:")?;
105-
writeln!(f, " Part 1: {}", self.part1)?;
106-
writeln!(f, " Part 2: {}", self.part2)
114+
115+
let width = self
116+
.results
117+
.iter()
118+
.fold(0, |acc, (name, _)| max(acc, name.len()))
119+
+ 1;
120+
121+
for (name, result) in &self.results {
122+
writeln!(
123+
f,
124+
" {:<width$} {}",
125+
format!("{}:", name),
126+
result,
127+
width = width
128+
)?;
129+
}
130+
Ok(())
107131
}
108132
}

src/days/day14.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
2+
use hashbrown::HashMap;
23
use libaoc::{aoc, AocResult, Timer};
34
use regex::Regex;
4-
use hashbrown::HashMap;
55

66
#[derive(Debug)]
77
enum Command {
@@ -15,25 +15,28 @@ pub fn solve(timer: &mut Timer, input: String) -> Result<AocResult> {
1515
let mem_reg = Regex::new(r"mem\[(\d+)\] = (\d+)")?;
1616
let mask_reg = Regex::new(r"mask = ([X01]+)")?;
1717

18-
let lines: Vec<_> = input.lines().map(|x| {
19-
if let Some(cap) = mem_reg.captures(x) {
20-
Mem(cap[1].parse().unwrap(), cap[2].parse().unwrap())
21-
} else {
22-
let cap = mask_reg.captures(x).unwrap();
23-
let mut zero_mask = 0;
24-
let mut one_mask = 0;
25-
let mut x_mask = vec![];
26-
for (i,c) in cap[1].chars().rev().enumerate() {
27-
match c {
28-
'0' => zero_mask |= 1 << i,
29-
'1' => one_mask |= 1 << i,
30-
'X' => x_mask.push(i),
31-
_ => ()
18+
let lines: Vec<_> = input
19+
.lines()
20+
.map(|x| {
21+
if let Some(cap) = mem_reg.captures(x) {
22+
Mem(cap[1].parse().unwrap(), cap[2].parse().unwrap())
23+
} else {
24+
let cap = mask_reg.captures(x).unwrap();
25+
let mut zero_mask = 0;
26+
let mut one_mask = 0;
27+
let mut x_mask = vec![];
28+
for (i, c) in cap[1].chars().rev().enumerate() {
29+
match c {
30+
'0' => zero_mask |= 1 << i,
31+
'1' => one_mask |= 1 << i,
32+
'X' => x_mask.push(i),
33+
_ => (),
34+
}
3235
}
36+
Mask(!zero_mask, one_mask, x_mask)
3337
}
34-
Mask(!zero_mask,one_mask,x_mask)
35-
}
36-
}).collect();
38+
})
39+
.collect();
3740

3841
timer.lap("Parse");
3942

@@ -42,12 +45,12 @@ pub fn solve(timer: &mut Timer, input: String) -> Result<AocResult> {
4245
let mut mem = HashMap::new();
4346
for line in &lines {
4447
match line {
45-
Mask(a,b,_) => {
48+
Mask(a, b, _) => {
4649
zero_mask = *a;
4750
one_mask = *b;
4851
}
49-
Mem(a,b) => {
50-
mem.insert(a, (b|one_mask)&zero_mask);
52+
Mem(a, b) => {
53+
mem.insert(a, (b | one_mask) & zero_mask);
5154
}
5255
}
5356
}
@@ -60,7 +63,7 @@ pub fn solve(timer: &mut Timer, input: String) -> Result<AocResult> {
6063
let mut mem = HashMap::new();
6164
for line in &lines {
6265
match line {
63-
Mask(_,b,c) => {
66+
Mask(_, b, c) => {
6467
one_mask = *b;
6568
x_mask = c;
6669
}
@@ -70,7 +73,7 @@ pub fn solve(timer: &mut Timer, input: String) -> Result<AocResult> {
7073
let mut addr = addr;
7174
for (i, addr_index) in x_mask.iter().enumerate() {
7275
if (curr_change & (1 << i)) != 0 {
73-
addr &= !(1<<addr_index);
76+
addr &= !(1 << addr_index);
7477
} else {
7578
addr |= 1 << addr_index;
7679
}

0 commit comments

Comments
 (0)