Day 11 part 1 done

This commit is contained in:
Daniel Flanagan 2021-12-13 10:51:18 -06:00
parent 3562f82032
commit c9896ab4e6
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
3 changed files with 61 additions and 34 deletions

View file

@ -44,11 +44,15 @@ proc doDay*[T](
expectedPart1: int, expectedPart1: int,
expectedPart2: int): void = expectedPart2: int): void =
time(&"day {day} part 1"): echo day.inputLoader().part1() time(&"day {day} part 1"): echo day.inputLoader().part1()
time(&"day {day} part 2"): echo day.inputLoader().part2() # time(&"day {day} part 2"): echo day.inputLoader().part2()
when not defined(release): when not defined(release):
doAssert testInput.part1() == expectedPart1 var p1 = testInput.part1()
doAssert testInput.part2() == expectedPart2 echo p1
doAssert p1 == expectedPart1
# var p2 = testInput.part2()
# echo p2
# doAssert p2 == expectedPart2
proc reduce*[T, X](s: openArray[T], op: (X, T) -> X, init: X): X = proc reduce*[T, X](s: openArray[T], op: (X, T) -> X, init: X): X =
result = init result = init

54
2021/nim/day11.nim Normal file
View file

@ -0,0 +1,54 @@
import ./common, std/[sequtils, algorithm, sugar, sets, strformat, strutils]
proc toGrid(s: seq[string]): seq[seq[int]] =
for l in s: result.add(l.mapIt(int(it)-int('0')))
proc octopi(input: seq[seq[int]]): int =
var o = input
let h = o.len
let w = o[0].len
# echo &"w{w} h{h}"
for n in 1..100:
# for nums in o:
# echo nums.mapIt($it).join()
# echo n
var allFlashes = toHashSet[(int, int)] []
var flashes = toHashSet[(int, int)] []
for y in 0..<h:
for x in 0..<w:
# inc and check flashes
inc o[y][x]
if o[y][x] > 9:
# echo &"Flash {y}, {x}"
inc result
flashes.incl (y, x)
allFlashes.incl (y, x)
while flashes.len > 0:
let (y, x) = flashes.pop
# echo &"Handling flash {y}, {x}"
for p in product @[@[y-1, y, y+1], @[x-1, x, x+1]]:
let (fy, fx) = (p[0], p[1])
if fx < 0 or fy < 0 or fx >= w or fy >= h or (fx == x and fy == y): continue
# inc and check flashes
inc o[fy][fx]
if o[fy][fx] > 9 and not allFlashes.contains (fy, fx):
# echo &"Subflash {fy}, {fx}"
inc result
flashes.incl (fy, fx)
allFlashes.incl (fy, fx)
for f in allFlashes.items():
o[f[0]][f[1]] = 0
doDay 11, n => n.loadInput().toGrid(), octopi, octopi,
@[
"5483143223",
"2745854711",
"5264556173",
"6141336146",
"6357385478",
"4167524645",
"2176841721",
"6882881134",
"4846848554",
"5283751526",
].toGrid(), 1656, 288957

View file

@ -1,31 +0,0 @@
import ./common, std/[strutils, sequtils, algorithm, sugar, tables]
const pairs = toTable {'(': ')', '[': ']', '{': '}', '<': '>'}
const corruptPoints = toTable {')': 3, ']': 57, '}': 1197, '>': 25137}
proc parse(input: seq[string]): (int, int) =
var cmps = newSeq[int]()
for line in input:
var stack: seq[char] = @[]
for ch in line:
if pairs.hasKey ch: stack.add pairs[ch]
elif stack.pop != ch:
result[0] += corruptPoints[ch]
stack.setLen 0
break
if stack.len > 0: cmps.add stack.mapIt(" )]}>".find it).foldr 5 * b + a
result[1] = cmps.sorted[cmps.len div 2]
doDay 10, n => n.loadInput(), s => s.parse[0], s => s.parse[1],
@[
"[({(<(())[]>[[{[]{<()<>>",
"[(()[<>])]({[<{<<[]>>(",
"{([(<{}[<>[]}>{[]{[(<()>",
"(((({<>}<{<{<>}{[]{[]{}",
"[[<[([]))<([[{}[[()]]]",
"[{[{({}]{}}([{[{{{}}([]",
"{<[[]]>}<{[{[{[]{()[[[]",
"[<(<(<(<{}))><([]([]()",
"<{([([[(<>()){}]>(<<{{",
"<{([{{}}[<[[[<>{}]]]>[]]",
], 26397, 288957