advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 1246bcf29d9f8af9415f7e8dc8ed620714adc9b2
parent e7b86edb90af39628d7e2ffab632cd8f5919adac
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Wed,  4 Dec 2019 13:02:09 -0500

Add Julia solution for day 04

Diffstat:
Aday-04/day-04.jl | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+), 0 deletions(-)
diff --git a/day-04/day-04.jl b/day-04/day-04.jl
@@ -0,0 +1,91 @@
+function part_1(input)
+    start = input[1]
+    finish = input[2]
+    pw_count = 0
+    for first in (start ÷ 100000):(finish ÷ 100000)
+        for second in first:9
+            if first * 100000 + second * 10000 > finish
+                break
+            end
+            for third in second:9
+                for forth in third:9
+                    for fifth in forth:9
+                        for sixth in fifth:9
+                            pw = (
+                                first * 100000
+                                + second * 10000
+                                + third * 1000
+                                + forth * 100
+                                + fifth * 10
+                                + sixth
+                            )
+                            # Ensure in range.
+                            if pw >= start && pw <= finish
+                                if (
+                                    first == second
+                                    || second == third
+                                    || third == forth
+                                    || forth == fifth
+                                    || fifth == sixth
+                                )
+                                    pw_count += 1
+                                end
+                            end
+                        end
+                    end
+                end
+            end
+        end
+    end
+    pw_count
+end
+
+function part_2(input)
+    start = input[1]
+    finish = input[2]
+    pw_count = 0
+    for first in (start ÷ 100000):(finish ÷ 100000)
+        for second in first:9
+            if first * 100000 + second * 10000 > finish
+                break
+            end
+            for third in second:9
+                for forth in third:9
+                    for fifth in forth:9
+                        for sixth in fifth:9
+                            pw = (
+                                first * 100000
+                                + second * 10000
+                                + third * 1000
+                                + forth * 100
+                                + fifth * 10
+                                + sixth
+                            )
+                            # Ensure in range.
+                            if pw >= start && pw <= finish
+                                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
+                                end
+                            end
+                        end
+                    end
+                end
+            end
+        end
+    end
+    pw_count
+end
+
+input = map(readlines(open("input.txt"))) do line
+    map(x -> parse(Int, x), split(strip(line), '-'))
+end[1]
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))