Class: AocCli::Database::Attempt

Inherits:
Object
  • Object
show all
Defined in:
lib/aoc_cli/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attempt:) ⇒ Attempt

Returns a new instance of Attempt.



41
42
43
44
45
# File 'lib/aoc_cli/database.rb', line 41

def initialize(attempt:)
  @attempt = attempt
  @db = Query.new(path:Paths::Database.cfg(attempt.user))
    .table(t:"attempts", cols:cols)
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



40
41
42
# File 'lib/aoc_cli/database.rb', line 40

def attempt
  @attempt
end

#dbObject (readonly)

Returns the value of attribute db.



40
41
42
# File 'lib/aoc_cli/database.rb', line 40

def db
  @db
end

Instance Method Details

#colsObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/aoc_cli/database.rb', line 69

def cols
  { time: :TEXT, 
    year: :INT, 
    day: :INT, 
    part: :INT, 
    answer: :TEXT, 
    correct: :INT,
    low: :INT,
    high: :INT }
end

#correctObject



46
47
48
49
# File 'lib/aoc_cli/database.rb', line 46

def correct
  db.insert(t:"attempts", val:data << 1 << 0 << 0)
  self
end

#count_attemptsObject



79
80
81
# File 'lib/aoc_cli/database.rb', line 79

def count_attempts
  db.select(t:"attempts", where:where).count
end

#dataObject



62
63
64
65
66
67
68
# File 'lib/aoc_cli/database.rb', line 62

def data
  [ "'#{Time.now}'",
    "'#{attempt.year}'",
    "'#{attempt.day}'",
    "'#{attempt.part}'",
    "'#{attempt.answer}'" ]
end

#duplicate?(ans:) ⇒ Boolean

Returns:



55
56
57
58
# File 'lib/aoc_cli/database.rb', line 55

def duplicate?(ans:)
  db.select(t:"attempts", 
        where:where.merge({answer:ans})).count > 0
end

#incorrect(low:, high:) ⇒ Object



50
51
52
53
54
# File 'lib/aoc_cli/database.rb', line 50

def incorrect(low:, high:)
  db.insert(t:"attempts", 
    val:data << 0 << parse_hint(low:low, high:high))
  self
end

#parse_hint(low:, high:) ⇒ Object



59
60
61
# File 'lib/aoc_cli/database.rb', line 59

def parse_hint(low:, high:)
  [ low ? 1 : 0, high ? 1 : 0 ]
end

#whereObject



82
83
84
85
86
# File 'lib/aoc_cli/database.rb', line 82

def where
  { year:attempt.year,
    day:attempt.day,
    part:attempt.part }
end