Day 2 implementation
This commit is contained in:
parent
3dd7542619
commit
2439761d36
|
@ -8,4 +8,4 @@ proc solve_for_day(n: int) {.used.} =
|
|||
echo solvers[n]()
|
||||
|
||||
when isMainModule:
|
||||
solve_for_day(2)
|
||||
solve_all()
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
import sets, streams, strutils, options
|
||||
import sets, streams, strutils, options, input_helpers
|
||||
|
||||
# Day 1
|
||||
|
||||
let day1TargetSum = 2020
|
||||
|
||||
proc part1*(strm: Stream): Option[int] =
|
||||
proc part1*(s: Stream): int =
|
||||
var targets = initHashSet[int]()
|
||||
for line in strm.lines():
|
||||
let n = parseInt line
|
||||
for n in asInts(s):
|
||||
if targets.contains(day1TargetSum - n):
|
||||
return some((day1TargetSum - n) * n)
|
||||
return (day1TargetSum - n) * n
|
||||
else:
|
||||
targets.incl n
|
||||
|
||||
proc part2*(strm: Stream): Option[int] =
|
||||
proc part2*(strm: Stream): int =
|
||||
# this works exactly the same as the previous algorithm, except we simply
|
||||
# permute once more
|
||||
# TODO: if I was really cool, I could split the shared functionality into
|
||||
|
@ -26,5 +25,5 @@ proc part2*(strm: Stream): Option[int] =
|
|||
let nTargetSum = day1TargetSum - n
|
||||
for n2 in nums:
|
||||
if iset.contains(nTargetSum - n2):
|
||||
return some(n * n2 * (nTargetSum - n2))
|
||||
return n * n2 * (nTargetSum - n2)
|
||||
iset.incl n2
|
||||
|
|
|
@ -1,7 +1,32 @@
|
|||
import streams
|
||||
import streams, strutils, sequtils, re, options
|
||||
|
||||
proc part1*(strm: Stream): string =
|
||||
"ayy"
|
||||
type PasswordPolicy = tuple[min: int, max: int, keyChar: char]
|
||||
|
||||
proc part2*(strm: Stream): string =
|
||||
"ayy"
|
||||
proc isValidPassword(str: string, pp: PasswordPolicy): bool =
|
||||
let count = str.count pp.keyChar
|
||||
(pp.min <= count) and (count <= pp.max)
|
||||
|
||||
proc isValidPasswordPart2(str: string, pp: PasswordPolicy): bool =
|
||||
(str[pp.min - 1] == pp.keyChar) xor (pp.keyChar == str[pp.max - 1])
|
||||
|
||||
let parsePasswordPolicyRe = re"^(\d+)-(\d+) (.): (.*)$"
|
||||
proc parsePasswordPolicy(str: string): (PasswordPolicy, string) =
|
||||
var matches: array[4, string]
|
||||
if match(str, parsePasswordPolicyRe, matches):
|
||||
return ((min: parseInt(matches[0]), max: parseInt(matches[1]), keyChar: matches[2][0]), matches[3])
|
||||
|
||||
iterator asPasswordPolicies(s: Stream): (PasswordPolicy, string) =
|
||||
for line in s.lines():
|
||||
yield parsePasswordPolicy line
|
||||
|
||||
proc part1*(s: Stream): int =
|
||||
result = 0
|
||||
for (pp, pw) in asPasswordPolicies(s):
|
||||
if isValidPassword(pw, pp):
|
||||
result += 1
|
||||
|
||||
proc part2*(s: Stream): int =
|
||||
result = 0
|
||||
for (pp, pw) in asPasswordPolicies(s):
|
||||
if isValidPasswordPart2(pw, pp):
|
||||
result += 1
|
||||
|
|
7
2020/src/day3.nim
Normal file
7
2020/src/day3.nim
Normal file
|
@ -0,0 +1,7 @@
|
|||
import streams
|
||||
|
||||
proc part1*(s: Stream): int =
|
||||
0
|
||||
|
||||
proc part2*(s: Stream): int =
|
||||
0
|
|
@ -3,14 +3,16 @@ import input_requestor, os, macros, strformat, tables
|
|||
macro loadDays(): untyped =
|
||||
var solver_str = "var solvers = {\n"
|
||||
result = newStmtList()
|
||||
for day in 1..2:
|
||||
for day in 1..3:
|
||||
let module = fmt"day{day}"
|
||||
if fileExists joinPath("src/", &"{module}.nim"):
|
||||
result.add parseStmt fmt"from {module} import nil"
|
||||
solver_str = solver_str & &"""
|
||||
{day}: proc(): (string, string) = (
|
||||
$(day{day}.part1(getInputFileStreamForDay({day}))),
|
||||
$(day{day}.part2(getInputFileStreamForDay({day})))
|
||||
{day}: proc(): tuple[part1: int, part2: int] =
|
||||
echo "Day {day}"
|
||||
(
|
||||
{module}.part1(getInputFileStreamForDay({day})),
|
||||
{module}.part2(getInputFileStreamForDay({day}))
|
||||
),
|
||||
"""
|
||||
result.add parseStmt (solver_str & "\n}.newTable()\nexport solvers")
|
||||
|
|
9
2020/src/input_helpers.nim
Normal file
9
2020/src/input_helpers.nim
Normal file
|
@ -0,0 +1,9 @@
|
|||
import streams, strutils, sugar
|
||||
|
||||
iterator mapStream*[T](s: Stream, cb: (string) -> T): T =
|
||||
for line in s.lines():
|
||||
yield cb line
|
||||
|
||||
iterator asInts*(s: Stream): int =
|
||||
for i in mapStream[int](s, parseInt):
|
||||
yield i
|
Loading…
Reference in a new issue