Skip to content

Commit

Permalink
[Day 12] Add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Dec 13, 2023
1 parent fba0fb5 commit 42beaf9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.1.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Memoize = "c03570c3-d221-55d1-a50c-7939bbd78826"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[compat]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
| 9 | [:white_check_mark:](https://adventofcode.com/2023/day/9) | 1.408 ms | 1.72 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day09.jl) |
| 10 | [:white_check_mark:](https://adventofcode.com/2023/day/10) | 43.997 ms | 8.82 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day10.jl) |
| 11 | [:white_check_mark:](https://adventofcode.com/2023/day/11) | 4.728 ms | 1.71 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day11.jl) |
<!-- | 12 | [:white_check_mark:](https://adventofcode.com/2023/day/12) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day12.jl) | -->
| 12 | [:white_check_mark:](https://adventofcode.com/2023/day/12) | 9.320 ms | 2.64 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day12.jl) |
<!-- | 13 | [:white_check_mark:](https://adventofcode.com/2023/day/13) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day13.jl) | -->
<!-- | 14 | [:white_check_mark:](https://adventofcode.com/2023/day/14) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day14.jl) | -->
<!-- | 15 | [:white_check_mark:](https://adventofcode.com/2023/day/15) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day15.jl) | -->
Expand Down
2 changes: 1 addition & 1 deletion src/AdventOfCode2023.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module AdventOfCode2023
using BenchmarkTools
using Printf

solvedDays = 1:11
solvedDays = 1:12
# Include the source files:
for day in solvedDays
ds = @sprintf("%02d", day)
Expand Down
49 changes: 49 additions & 0 deletions src/day12.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Day12

using AdventOfCode2023
using Memoize


function day12(input::String = readInput(joinpath(@__DIR__, "..", "data", "day12.txt")))
patterngroups = parse_input(input)
p1 = sum(count_combinations(p, g) for (p, g) patterngroups)
return [p1, part2(patterngroups)]
end

function parse_input(input::AbstractString)
patterngroups = []
for (pattern, instructions) split.(eachsplit(rstrip(input), "\n"), " ")
numbers = Tuple(parse.(Int, split(instructions, ",")))
push!(patterngroups, (pattern, numbers))
end
return patterngroups
end

@memoize function count_combinations(pattern::AbstractString, groups::Tuple)
count = 0
if isempty(groups)
'#' pattern && return 0
return 1
end
length(pattern) < sum(groups) + length(groups) - 1 && return 0
if '.' pattern[begin:groups[begin]]
if length(pattern) == groups[begin] || pattern[groups[begin]+1] != '#'
count += count_combinations(pattern[groups[begin] + 2:end], groups[begin+1:end])
end
end
if pattern[begin] != '#'
count += count_combinations(pattern[begin + 1:end], groups)
end
return count
end

function part2(patterngroups)
s = Int64(0)
for (pattern, groups) patterngroups
pattern = pattern * "?" * pattern * "?" * pattern * "?" * pattern * "?" * pattern
s += count_combinations(pattern, Tuple(repeat(collect(groups), 5)))
end
return s
end

end # module
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,15 @@ end
"#...#.....\n"
@test AdventOfCode2023.Day11.day11(sample) == [374, 82000210]
@test AdventOfCode2023.Day11.day11() == [10490062, 382979724122]
end

@testset "Day 12" begin
sample = "???.### 1,1,3\n" *
".??..??...?##. 1,1,3\n" *
"?#?#?#?#?#?#?#? 1,3,1,6\n" *
"????.#...#... 4,1,1\n" *
"????.######..#####. 1,6,5\n" *
"?###???????? 3,2,1\n"
AdventOfCode2023.Day12.day12(sample) == [21, 525152]
AdventOfCode2023.Day12.day12() == [7939, 850504257483930]
end

0 comments on commit 42beaf9

Please sign in to comment.