advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

day-04.jl (3250B)

    1 function part_1(input)
    2     start = input[1]
    3     finish = input[2]
    4     pw_count = 0
    5     for first in (start ÷ 100000):(finish ÷ 100000)
    6         for second in first:9
    7             if first * 100000 + second * 10000 > finish
    8                 break
    9             end
   10             for third in second:9
   11                 for forth in third:9
   12                     for fifth in forth:9
   13                         for sixth in fifth:9
   14                             pw = (
   15                                 first * 100000
   16                                 + second * 10000
   17                                 + third * 1000
   18                                 + forth * 100
   19                                 + fifth * 10
   20                                 + sixth
   21                             )
   22                             # Ensure in range.
   23                             if pw >= start && pw <= finish
   24                                 if (
   25                                     first == second
   26                                     || second == third
   27                                     || third == forth
   28                                     || forth == fifth
   29                                     || fifth == sixth
   30                                 )
   31                                     pw_count += 1
   32                                 end
   33                             end
   34                         end
   35                     end
   36                 end
   37             end
   38         end
   39     end
   40     pw_count
   41 end
   42 
   43 function part_2(input)
   44     start = input[1]
   45     finish = input[2]
   46     pw_count = 0
   47     for first in (start ÷ 100000):(finish ÷ 100000)
   48         for second in first:9
   49             if first * 100000 + second * 10000 > finish
   50                 break
   51             end
   52             for third in second:9
   53                 for forth in third:9
   54                     for fifth in forth:9
   55                         for sixth in fifth:9
   56                             pw = (
   57                                 first * 100000
   58                                 + second * 10000
   59                                 + third * 1000
   60                                 + forth * 100
   61                                 + fifth * 10
   62                                 + sixth
   63                             )
   64                             # Ensure in range.
   65                             if pw >= start && pw <= finish
   66                                 if (
   67                                     (first == second && second != third)
   68                                     || (first != second && second == third && third != forth)
   69                                     || (second != third && third == forth && forth != fifth)
   70                                     || (third != forth && forth == fifth && fifth != sixth)
   71                                     || (forth != fifth && fifth == sixth)
   72                                 )
   73                                     pw_count += 1
   74                                 end
   75                             end
   76                         end
   77                     end
   78                 end
   79             end
   80         end
   81     end
   82     pw_count
   83 end
   84 
   85 input = map(readlines(open("input.txt"))) do line
   86     map(x -> parse(Int, x), split(strip(line), '-'))
   87 end[1]
   88 
   89 println("Julia:")
   90 println("Part 1: ", part_1(input))
   91 println("Part 2: ", part_2(input))