aoc-all

My solutions to all Advent of Code
git clone https://git.instinctive.eu/aoc-all.git
Log | Files | Refs | README | LICENSE

commit ef3b58cb5b357dae05a05cc6964d55f6ffbb2cb8
parent 02255f39187cc8f74350c0725f841f21e7c3c4fa
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Tue,  3 Dec 2024 19:42:02 +0000

Day 3 reference and solutions
Diffstat:
A2024/day03.nim | 46++++++++++++++++++++++++++++++++++++++++++++++
A2024/ref/day03a.txt | 1+
A2024/ref/day03b.txt | 1+
3 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/2024/day03.nim b/2024/day03.nim @@ -0,0 +1,46 @@ +# Copyright (c) 2024, Natacha Porté +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +from std/re import findAll, re +from std/sequtils import foldl, map, toSeq +from std/strutils import parseInt, split + +# Read input file + +let data = stdin.lines().toSeq() + +# Puzzle 1 + +func summul(s: string): int = + for subs in findAll(s, re"mul\(\d+,\d+\)"): + let n = split(subs[4 .. ^2], ",").map(parseInt) + result += foldl(n, a * b) + +echo "Puzzle 1: ", data.map(summul).foldl(a + b) + +# Puzzle 2 + +func dosummul(s: string): int = + var active = true + for subs in findAll(s, re"mul\(\d+,\d+\)|do\(\)|don't\(\)"): + case subs[0 .. 3] + of "do()": + active = true + of "don'": + active = false + of "mul(": + if active: + result += subs[4 .. ^2].split(",").map(parseInt).foldl(a * b) + +echo "Puzzle 2: ", data.foldl(a & b).dosummul() diff --git a/2024/ref/day03a.txt b/2024/ref/day03a.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/2024/ref/day03b.txt b/2024/ref/day03b.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))