From 4be75cc24efebf5fa196f7ffc5b0723fd8a94f38 Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Tue, 2 Jan 2024 16:54:41 +0100 Subject: [PATCH] [Day 7] Read input only once --- README.md | 2 +- src/day07.jl | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2af45f4..bca9a14 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven | 4 | [:white_check_mark:](https://adventofcode.com/2023/day/4) | 2.745 ms | 1.40 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day04.jl) | | 5 | [:white_check_mark:](https://adventofcode.com/2023/day/5) | 71.969 ms | 198.09 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day05.jl) | | 6 | [:white_check_mark:](https://adventofcode.com/2023/day/6) | 7.953 μs | 7.44 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day06.jl) | -| 7 | [:white_check_mark:](https://adventofcode.com/2023/day/7) | 13.584 ms | 18.21 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day07.jl) | +| 7 | [:white_check_mark:](https://adventofcode.com/2023/day/7) | 11.831 ms | 15.28 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day07.jl) | | 8 | [:white_check_mark:](https://adventofcode.com/2023/day/8) | 11.879 ms | 556.42 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day08.jl) | | 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) | diff --git a/src/day07.jl b/src/day07.jl index d628ca7..0f752a5 100644 --- a/src/day07.jl +++ b/src/day07.jl @@ -16,13 +16,12 @@ end function day07(input::String = readInput(joinpath(@__DIR__, "..", "data", "day07.txt"))) hands = [Hand([_to_value(c) for c ∈ x[1]], parse(Int, x[2])) for x ∈ split.(eachsplit(rstrip(input), "\n"), " ")] p1 = solve(hands) - handsp2 = [Handp2([_to_value(c; p2=true) for c ∈ x[1]], parse(Int, x[2])) for x ∈ split.(eachsplit(rstrip(input), "\n"), " ")] + handsp2 = map(y -> Handp2(map(x -> x == 11 ? 1 : x, y.cards), y.bid), hands) p2 = solve(handsp2) return [p1, p2] end -function _to_value(c::Char; p2 = false) - p2 && c == 'J' && return Int8(1) +function _to_value(c::Char) isdigit(c) && return Int8(c - '0') c == 'T' && return Int8(10) c == 'J' && return Int8(11)