diff options
-rwxr-xr-x | day_18.py | 58 | ||||
-rw-r--r-- | src/day_18.rs | 45 |
2 files changed, 86 insertions, 17 deletions
diff --git a/day_18.py b/day_18.py new file mode 100755 index 0000000..7874652 --- /dev/null +++ b/day_18.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +inp = open("input/day_18.txt") +dat = inp.read().strip() + +pos = (0, 0) +sum1 = 0 +sum2 = 0 +sum_dir = 0 + +pos_corr = (0, 0) +sum1_corr = 0 +sum2_corr = 0 +sum_dir_corr = 0 + +for line in dat.split("\n"): + d, n, col = line.split() + match d: + case "L": + direction = (-1, 0) + case "R": + direction = (1, 0) + case "U": + direction = (0, -1) + case "D": + direction = (0, 1) + length = int(n) + next_pos = (pos[0] + direction[0] * length, pos[1] + direction[1] * length) + sum1 += (pos[0]) * (next_pos[1]) + sum2 += (pos[1]) * (next_pos[0]) + sum_dir += length + pos = next_pos + cleaned = col.removeprefix("(#").removesuffix(")") + corr_len = int(cleaned[:5], base=16) + match cleaned[5]: + case "0": + direction_corr = (1, 0) + case "1": + direction_corr = (0, 1) + case "2": + direction_corr = (-1, 0) + case "3": + direction_corr = (0, -1) + next_pos_corr = ( + pos_corr[0] + direction_corr[0] * corr_len, + pos_corr[1] + direction_corr[1] * corr_len, + ) + sum1_corr = sum1_corr + (pos_corr[0]) * (next_pos_corr[1]) + sum2_corr = sum2_corr + (pos_corr[1]) * (next_pos_corr[0]) + sum_dir_corr += corr_len + pos_corr = next_pos_corr + +area = abs(sum1 - sum2) / 2 +print(area + sum_dir / 2 + 1) + + +area_corr = abs(sum1_corr - sum2_corr) / 2 +print(area_corr + sum_dir_corr / 2 + 1) diff --git a/src/day_18.rs b/src/day_18.rs index 1ed5100..aa2bdf1 100644 --- a/src/day_18.rs +++ b/src/day_18.rs @@ -71,15 +71,15 @@ impl Solution for Day18 { } fn part_2(input: &str) -> anyhow::Result<Self::Answer2> { - let mut pos = (0_isize, 0_isize); - let mut trenches = vec![(pos, 'S')]; + let mut trenches = vec![]; + let mut pos = (0_isize, 0_isize); + let mut dir = 'D'; let mut b = 0; - let mut dir = 'S'; for line in input.lines() { let (c, len) = parse_code(line); - b += len; + b += len as usize; if "DU".contains(c) && dir != c { dir = c; @@ -88,17 +88,28 @@ impl Solution for Day18 { } } - for _ in 0..len { - match c { - 'U' => pos.0 -= 1, - 'D' => pos.0 += 1, - 'L' => pos.1 -= 1, - 'R' => pos.1 += 1, - c => return Err(anyhow::format_err!("Invalid character: {c}")), - }; - - trenches.push((pos, dir)); - } + // for _ in 0..len { + // match c { + // 'U' => pos.0 -= 1, + // 'D' => pos.0 += 1, + // 'L' => pos.1 -= 1, + // 'R' => pos.1 += 1, + // c => return Err(anyhow::format_err!("Invalid character: {c}")), + // }; + // + // trenches.push((pos, dir)); + // } + trenches.push(((pos.0, pos.1), dir)); + + match c { + 'U' => (1..len).for_each(|n| trenches.push(((pos.0 - n, pos.1), dir))), + 'D' => (1..len).for_each(|n| trenches.push(((pos.0 + n, pos.1), dir))), + 'L' => trenches.push(((pos.0, pos.1 - len), dir)), + 'R' => trenches.push(((pos.0, pos.1 + len), dir)), + c => return Err(anyhow::format_err!("Invalid character: {c}")), + }; + + pos = *trenches.last().map(|(p, _)| p).unwrap(); } trenches.sort(); @@ -122,7 +133,7 @@ impl Solution for Day18 { } } -fn parse_code(s: &str) -> (char, usize) { +fn parse_code(s: &str) -> (char, isize) { let iter = &mut s .split_whitespace() .nth(2) @@ -132,7 +143,7 @@ fn parse_code(s: &str) -> (char, usize) { .strip_suffix(')') .unwrap() .chars(); - let dist = usize::from_str_radix(&iter.take(5).collect::<String>(), 16).unwrap(); + let dist = isize::from_str_radix(&iter.take(5).collect::<String>(), 16).unwrap(); let code = iter.next().unwrap().to_digit(10).unwrap(); let c = match code { 0 => 'R', |