From b5aca56ebb2bf7d5ee879878b447eaf7cf9ab0d6 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Fri, 15 Nov 2019 16:31:45 +0100 Subject: [PATCH] 2018: day03: use struct instead of tuple --- aoc2018/src/day03.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/aoc2018/src/day03.rs b/aoc2018/src/day03.rs index 3ed3bd2..1ad17de 100644 --- a/aoc2018/src/day03.rs +++ b/aoc2018/src/day03.rs @@ -36,7 +36,15 @@ impl std::error::Error for ParseError { } } -fn parse(line: &str) -> Option<(usize, usize, usize, usize)> { +struct Claim { + x: usize, + y: usize, + width: usize, + height: usize, + id: usize, +} + +fn parse(line: &str) -> Option { // remove '#XXX @ ' start of line let line = &line[(line.find(" @ ")? + 3)..]; @@ -53,7 +61,13 @@ fn parse(line: &str) -> Option<(usize, usize, usize, usize)> { let width = line[..sep].parse().ok()?; let height = line[(sep + 1)..].parse().ok()?; - Some((x, y, width, height)) + Some(Claim { + x, + y, + width, + height, + id: 0, + }) } fn part1(input: &str) -> Result { @@ -61,12 +75,12 @@ fn part1(input: &str) -> Result { let mut map: HashMap<(usize, usize), u64> = HashMap::default(); for line in input.lines() { - let (x, y, width, height) = parse(line).ok_or(ParseError::new(line))?; + let claim = parse(line).ok_or(ParseError::new(line))?; - for i in 0..width { - for j in 0..height { - let x = x + i; - let y = y + j; + for i in 0..claim.width { + for j in 0..claim.height { + let x = claim.x + i; + let y = claim.y + j; // add tissue patch at coordinates (x, y) let entry = map.entry((x, y)).or_default();