commit e7b86edb90af39628d7e2ffab632cd8f5919adac
parent 76506c1d5fcdaf4de152016aa0fdf23b7ec81526
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Wed, 4 Dec 2019 08:46:09 -0500
Add Rust solution for day 04
Diffstat:
3 files changed, 102 insertions(+), 0 deletions(-)
diff --git a/day-04/Makefile b/day-04/Makefile
@@ -0,0 +1,9 @@
+CC := g++
+CCFLAGS := --std=c++17 -Wall
+
+all: day-04-rust day-04.jl
+ julia day-04.jl
+ ./day-04-rust
+
+day-04-rust: day-04.rs
+ rustc $^ -o $@
diff --git a/day-04/day-04.rs b/day-04/day-04.rs
@@ -0,0 +1,92 @@
+fn main() {
+ let input = std::fs::read_to_string("input.txt")
+ .unwrap()
+ .trim()
+ .split("-")
+ .map(|op| op.parse::<usize>().unwrap())
+ .collect::<Vec<usize>>();
+
+ println!("Rust:");
+ println!("Part 1: {}", part_1(&input));
+ println!("Part 2: {}", part_2(&input));
+}
+
+fn part_1(input: &Vec<usize>) -> usize {
+ let start = input[0];
+ let end = input[1];
+ let mut pw_count = 0;
+ for first in (start / 100000)..=(end / 100000) {
+ for second in first..=9 {
+ if first * 100000 + second * 10000 > end {
+ break;
+ }
+ for third in second..=9 {
+ for forth in third..=9 {
+ for fifth in forth..=9 {
+ for sixth in fifth..=9 {
+ let pw = first * 100000
+ + second * 10000
+ + third * 1000
+ + forth * 100
+ + fifth * 10
+ + sixth;
+ // Ensure in range.
+ if pw >= start && pw <= end {
+ if first == second
+ || second == third
+ || third == forth
+ || forth == fifth
+ || fifth == sixth
+ {
+ pw_count += 1;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ pw_count
+}
+
+fn part_2(input: &Vec<usize>) -> usize {
+ let start = input[0];
+ let end = input[1];
+ let mut pw_count = 0;
+ for first in (start / 100000)..=(end / 100000) {
+ for second in first..=9 {
+ if first * 100000 + second * 10000 > end {
+ break;
+ }
+ for third in second..=9 {
+ for forth in third..=9 {
+ for fifth in forth..=9 {
+ for sixth in fifth..=9 {
+ let pw = first * 100000
+ + second * 10000
+ + third * 1000
+ + forth * 100
+ + fifth * 10
+ + sixth;
+ // Ensure in range.
+ if pw >= start && pw <= end {
+ if (first == second && second != third)
+ || (first != second && second == third && third != forth)
+ || (second != third && third == forth && forth != fifth)
+ || (third != forth && forth == fifth && fifth != sixth)
+ || (forth != fifth && fifth == sixth)
+ {
+ pw_count += 1;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ pw_count
+}
diff --git a/day-04/input.txt b/day-04/input.txt
@@ -0,0 +1 @@
+272091-815432