summaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2023-12-02 00:30:11 -0500
committerJesse Luehrs <doy@tozt.net>2023-12-02 00:30:11 -0500
commitf6a8a5ef3ecb69962ca2dbd09327639bd959dc8f (patch)
tree5cf782e85dbcbb120a2bbc51881e5c4e61d1f0c8 /src/bin
parent5eba5d2b339075f51b7255de72d32c8a740614e3 (diff)
downloadadvent-of-code-f6a8a5ef3ecb69962ca2dbd09327639bd959dc8f.tar.gz
advent-of-code-f6a8a5ef3ecb69962ca2dbd09327639bd959dc8f.zip
day 2
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/2023/day2.rs115
-rw-r--r--src/bin/2023/main.rs2
2 files changed, 117 insertions, 0 deletions
diff --git a/src/bin/2023/day2.rs b/src/bin/2023/day2.rs
new file mode 100644
index 0000000..3a12adb
--- /dev/null
+++ b/src/bin/2023/day2.rs
@@ -0,0 +1,115 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+
+use std::str::FromStr;
+
+use advent_of_code::prelude::*;
+
+#[derive(Clone, Copy, Debug)]
+pub enum Color {
+ Red(i64),
+ Green(i64),
+ Blue(i64),
+}
+
+impl FromStr for Color {
+ type Err = anyhow::Error;
+
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ let parts: Vec<_> = s.split(' ').collect();
+ match parts[1] {
+ "red" => Ok(Self::Red(parts[0].parse()?)),
+ "green" => Ok(Self::Green(parts[0].parse()?)),
+ "blue" => Ok(Self::Blue(parts[0].parse()?)),
+ _ => Err(anyhow::anyhow!("failed to parse {}", parts[1])),
+ }
+ }
+}
+
+pub fn parse(fh: File) -> Result<Vec<Vec<Vec<Color>>>> {
+ parse::raw_lines(fh)
+ .map(|line| {
+ let line = line.split(": ").nth(1).unwrap();
+ line.split("; ")
+ .map(|draw| {
+ draw.split(", ")
+ .map(|color| color.parse::<Color>())
+ .collect::<Result<Vec<_>, _>>()
+ })
+ .collect::<Result<Vec<Vec<_>>, _>>()
+ })
+ .collect()
+}
+
+pub fn part1(games: Vec<Vec<Vec<Color>>>) -> Result<i64> {
+ let mut total = 0;
+ 'game: for (i, game) in games.into_iter().enumerate() {
+ for draw in game {
+ for color in draw {
+ match color {
+ Color::Red(n) => {
+ if n > 12 {
+ continue 'game;
+ }
+ }
+ Color::Green(n) => {
+ if n > 13 {
+ continue 'game;
+ }
+ }
+ Color::Blue(n) => {
+ if n > 14 {
+ continue 'game;
+ }
+ }
+ }
+ }
+ }
+ total += i64::try_from(i).unwrap() + 1;
+ }
+ Ok(total)
+}
+
+pub fn part2(games: Vec<Vec<Vec<Color>>>) -> Result<i64> {
+ let mut total = 0;
+ for game in games {
+ let mut min_red = 0;
+ let mut min_green = 0;
+ let mut min_blue = 0;
+ for draw in game {
+ for color in draw {
+ match color {
+ Color::Red(n) => {
+ if n > min_red {
+ min_red = n;
+ }
+ }
+ Color::Green(n) => {
+ if n > min_green {
+ min_green = n;
+ }
+ }
+ Color::Blue(n) => {
+ if n > min_blue {
+ min_blue = n;
+ }
+ }
+ }
+ }
+ }
+ total += min_red * min_green * min_blue;
+ }
+ Ok(total)
+}
+
+#[test]
+fn test() {
+ assert_eq!(
+ part1(parse(parse::data(2023, 2).unwrap()).unwrap()).unwrap(),
+ 2617
+ );
+ assert_eq!(
+ part2(parse(parse::data(2023, 2).unwrap()).unwrap()).unwrap(),
+ 59795
+ );
+}
diff --git a/src/bin/2023/main.rs b/src/bin/2023/main.rs
index 01e0de3..cb9aa54 100644
--- a/src/bin/2023/main.rs
+++ b/src/bin/2023/main.rs
@@ -12,6 +12,7 @@
use advent_of_code::prelude::*;
mod day1;
+mod day2;
// NEXT MOD
#[paw::main]
@@ -19,6 +20,7 @@ fn main(opt: Opt) -> Result<()> {
#[allow(clippy::match_single_binding)]
match opt.day {
1 => advent_of_code::day!(2023, opt.day, opt.puzzle, day1),
+ 2 => advent_of_code::day!(2023, opt.day, opt.puzzle, day2),
// NEXT PART
_ => panic!("unknown day {}", opt.day),
}