Day 7 part 2 bruteforced
This commit is contained in:
parent
b9a249df6b
commit
e88a868165
|
@ -1,6 +1,9 @@
|
||||||
import std/[strutils, sequtils, tables, strformat]
|
import std/[strutils, sequtils, tables, strformat]
|
||||||
import ./common
|
import ./common
|
||||||
|
|
||||||
|
proc f(n: int): int = toSeq(0..abs(n)).foldl(a + b, 0)
|
||||||
|
|
||||||
|
|
||||||
proc crabsDistance(input: string): int =
|
proc crabsDistance(input: string): int =
|
||||||
result = int.high()
|
result = int.high()
|
||||||
var max_try = 0
|
var max_try = 0
|
||||||
|
@ -11,10 +14,22 @@ proc crabsDistance(input: string): int =
|
||||||
let fuel = dists.foldl(a + abs(b - t), 0)
|
let fuel = dists.foldl(a + abs(b - t), 0)
|
||||||
if fuel < result: result = fuel
|
if fuel < result: result = fuel
|
||||||
|
|
||||||
|
proc crabsBigDistance(input: string): int =
|
||||||
|
result = int.high()
|
||||||
|
var max_try = 0
|
||||||
|
let dists = input.split(',').map(parseInt)
|
||||||
|
for d in dists:
|
||||||
|
if d > max_try: max_try = d
|
||||||
|
for t in 0..max_try:
|
||||||
|
let fuel = dists.foldl(a + f(b - t), 0)
|
||||||
|
if fuel < result: result = fuel
|
||||||
|
|
||||||
let input = 7.loadInputText()
|
let input = 7.loadInputText()
|
||||||
time("day 7 part 1"): echo input.crabsDistance()
|
time("day 7 part 1"): echo input.crabsDistance()
|
||||||
|
time("day 7 part 2"): echo input.crabsBigDistance()
|
||||||
# time("day 7 part 2"): echo "?"
|
# time("day 7 part 2"): echo "?"
|
||||||
|
|
||||||
when not defined(release):
|
when not defined(release):
|
||||||
let testInput = "16,1,2,0,4,2,7,1,2,14"
|
let testInput = "16,1,2,0,4,2,7,1,2,14"
|
||||||
doAssert testInput.crabsDistance() == 37
|
doAssert testInput.crabsDistance() == 37
|
||||||
|
doAssert testInput.crabsBigDistance() == 168
|
||||||
|
|
Loading…
Reference in a new issue